leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE | |
commit | 3c9abce7a0156391f17fb7e03c4a7635375a1ae4 |
parent | 61cbebf2c36bcaf515c4336bfd3df65072040435 |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Mon, 5 Feb 2024 17:12:31 +0000 |
1 Random Problem
Diffstat:A | Problems/1775.cpp | | | +++++++++++++++++++++++++++++++++++++++++++++ |
M | README.md | | | + |
2 files changed, 46 insertions(+), 0 deletions(-)
diff --git a/Problems/1775.cpp b/Problems/1775.cpp
@@ -0,0 +1,45 @@
class Solution {
public:
int minOperations(vector<int> &nums1, vector<int> &nums2) const {
int n = size(nums1), m = size(nums2);
if (6 * n < m || 6 * m < n) return -1;
const int s1 = accumulate(begin(nums1), end(nums1), 0);
const int s2 = accumulate(begin(nums2), end(nums2), 0);
if (s1 > s2) swap(nums1, nums2), swap(n, m);
make_heap(begin(nums1), end(nums1), greater<int>());
make_heap(begin(nums2), end(nums2), less<int>());
int goal = abs(s1 - s2), res = 0;
while (goal > 0 && !empty(nums1) && !empty(nums2)) {
res++;
if (6 - nums1.front() > nums2.front() - 1) {
goal -= 6 - nums1.front();
pop_heap(begin(nums1), end(nums1), greater<int>());
nums1.pop_back();
} else {
goal -= nums2.front() - 1;
pop_heap(begin(nums2), end(nums2), less<int>());
nums2.pop_back();
}
}
while (goal > 0 && !empty(nums1)) {
goal -= 6 - nums1.front();
pop_heap(begin(nums1), end(nums1), greater<int>());
nums1.pop_back();
res++;
}
while (goal > 0 && !empty(nums2)) {
goal -= nums2.front() - 1;
pop_heap(begin(nums2), end(nums2), less<int>());
nums2.pop_back();
res++;
}
return goal <= 0 ? res : -1;
}
};
diff --git a/README.md b/README.md
@@ -888,6 +888,7 @@ for solving problems.
| 1765 | Medium | [Map of Highest Peak](Problems/1765.cpp) |
| 1768 | Easy | [Merge Strings Alternately](Problems/1768.cpp) |
| 1769 | Medium | [Minimum Number of Operations to Move All Balls to Each Box](Problems/1769.cpp) |
| 1775 | Medium | [Equal Sum Arrays With Minimum Number of Operations](Problems/1775.cpp) |
| 1780 | Medium | [Check if Number is a Sum of Powers of Three](Problems/1780.cpp) |
| 1781 | Medium | [Sum of Beauty of All Substrings](Problems/1781.cpp) |
| 1786 | Medium | [Number of Restricted Paths From First to Last Node](Problems/1786.cpp) |