leetcode

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

commit 8615f659314ffba580cc6f0a275548e5fad50cbe
parent 7164935335ae81c4e3a6786178b73f22fa2e997a
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Mon,  2 Oct 2023 23:44:44 +0000

Improved Daily Problem and 1 Random Problems

Diffstat:
AProblems/1583.cpp | 34++++++++++++++++++++++++++++++++++
AProblems/2038.cpp | 19+++++++++++++++++++
MREADME.md | 2++
3 files changed, 55 insertions(+), 0 deletions(-)

diff --git a/Problems/1583.cpp b/Problems/1583.cpp @@ -0,0 +1,34 @@ +class Solution { + static int position[501][501]; + bitset<500> unhappy; + + void is_unhappy(const int x, const int y, const int u, const int v) { + if (position[x][u] < position[x][y] && position[u][x] < position[u][v]) { + unhappy.set(x), unhappy.set(u); + } + } + + public: + int unhappyFriends(const int n, const vector<vector<int>> &preferences, + const vector<vector<int>> &pairs) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - 1; j++) { + position[i][preferences[i][j]] = j; + } + } + + for (int i = 0; i < n / 2; i++) { + const int x = pairs[i][0], y = pairs[i][1]; + for (int j = i + 1; j < n / 2; j++) { + const int u = pairs[j][0], v = pairs[j][1]; + is_unhappy(x, y, u, v); + is_unhappy(x, y, v, u); + is_unhappy(y, x, u, v); + is_unhappy(y, x, v, u); + } + } + return unhappy.count(); + } +}; + +int Solution::position[501][501]; diff --git a/Problems/2038.cpp b/Problems/2038.cpp @@ -0,0 +1,19 @@ +class Solution { + public: + bool winnerOfGame(const string &colors) { + int a = 0, b = 0, i = 0; + while (i < colors.size()) { + int count = 0; + if (colors[i] == 'A') { + while (i < colors.size() && colors[i] == 'A') + i++, count++; + if (count > 2) a += count - 2; + } else { + while (i < colors.size() && colors[i] == 'B') + i++, count++; + if (count > 2) b += count - 2; + } + } + return a > b; + } +}; diff --git a/README.md b/README.md @@ -649,6 +649,7 @@ for solving problems. | 1575 | Medium | [Count All Possible Routes](Problems/1575.cpp) | | 1578 | Medium | [Minimum Time to Make Rope Colorful](Problems/1578.cpp) | | 1579 | Hard | [Remove Max Number of Edges to Keep Graph Fully Traversable](Problems/1579.cpp) | +| 1583 | Medium | [Count Unhappy Friends](Problems/1583.cpp) | | 1584 | Medium | [Min Cost to Connect All Points](Problems/1584.cpp) | | 1600 | Medium | [Throne Inheritance](Problems/1600.cpp) | | 1601 | Hard | [Maximum Number of Achievable Transfer Requests](Problems/1601.cpp) | @@ -733,6 +734,7 @@ for solving problems. | 1992 | Medium | [Find All Groups of Farmland](Problems/1992.cpp) | | 2023 | Medium | [Number of Pairs of Strings With Concatenation Equal to Target](Problems/2023.cpp) | | 2024 | Medium | [Maximize the Confusion of an Exam](Problems/2024.cpp) | +| 2038 | Medium | [Remove Colored Pieces if Both Neighbors are the Same Color](Problems/2038.cpp) | | 2039 | Medium | [The Time When the Network Becomes Idle](Problems/2039.cpp) | | 2043 | Medium | [Simple Bank System](Problems/2043.cpp) | | 2044 | Medium | [Count Number of Maximum Bitwise-OR Subsets](Problems/2044.cpp) |