栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

LeetCode-870. Advantage Shuffle [C++][Java]

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LeetCode-870. Advantage Shuffle [C++][Java]

LeetCode-870. Advantage Shufflehttps://leetcode.com/problems/advantage-shuffle/

You are given two integer arrays nums1 and nums2 both of the same length. The advantage of nums1 with respect to nums2 is the number of indices i for which nums1[i] > nums2[i].

Return any permutation of nums1 that maximizes its advantage with respect to nums2.

Example 1:

Input: nums1 = [2,7,11,15], nums2 = [1,10,4,11]
Output: [2,11,7,15]

Example 2:

Input: nums1 = [12,24,8,32], nums2 = [13,25,32,11]
Output: [24,32,8,12]

Constraints:

  • 1 <= nums1.length <= 10^5
  • nums2.length == nums1.length
  • 0 <= nums1[i], nums2[i] <= 10^9

【C++】
class Solution {
public:
    vector advantageCount(vector& nums1, vector& nums2) {
        multiset t;
        for (auto num : nums1) {t.insert(num);}
        vector ans;
        for (auto num : nums2) {
            auto upper = t.upper_bound(num);
            if (upper != t.end()) {
                ans.push_back(*upper);
                t.erase(upper);
            } else {
                ans.push_back(*t.begin());
                t.erase(t.begin());
            }
        }
        return ans;
    }
};

【Java】
class Solution {
    public int[] advantageCount(int[] nums1, int[] nums2) {
        PriorityQueue pq = new PriorityQueue<>((a,b) -> nums2[b]-nums2[a]);
        for (int i=0; i nums2[index]){
                v[index] = nums1[right--];
            } else {
                v[index]=nums1[left++];
            }
        }
        return v;
    }
}

参考文献

【1】multiset用法总结_二喵君的博客-CSDN博客_multiset

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/857352.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号