Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
advantage-shuffle.cpp 1.00 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2018-07-15 14:15 +08:00 . Create advantage-shuffle.cpp
// Time: O(nlogn)
// Space: O(n)
class Solution {
public:
vector<int> advantageCount(vector<int>& A, vector<int>& B) {
vector<int> sortedA(A.cbegin(), A.cend());
sort(sortedA.begin(), sortedA.end());
vector<int> sortedB(B.cbegin(), B.cend());
sort(sortedB.begin(), sortedB.end());
unordered_map<int, vector<int>> candidates;
vector<int> others;
int j = 0;
for (const auto& a : sortedA) {
if (a > sortedB[j]) {
candidates[sortedB[j]].emplace_back(a);
++j;
} else {
others.emplace_back(a);
}
}
vector<int> result;
for (const auto& b : B) {
if (!candidates[b].empty()) {
result.emplace_back(candidates[b].back());
candidates[b].pop_back();
} else {
result.emplace_back(others.back());
others.pop_back();
}
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助