leetCode870题田忌赛马

LeetCode870题

给定两个大小相等的数组nums1和nums2,nums1相对于 nums2 的优势可以用满足nums1[i] > nums2[i]的索引 i的数目来描述。

返回 nums1的任意排列,使其相对于 nums2的优势最大化

示例 1:

输入:nums1 = [2,7,11,15], nums2 = [1,10,4,11]
输出:[2,11,7,15]
示例 2:

输入:nums1 = [12,24,8,32], nums2 = [13,25,32,11]
输出:[24,32,8,12]

提示:

1 <= nums1.length <= 105
nums2.length == nums1.length
0 <= nums1[i], nums2[i] <= 109

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/advantage-shuffle
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
LeetCode870题

思路:

有数组nums1和数组nums2,先将nums2的元素按着从大到小放入队列,且放入其下标位置
将nums1按着从小到大排列
依次取出队列中的元素
取出数组1的最后一个元素跟队列中的第一个元素比较,如果大于队列的第一个元素则将其放入结果数组的对应下标的位置如果小于队列第1个元素则数组1的最小元素放入其对应的下标

思路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static void main(String[] args) {

int[] nums1 = {2,11,7,15};
int[] nums2 ={1,10,4,11};
int[] ints = advantageCount(nums1, nums2);
for (int anInt : ints) {
System.out.println(anInt);
}

}

public static int[] advantageCount(int[] nums1, int[] nums2) {
int n = nums1.length;
int[] ans = new int[n];
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> o2[0] - o1[0]);
//将nums2的元素按从大到小插入到队列中,并记录其下标
//11,3 10,1 4,2 1,0
//
for (int i = 0; i < n; i++) {
pq.offer(new int[]{nums2[i], i});
}
//重排序nums1
Arrays.sort(nums1);
int lo = 0, hi = n - 1;
while(!pq.isEmpty()) {
int[] arr = pq.poll();
if (nums1[hi] > arr[0]) {
ans[arr[1]] = nums1[hi--];
} else {
ans[arr[1]] = nums1[lo++];
}
}
return ans;
}

思路二

拉出一匹马,用我自己的比当前马大的所有马中最小的马去比,比的过则放入结果集,比不过则拉一匹最小的马放入结果集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

int[] advantageCount(int[] nums1, int[] nums2) {
int n = nums1.length;
// 给 nums2 降序排序
PriorityQueue<int[]> maxpq = new PriorityQueue<>(
(int[] pair1, int[] pair2) -> {
return pair2[1] - pair1[1];
}
);
for (int i = 0; i < n; i++) {
maxpq.offer(new int[]{i, nums2[i]});
}
// 给 nums1 升序排序
Arrays.sort(nums1);

// nums1[left] 是最小值,nums1[right] 是最大值
int left = 0, right = n - 1;
int[] res = new int[n];

while (!maxpq.isEmpty()) {
int[] pair = maxpq.poll();
// maxval 是 nums2 中的最大值,index 是对应索引
int index = pair[0], maxval = pair[1];
if (maxval < nums1[right]) {
// 如果 nums1[right] 能胜过 maxval,那就直接上
res[index] = nums1[right];
right--;
} else {
// 战胜不过,就用最小值去送
res[index] = nums1[left];
left++;
}
}
return res;
}

官网解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public int[] advantageCount(int[] nums1, int[] nums2) {
int n = nums1.length;
Integer[] idx1 = new Integer[n];
Integer[] idx2 = new Integer[n];
for (int i = 0; i < n; ++i) {
idx1[i] = i;
idx2[i] = i;
}
Arrays.sort(idx1, (i, j) -> nums1[i] - nums1[j]);
Arrays.sort(idx2, (i, j) -> nums2[i] - nums2[j]);

int[] ans = new int[n];
int left = 0, right = n - 1;
for (int i = 0; i < n; ++i) {
if (nums1[idx1[i]] > nums2[idx2[left]]) {
ans[idx2[left]] = nums1[idx1[i]];
++left;
} else {
ans[idx2[right]] = nums1[idx1[i]];
--right;
}
}
return ans;
}
}

作者:LeetCode-Solution
链接:https://leetcode.cn/problems/advantage-shuffle/solution/you-shi-xi-pai-by-leetcode-solution-sqsf/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

{% if post.top %} 置顶 | {% endif %}