leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE | |
commit | ccdf347047f64d09dcb3aa68f08b4a3d68295480 |
parent | ef75c4a89367a5bad7e5e7301cd437489cf02cda |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Mon, 8 Apr 2024 14:18:12 +0200 |
1 Random Problem
Diffstat:A | Problems/0870.cpp | | | ++++++++++++++++++++++++ |
M | README.md | | | + |
2 files changed, 25 insertions(+), 0 deletions(-)
diff --git a/Problems/0870.cpp b/Problems/0870.cpp
@@ -0,0 +1,24 @@
class Solution {
public:
vector<int> advantageCount(vector<int> &nums1, vector<int> &nums2) {
static int indices[100001];
const int n = size(nums1);
sort(begin(nums1), end(nums1), greater<int>());
iota(indices, indices + n, 0);
sort(indices, indices + n, [&nums2](int a, int b) { return nums2[a] > nums2[b]; });
int i = 0, j = 0, k = n - 1;
vector<int> res(n);
while (i <= k) {
if (nums1[i] > nums2[indices[j]])
res[indices[j++]] = nums1[i++];
else
res[indices[j++]] = nums1[k--];
}
return res;
}
};
diff --git a/README.md b/README.md
@@ -519,6 +519,7 @@ for solving problems.
| 0865 | Medium | [Smallest Subtree with all the Deepest Nodes](Problems/0865.cpp) |
| 0867 | Easy | [Transpose Matrix](Problems/0867.cpp) |
| 0869 | Medium | [Reordered Power of 2](Problems/0869.cpp) |
| 0870 | Medium | [Advantage Shuffle](Problems/0870.cpp) |
| 0872 | Easy | [Leaf-Similar Trees](Problems/0872.cpp) |
| 0875 | Medium | [Koko Eating Bananas](Problems/0875.cpp) |
| 0876 | Easy | [Middle of the Linked List](Problems/0876.cpp) |