870:优势洗牌
This commit is contained in:
parent
e4e1e9c26f
commit
8a4171b087
72
src/main/java/leetcode/editor/cn/AdvantageShuffle.java
Normal file
72
src/main/java/leetcode/editor/cn/AdvantageShuffle.java
Normal file
@ -0,0 +1,72 @@
|
||||
//给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述。
|
||||
//
|
||||
// 返回 A 的任意排列,使其相对于 B 的优势最大化。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
// 输入:A = [2,7,11,15], B = [1,10,4,11]
|
||||
//输出:[2,11,7,15]
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
// 输入:A = [12,24,8,32], B = [13,25,32,11]
|
||||
//输出:[24,32,8,12]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= A.length = B.length <= 10000
|
||||
// 0 <= A[i] <= 10^9
|
||||
// 0 <= B[i] <= 10^9
|
||||
//
|
||||
// Related Topics 贪心算法 数组
|
||||
// 👍 125 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
//870:优势洗牌
|
||||
public class AdvantageShuffle {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new AdvantageShuffle().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] advantageCount(int[] A, int[] B) {
|
||||
int length = A.length;
|
||||
Pair<Integer, Integer>[] pairs = new Pair[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
pairs[i] = new Pair<>(i, B[i]);
|
||||
}
|
||||
Arrays.sort(pairs, Comparator.comparingInt(Pair::getValue));
|
||||
int[] copy = Arrays.copyOf(A, length);
|
||||
Arrays.sort(copy);
|
||||
int i = 0, j = 0;
|
||||
while(i < A.length) {
|
||||
if(copy[i] > pairs[j].getValue()){
|
||||
A[pairs[j].getKey()] = copy[i];
|
||||
i++;
|
||||
j++;
|
||||
} else {
|
||||
A[pairs[(A.length-1) - (i-j)].getKey()] = copy[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return A;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
28
src/main/java/leetcode/editor/cn/AdvantageShuffle.md
Normal file
28
src/main/java/leetcode/editor/cn/AdvantageShuffle.md
Normal file
@ -0,0 +1,28 @@
|
||||
<p>给定两个大小相等的数组 <code>A</code> 和 <code>B</code>,A 相对于 B 的<em>优势</em>可以用满足 <code>A[i] > B[i]</code> 的索引 <code>i</code> 的数目来描述。</p>
|
||||
|
||||
<p>返回 <code>A</code> 的<strong>任意</strong>排列,使其相对于 <code>B</code> 的优势最大化。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>A = [2,7,11,15], B = [1,10,4,11]
|
||||
<strong>输出:</strong>[2,11,7,15]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>A = [12,24,8,32], B = [13,25,32,11]
|
||||
<strong>输出:</strong>[24,32,8,12]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= A.length = B.length <= 10000</code></li>
|
||||
<li><code>0 <= A[i] <= 10^9</code></li>
|
||||
<li><code>0 <= B[i] <= 10^9</code></li>
|
||||
</ol>
|
||||
<div><div>Related Topics</div><div><li>贪心算法</li><li>数组</li></div></div>\n<div><li>👍 125</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user