首页 > 其他 > 详细

[Greedy] leetcode 870 Advantage Shuffle

时间:2019-08-18 01:14:28      阅读:124      评论:0      收藏:0      [点我收藏+]

problem: https://leetcode.com/problems/advantage-shuffle/

        In ordered to maximize the advantage of array A with respect to B, we had better choose the smallest element in array A that is larger than the element in B. After each selection, we erase the data we choose in A to avoid repetition.

class Solution {
public:
    vector<int> advantageCount(vector<int>& A, vector<int>& B) {
        vector<int> res;
        multiset<int> datas(A.begin(), A.end());
        for(int i = 0;i < B.size(); i++)
        {
            // find the smallest number in array A that is larger than the element in array B
            auto it = datas.upper_bound(B[i]);
            
            if(it != datas.end())
            {
                res.push_back(*it);
                datas.erase(it);
            }
            else
            {
                res.push_back(*datas.begin());
                datas.erase(datas.begin());
            }
        }
        
        return res;
    }
};

 

[Greedy] leetcode 870 Advantage Shuffle

原文:https://www.cnblogs.com/fish1996/p/11370967.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!