[leetcode]870. Advantage Shuffle

[leetcode]870. Advantage Shuffle


Analysis

放假回家效率果然打折—— [每天刷题并不难0.0]

Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indices i for which A[i] > B[i].
Return any permutation of A that maximizes its advantage with respect to B.
在这里插入图片描述

Explanation:

用hashmap解决
notes:
对HashMap,key为其他字符的测试
显示多字符串:写入,读取,无序
单字符:写入无序,读取按照key顺序排列
单数字:写入无序,读取按照key顺序排列

Implement

class Solution {
public:
    vector<int> advantageCount(vector<int>& A, vector<int>& B) {
        map<int, int> mmp;
        vector<int> res;
        for(int a:A){
            if(mmp.find(a) == mmp.end())
                mmp[a] = 1;
            else
                mmp[a]++;
        }
        for(int b:B){
            auto ait = mmp.upper_bound(b);
            auto it = (ait != mmp.end())?ait:mmp.begin();
            res.push_back(it->first);
            if(it->second > 1)
                it->second--;
            else
                mmp.erase(it);
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/86687777