leetcode

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

commit 202a36afa7f6d9bc8f16cf5151a8fff4763c773c
parent 4e6b28661ee53cda09b1fcfc95fae9e360a1efb3
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu, 25 Jan 2024 21:05:16 +0000

2 Random Problems

Diffstat:
AProblems/1670.cpp | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AProblems/2924.cpp | 12++++++++++++
MREADME.md | 2++
3 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/Problems/1670.cpp b/Problems/1670.cpp @@ -0,0 +1,71 @@ +class FrontMiddleBackQueue { + public: + deque<int> left, right; + + void balance() { + if (size(left) > size(right)) { + right.push_front(left.back()); + left.pop_back(); + } + + if (size(left) + 1 < size(right)) { + left.push_back(right.front()); + right.pop_front(); + } + } + + void pushFront(int val) { + left.push_front(val); + balance(); + } + + void pushMiddle(int val) { + left.push_back(val); + balance(); + } + + void pushBack(int val) { + right.push_back(val); + balance(); + } + + int popFront() { + if (size(left) == 0) { + if (size(right) == 0) return -1; + const int res = right.front(); + right.pop_front(); + return res; + } + const int res = left.front(); + left.pop_front(); + balance(); + return res; + } + + int popMiddle() { + if (size(right) + size(left) == 0) return -1; + if (size(left) < size(right)) { + const int res = right.front(); + right.pop_front(); + return res; + } else { + const int res = left.back(); + left.pop_back(); + balance(); + return res; + } + } + + int popBack() { + if (size(right) == 0) { + if (size(left) == 0) return -1; + const int res = left.back(); + left.pop_back(); + return res; + } + const int res = right.back(); + right.pop_back(); + balance(); + return res; + } +}; diff --git a/Problems/2924.cpp b/Problems/2924.cpp @@ -0,0 +1,12 @@ +class Solution { + public: + int findChampion(int n, const vector<vector<int>> &edges) const { + bitset<128> bs; + for (const auto &edge : edges) + bs.set(edge[1]); + if (bs.count() + 1 != n) return -1; + for (int i = 0; i < n; i++) + if (!bs.test(i)) return i; + return -2; + } +}; diff --git a/README.md b/README.md @@ -830,6 +830,7 @@ for solving problems. | 1664 | Medium | [Ways to Make a Fair Array](Problems/1664.cpp) | | 1667 | Easy | [Fix Names in a Table](Problems/1667.cpp) | | 1669 | Medium | [Merge In Between Linked Lists](Problems/1669.cpp) | +| 1670 | Medium | [Design Front Middle Back Queue](Problems/1670.cpp) | | 1672 | Easy | [Richest Customer Wealth](Problems/1672.cpp) | | 1675 | Hard | [Minimize Deviation in Array](Problems/1675.cpp) | | 1679 | Medium | [Max Number of K-Sum Pairs](Problems/1679.cpp) | @@ -1097,6 +1098,7 @@ for solving problems. | 2895 | Medium | [Minimum Processing Time](Problems/2895.cpp) | | 2900 | Medium | [Longest Unequal Adjacent Groups Subsequence I](Problems/2900.cpp) | | 2914 | Medium | [Minimum Number of Changes to Make Binary String Beautiful](Problems/2914.cpp) | +| 2924 | Medium | [Find Champion II](Problems/2924.cpp) | | 2947 | Medium | [Count Beautiful Substrings I](Problems/2947.cpp) | | 2966 | Medium | [Divide Array Into Arrays With Max Difference](Problems/2966.cpp) | | 2997 | Medium | [Minimum Number of Operations to Make Array XOR Equal to K](Problems/2997.cpp) |