leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 5bd65ab9300f70ee922b0baa973adc2b91a19fdf
parent 6646f95b1758ad0ff4113da8268ace6e54b2f4d0
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sat,  4 Nov 2023 20:42:56 +0000

Daily Problem and 1 Random Problem

Diffstat:
AProblems/1503.cpp | 9+++++++++
AProblems/2856.cpp | 47+++++++++++++++++++++++++++++++++++++++++++++++
MREADME.md | 2++
3 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/Problems/1503.cpp b/Problems/1503.cpp @@ -0,0 +1,9 @@ +class Solution { + public: + int getLastMoment(int n, const vector<int> &left, const vector<int> &right) const { + int l = 0, r = 0; + if (!left.empty()) l = *max_element(begin(left), end(left)); + if (!right.empty()) r = n - *min_element(begin(right), end(right)); + return max(l, r); + } +}; diff --git a/Problems/2856.cpp b/Problems/2856.cpp @@ -0,0 +1,47 @@ +class Solution { + public: + int minLengthAfterRemovals(const vector<int> &nums) const { + priority_queue<int> pq; + + int count = 1; + for (int i = 1; i < nums.size(); i++) { + if (nums[i] != nums[i - 1]) { + pq.push(count); + count = 1; + } else + count++; + } + pq.push(count); + + while (pq.size() > 1) { + const int first = pq.top(); + pq.pop(); + const int second = pq.top(); + pq.pop(); + if (first > 1) pq.push(first - 1); + if (second > 1) pq.push(second - 1); + } + + return pq.size() ? pq.top() : 0; + } +}; + +// Not sure why this works... +class Solution { + public: + int minLengthAfterRemovals(const vector<int> &nums) const { + const int n = nums.size(); + int count = 1, maxi = 0; + for (int i = 1; i < n; i++) { + if (nums[i] != nums[i - 1]) { + maxi = max(maxi, count); + count = 1; + } else + count++; + } + maxi = max(maxi, count); + + if (maxi <= n / 2) return n % 2; + return 2 * maxi - n; + } +}; diff --git a/README.md b/README.md @@ -650,6 +650,7 @@ for solving problems. | 1493 | Medium | [Longest Subarray of 1's After Deleting One Element](Problems/1493.cpp) | | 1498 | Medium | [Number of Subsequences That Satisfy the Given Sum Condition](Problems/1498.cpp) | | 1502 | Easy | [Can Make Arithmetic Progression From Sequence](Problems/1502.cpp) | +| 1503 | Medium | [Last Moment Before All Ants Fall Out of a Plank](Problems/1503.cpp) | | 1512 | Easy | [Number of Good Pairs](Problems/1512.cpp) | | 1514 | Medium | [Path with Maximum Probability](Problems/1514.cpp) | | 1519 | Medium | [Number of Nodes in the Sub-Tree With the Same Label](Problems/1519.cpp) | @@ -893,5 +894,6 @@ for solving problems. | 2785 | Medium | [Sort Vowels in a String](Problems/2785.cpp) | | 2799 | Medium | [Count Complete Subarrays in an Array](Problems/2799.cpp) | | 2807 | Medium | [Insert Greatest Common Divisors in Linked List](Problems/2807.cpp) | +| 2856 | Medium | [Minimum Array Length After Pair Removals](Problems/2856.cpp) | | 2895 | Medium | [Minimum Processing Time](Problems/2895.cpp) | | 2900 | Medium | [Longest Unequal Adjacent Groups Subsequence I](Problems/2900.cpp) |