leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE | |
commit | 1c1e890065425bb80206a0e5615e79ec4772e04d |
parent | 728ba868cb3c59350689f3a9c5b48b62de44c1e2 |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Wed, 30 Aug 2023 18:19:38 +0200 |
Daily Problem and 5 Random Problems
Diffstat:A | Problems/0537.cpp | | | ++++++++++ |
A | Problems/0889.cpp | | | ++++++++++++++ |
A | Problems/0893.cpp | | | ++++++++++++++++++ |
A | Problems/1850.cpp | | | ++++++++++++++++++ |
A | Problems/2225.cpp | | | ++++++++++++++++++++ |
A | Problems/2366.cpp | | | +++++++++++++ |
M | README.md | | | ++++++ |
7 files changed, 99 insertions(+), 0 deletions(-)
diff --git a/Problems/0537.cpp b/Problems/0537.cpp
@@ -0,0 +1,10 @@
class Solution {
public:
string complexNumberMultiply(const string num1, const string num2) {
int a, b, c, d;
char t;
stringstream(num1) >> a >> t >> b;
stringstream(num2) >> c >> t >> d;
return to_string(a * c - b * d) + "+" + to_string(a * d + b * c) + "i";
}
};
diff --git a/Problems/0889.cpp b/Problems/0889.cpp
@@ -0,0 +1,14 @@
class Solution {
public:
TreeNode *constructFromPrePost(const vector<int> &pre, const vector<int> &post) {
vector<TreeNode *> s = {new TreeNode(pre[0])};
for (int i = 1, j = 0; i < pre.size(); i++) {
TreeNode *node = new TreeNode(pre[i]);
while (s.back()->val == post[j])
s.pop_back(), j++;
(!s.back()->left ? s.back()->left : s.back()->right) = node;
s.push_back(node);
}
return s.front();
}
};
diff --git a/Problems/0893.cpp b/Problems/0893.cpp
@@ -0,0 +1,18 @@
class Solution {
public:
int numSpecialEquivGroups(const vector<string> &words) {
unordered_set<string> st;
const int n = words[0].size();
for (const auto &word : words) {
string s1, s2;
for (int i = 0; i < n; i += 2)
s1 += word[i];
for (int i = 1; i < n; i += 2)
s2 += word[i];
sort(begin(s1), end(s1));
sort(begin(s2), end(s2));
st.insert(s1 + ' ' + s2);
}
return st.size();
}
};
diff --git a/Problems/1850.cpp b/Problems/1850.cpp
@@ -0,0 +1,18 @@
class Solution {
public:
int getMinSwaps(string &num, int k) {
string perm = num;
while (k--)
next_permutation(perm.begin(), perm.end());
int res = 0;
for (int i = 0, j = 0; i < num.size(); j = ++i) {
while (num[j++] != perm[i])
;
res += j - i - 1;
while (i < --j)
swap(num[j], num[j - 1]);
}
return res;
}
};
diff --git a/Problems/2225.cpp b/Problems/2225.cpp
@@ -0,0 +1,20 @@
class Solution {
public:
vector<vector<int>> findWinners(const vector<vector<int>> &matches) {
static int count[100001];
vector<vector<int>> res(2);
memset(count, 0xFF, sizeof(count));
for (const auto &match : matches) {
if (count[match[0]] == -1) count[match[0]] = 0;
if (count[match[1]] == -1) count[match[1]] = 0;
count[match[1]]++;
}
for (int player = 1; player <= 100000; player++) {
if (count[player] == 0) res[0].push_back(player);
if (count[player] == 1) res[1].push_back(player);
}
return res;
}
};
diff --git a/Problems/2366.cpp b/Problems/2366.cpp
@@ -0,0 +1,13 @@
class Solution {
public:
long long minimumReplacement(vector<int> &nums) {
long long int res = 0;
for (int i = nums.size() - 2; i >= 0; i--) {
if (nums[i] <= nums[i + 1]) continue;
long long num = (nums[i] + nums[i + 1] - 1) / nums[i + 1];
res += num - 1;
nums[i] = nums[i] / num;
}
return res;
}
};
diff --git a/README.md b/README.md
@@ -311,6 +311,7 @@ for solving problems.
| 0530 | Easy | [Minimum Absolute Difference in BST](Problems/0530.cpp) |
| 0535 | Medium | [Encode and Decode TinyURL](Problems/0532.cpp) |
| 0535 | Medium | [K-diff Pairs in an Array](Problems/0532.cpp) |
| 0537 | Medium | [Complex Number Multiplication](Problems/0537.cpp) |
| 0538 | Medium | [Convert BST to Greater Tree](Problems/0538.cpp) |
| 0540 | Medium | [Single Element in a Sorted Array](Problems/0540.cpp) |
| 0542 | Medium | [01 Matrix](Problems/0542.cpp) |
@@ -400,7 +401,9 @@ for solving problems.
| 0884 | Easy | [Uncommon Words from Two Sentences](Problems/0884.cpp) |
| 0885 | Medium | [Spiral Matrix III](Problems/0885.cpp) |
| 0886 | Medium | [Possible Bipartition](Problems/0886.cpp) |
| 0889 | Medium | [Construct Binary Tree from Preorder and Postorder Traversal](Problems/0889.cpp) |
| 0890 | Medium | [Find and Replace Pattern](Problems/0890.cpp) |
| 0893 | Medium | [Groups of Special-Equivalent Strings](Problems/0893.cpp) |
| 0894 | Medium | [All Possible Full Binary Trees](Problems/0894.cpp) |
| 0897 | Easy | [Increasing Order Search Tree](Problems/0897.cpp) |
| 0901 | Medium | [Online Stock Span](Problems/0901.cpp) |
@@ -595,6 +598,7 @@ for solving problems.
| 1829 | Medium | [Maximum XOR for Each Query](Problems/1829.cpp) |
| 1833 | Medium | [Maximum Ice Cream Bars](Problems/1833.cpp) |
| 1834 | Medium | [Single-Threaded CPU](Problems/1834.cpp) |
| 1850 | Medium | [Minimum Adjacent Swaps to Reach the Kth Smallest Number](Problems/1850.cpp) |
| 1857 | Hard | [Largest Color Value in a Directed Graph](Problems/1857.cpp) |
| 1860 | Medium | [Incremental Memory Leak](Problems/1860.cpp) |
| 1870 | Medium | [Minimum Speed to Arrive on Time](Problems/1870.cpp) |
@@ -636,6 +640,7 @@ for solving problems.
| 2215 | Easy | [Find the Difference of Two Arrays](Problems/2215.cpp) |
| 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) |
| 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) |
@@ -661,6 +666,7 @@ for solving problems.
| 2352 | Medium | [Equal Row and Column Pairs](Problems/2352.cpp) |
| 2359 | Medium | [Find Closest Node to Given Two Nodes](Problems/2359.cpp) |
| 2360 | Hard | [Longest Cycle in a Graph](Problems/2360.cpp) |
| 2366 | Hard | [Minimum Replacements to Sort the Array](Problems/2366.cpp) |
| 2368 | Medium | [Reachable Nodes With Restrictions](Problems/2368.cpp) |
| 2369 | Medium | [Check if There is a Valid Partition For The Array](Problems/2369.cpp) |
| 2374 | Medium | [Node With Highest Edge Score](Problems/2374.cpp) |