leetcode

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

commit 8337f9f03bc076414a6b3830dfd7d605e99a32a2
parent 8dd5d7216a62bcb543aa738fcf627b82bc327cef
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri,  8 Sep 2023 15:19:21 +0200

5 Random Problems

Diffstat:
AProblems/1395.cpp | 15+++++++++++++++
AProblems/1807.cpp | 23+++++++++++++++++++++++
AProblems/1980.cpp | 15+++++++++++++++
AProblems/2232.cpp | 24++++++++++++++++++++++++
AProblems/2487.cpp | 24++++++++++++++++++++++++
MREADME.md | 5+++++
6 files changed, 106 insertions(+), 0 deletions(-)

diff --git a/Problems/1395.cpp b/Problems/1395.cpp @@ -0,0 +1,15 @@ +class Solution { + public: + int numTeams(const vector<int> &rating) { + int res = 0; + for (int i = 1; i < rating.size() - 1; i++) { + int less[2] = {}, greater[2] = {}; + for (auto j = 0; j < rating.size(); j++) { + if (rating[i] < rating[j]) less[j > i]++; + if (rating[i] > rating[j]) greater[j > i]++; + } + res += less[0] * greater[1] + greater[0] * less[1]; + } + return res; + } +}; diff --git a/Problems/1807.cpp b/Problems/1807.cpp @@ -0,0 +1,23 @@ +class Solution { + public: + string evaluate(const string &s, const vector<vector<string>> &knowledge) { + unordered_map<string, string> um; + for (const auto &p : knowledge) + um[p[0]] = p[1]; + + string res, key; + bool open = false; + for (const char c : s) { + if (c == '(') + open = true; + else if (c == ')') { + res += um.count(key) ? um[key] : "?"; + open = false; + key.clear(); + } else { + (open ? key : res) += c; + } + } + return res; + } +}; diff --git a/Problems/1980.cpp b/Problems/1980.cpp @@ -0,0 +1,15 @@ +class Solution { + public: + string findDifferentBinaryString(const vector<string> &nums) { + const unordered_set<string> us(begin(nums), end(nums)); + const int n = nums.size(); + + for (int mask = 0, crnt = 0; mask < (1 << n); crnt = ++mask) { + string s(n, '0'); + for (int i = n - 1; i >= 0; i--, crnt >>= 1) + if (crnt & 1) s[i]++; + if (!us.count(s)) return s; + } + return ""; + } +}; diff --git a/Problems/2232.cpp b/Problems/2232.cpp @@ -0,0 +1,24 @@ +class Solution { + public: + string minimizeResult(const string &expression) { + const int idx = expression.find('+'); + const string a = expression.substr(0, idx); + const string b = expression.substr(idx + 1); + + int mini = INT_MAX, mi, mj; + for (int i = 0; i < a.size(); i++) { + for (int j = 1; j <= b.size(); j++) { + int out = (i != 0 ? stoi(a.substr(0, i)) : 1) * (j != b.size() ? stoi(b.substr(j)) : 1); + int in = stoi(a.substr(i)) + stoi(b.substr(0, j)); + if (in * out < mini) { + mini = in * out; + mi = i; + mj = j; + } + } + } + + return a.substr(0, mi) + "(" + a.substr(mi) + "+" + b.substr(0, mj) + ")" + b.substr(mj); + ; + } +}; diff --git a/Problems/2487.cpp b/Problems/2487.cpp @@ -0,0 +1,24 @@ +class Solution { + ListNode *rev(ListNode *list) { + ListNode *prev = nullptr, *next; + while (list) { + next = list->next; + list->next = prev; + prev = list; + list = next; + } + return prev; + } + + public: + ListNode *removeNodes(ListNode *head) { + ListNode dummy(-1, rev(head)); + int maxi = -1; + for (ListNode *crnt = &dummy; crnt && crnt->next; crnt = crnt->next) { + maxi = max(maxi, crnt->val); + while (crnt->next && crnt->next->val < maxi) + crnt->next = crnt->next->next; + } + return rev(dummy.next); + } +}; diff --git a/README.md b/README.md @@ -538,6 +538,7 @@ for solving problems. | 1381 | Medium | [Design a Stack With Increment Operation](Problems/1381.cpp) | | 1382 | Medium | [Balance a Binary Search Tree](Problems/1382.cpp) | | 1387 | Medium | [Sort Integers by The Power Value](Problems/1387.cpp) | +| 1395 | Medium | [Count Number of Teams](Problems/1395.cpp) | | 1396 | Medium | [Design Underground System](Problems/1396.cpp) | | 1402 | Hard | [Reducing Dishes](Problems/1402.cpp) | | 1406 | Hard | [Stone Game III](Problems/1406.cpp) | @@ -621,6 +622,7 @@ for solving problems. | 1799 | Medium | [Maximize Score After N Operations](Problems/1799.cpp) | | 1802 | Medium | [Maximum Value at a Given Index in a Bounded Array](Problems/1802.cpp) | | 1806 | Medium | [Minimum Number of Operations to Reinitialize a Permutation](Problems/1806.cpp) | +| 1807 | Medium | [Evaluate the Bracket Pairs of a String](Problems/1807.cpp) | | 1817 | Medium | [Finding the Users Active Minutes](Problems/1817.cpp) | | 1822 | Easy | [Sign of the Product of an Array](Problems/1822.cpp) | | 1823 | Medium | [Find the Winner of the Circular Game](Problems/1823.cpp) | @@ -644,6 +646,7 @@ for solving problems. | 1970 | Hard | [Last Day Where You Can Still Cross](Problems/1970.cpp) | | 1971 | Easy | [Find if Path Exists in Graph](Problems/1971.cpp) | | 1976 | Medium | [Number of Ways to Arrive at Destination](Problems/1976.cpp) | +| 1980 | Medium | [Find Unique Binary String](Problems/1980.cpp) | | 1991 | Easy | [Find the Middle Index in Array](Problems/1991.cpp) | | 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) | @@ -675,6 +678,7 @@ for solving problems. | 2218 | Hard | [Maximum Value of K Coins From Piles](Problems/2218.cpp) | | 2221 | Medium | [Find Triangular Sum of an Array](Problems/2221.cpp) | | 2225 | Medium | [Find Players With Zero or One Losses](Problems/2225.cpp) | +| 2232 | Medium | [Minimize Result by Adding Parentheses to Expression](Problems/2232.cpp) | | 2235 | Easy | [Add Two Integers](Problems/2235.cpp) | | 2236 | Easy | [Root Equals Sum of Children](Problems/2236.cpp) | | 2243 | Easy | [Calculate Digit Sum of a String](Problems/2243.cpp) | @@ -726,6 +730,7 @@ for solving problems. | 2477 | Medium | [Minimum Fuel Cost to Report to the Capital](Problems/2477.cpp) | | 2482 | Medium | [Difference Between Ones and Zeros in Row and Column](Problems/2482.cpp) | | 2483 | Medium | [Minimum Penalty for a Shop](Problems/2483.cpp) | +| 2487 | Medium | [Remove Nodes From Linked List](Problems/2487.cpp) | | 2492 | Medium | [Minimum Score of a Path Between Two Cities](Problems/2492.cpp) | | 2497 | Medium | [Maximum Star Sum of a Graph](Problems/2497.cpp) | | 2527 | Medium | [Find Xor-Beauty of Array](Problems/2527.cpp) |