LeetCode 870. 优势洗牌(C++)

给定两个大小相等的数组 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

C++

class Solution {
public:
    vector advantageCount(vector& A, vector& B) 
    {
        vector res;
        int n=A.size();
        vector tmpA=A;
        vector tmpB=B;
        sort(tmpA.begin(),tmpA.end());
        sort(tmpB.begin(),tmpB.end());
        map> vec;
        vector other;
        int k=0;
        int flag=0;
        for(int i=0;itmpB[i])
                {
                    vec[tmpB[i]].push_back(tmpA[j]);
                    flag=1;
                }
                else
                {
                    other.push_back(tmpA[j]);
                }
                if(flag)
                {
                    flag=0;
                    k=j+1;
                    break;
                }
            }
        }
        k=0;
        for(int i=0;i

 

你可能感兴趣的:(LeetCode)