leetcode

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

commit 26ce085c68cca11fcf52797756a1e2bbf48a98b6
parent 7f63e95a33b12ce9cd900c0f75c2bd422db067f6
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Mon, 25 Sep 2023 16:03:02 +0000

Daily Problem and 5 Random Problems

Diffstat:
AProblems/0378.cpp | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AProblems/0389.cpp | 11+++++++++++
AProblems/0858.cpp | 9+++++++++
AProblems/0869.cpp | 20++++++++++++++++++++
AProblems/1238.cpp | 28++++++++++++++++++++++++++++
AProblems/2471.cpp | 25+++++++++++++++++++++++++
MREADME.md | 6++++++
7 files changed, 163 insertions(+), 0 deletions(-)

diff --git a/Problems/0378.cpp b/Problems/0378.cpp @@ -0,0 +1,64 @@ +class Solution { + public: + int kthSmallest(vector<vector<int>> &matrix, int k) { + const int n = matrix.size(); + priority_queue<int> heap; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + heap.push(matrix[i][j]); + if (heap.size() > k) heap.pop(); + } + } + return heap.top(); + } +}; + +class Solution { + typedef tuple<int, uint8_t, uint8_t> tp; + + public: + int kthSmallest(vector<vector<int>> &matrix, int k) { + const int n = matrix.size(); + priority_queue<tp, vector<tp>, greater<tp>> heap; + for (int i = 0; i < min(n, k); i++) + heap.push({matrix[i][0], i, 0}); + + for (int i = 1; i < k; i++) { + const auto [elem, row, col] = heap.top(); + heap.pop(); + if (col + 1 < n) heap.push({matrix[row][col + 1], row, col + 1}); + } + return get<0>(heap.top()); + } +}; + +class Solution { + int count(const vector<vector<int>> &matrix, int mid) { + const int n = matrix.size(); + int i = n - 1, j = 0, count = 0; + while (i >= 0 && j < n) { + if (matrix[i][j] > mid) + i--; + else { + count += i + 1; + j++; + } + } + return count; + } + + public: + int kthSmallest(vector<vector<int>> &matrix, int k) { + const int n = matrix.size(); + int low = matrix.front().front(), high = matrix.back().back(); + while (low <= high) { + const int mid = low + (high - low) / 2; + const int ans = count(matrix, mid); + if (ans < k) + low = mid + 1; + else + high = mid - 1; + } + return low; + } +}; diff --git a/Problems/0389.cpp b/Problems/0389.cpp @@ -0,0 +1,11 @@ +class Solution { + public: + char findTheDifference(const string &s, const string &t) { + int sum = 0; + for (const char c : t) + sum += c; + for (const char c : s) + sum -= c; + return sum; + } +}; diff --git a/Problems/0858.cpp b/Problems/0858.cpp @@ -0,0 +1,9 @@ +class Solution { + public: + int mirrorReflection(int p, int q) { + while (p % 2 == 0 && q % 2 == 0) + p >>= 1, q >>= 1; + if (p & 1) return q & 1; + return 2; + } +}; diff --git a/Problems/0869.cpp b/Problems/0869.cpp @@ -0,0 +1,20 @@ +class Solution { + static const unordered_set<string> cache; + + public: + bool reorderedPowerOf2(int n) { + string digits; + do { + digits.push_back('0' + n % 10); + } while ((n /= 10) > 0); + + sort(begin(digits), end(digits)); + return cache.count(digits); + } +}; + +const unordered_set<string> Solution::cache = { + "1", "2", "4", "8", "16", "23", "46", "128", + "256", "125", "0124", "0248", "0469", "1289", "13468", "23678", + "35566", "011237", "122446", "224588", "0145678", "0122579", "0134449", "0368888", + "11266777", "23334455", "01466788", "112234778", "234455668", "012356789"}; diff --git a/Problems/1238.cpp b/Problems/1238.cpp @@ -0,0 +1,28 @@ +class Solution { + public: + vector<int> circularPermutation(int n, int start) { + vector<int> res = {0, 1}; + int idx = 2; + for (int i = 1; i < n; i++) { + const int size = 1 << i; + for (int j = 1; j <= size; j++) { + res.push_back(size | res[size - j]); + if (res.back() == start) idx = res.size(); + } + } + if (start == 0) return res; + reverse(begin(res), begin(res) + idx); + reverse(begin(res) + idx, end(res)); + return res; + } +}; + +class Solution { + public: + vector<int> circularPermutation(int n, int start) { + vector<int> res; + for (int i = 0; i < (1 << n); i++) + res.push_back(start ^ i ^ (i >> 1)); + return res; + } +}; diff --git a/Problems/2471.cpp b/Problems/2471.cpp @@ -0,0 +1,25 @@ +class Solution { + public: + int minimumOperations(TreeNode *root) { + queue<TreeNode *> q; + q.push(root); + int res = 0; + while (!q.empty()) { + vector<int> lvl, idx(q.size()); + for (int k = q.size(); k > 0; k--) { + TreeNode *root = q.front(); + q.pop(); + lvl.push_back(root->val); + if (root->left) q.push(root->left); + if (root->right) q.push(root->right); + } + iota(begin(idx), end(idx), 0); + sort(begin(idx), end(idx), [&](const int i, const int j) { return lvl[i] < lvl[j]; }); + for (int i = 0; i < idx.size(); i++) { + for (; idx[i] != i; res++) + swap(idx[i], idx[idx[i]]); + } + } + return res; + } +}; diff --git a/README.md b/README.md @@ -264,11 +264,13 @@ for solving problems. | 0374 | Easy | [Guess Number Higher or Lower](Problems/0374.cpp) | | 0376 | Medium | [Wiggle Subsequence](Problems/0376.cpp) | | 0377 | Medium | [Combination Sum IV](Problems/0377.cpp) | +| 0378 | Medium | [Kth Smallest Element in a Sorted Matrix](Problems/0378.cpp) | | 0382 | Medium | [Linked List Random Node](Problems/0382.cpp) | | 0383 | Easy | [Ransom Note](Problems/0383.cpp) | | 0384 | Medium | [Shuffle an Array](Problems/0384.cpp) | | 0386 | Medium | [Lexicographical Numbers](Problems/0386.cpp) | | 0387 | Easy | [First Unique Character in a String](Problems/0387.cpp) | +| 0389 | Easy | [Find the Difference](Problems/0389.cpp) | | 0392 | Easy | [Is Subsequence](Problems/0392.cpp) | | 0394 | Medium | [Decode String](Problems/0394.cpp) | | 0398 | Medium | [Random Pick Index](Problems/0398.cpp) | @@ -414,11 +416,13 @@ for solving problems. | 0852 | Medium | [Peak Index in a Mountain Array](Problems/0852.cpp) | | 0853 | Medium | [Car Fleet](Problems/0853.cpp) | | 0856 | Medium | [Score of Parentheses](Problems/0856.cpp) | +| 0858 | Medium | [Mirror Reflection](Problems/0858.cpp) | | 0859 | Easy | [Buddy Strings](Problems/0859.cpp) | | 0861 | Medium | [Score After Flipping Matrix](Problems/0861.cpp) | | 0863 | Medium | [All Nodes Distance K in Binary Tree](Problems/0863.cpp) | | 0864 | Hard | [Shortest Path to Get All Keys](Problems/0864.cpp) | | 0865 | Medium | [Smallest Subtree with all the Deepest Nodes](Problems/0865.cpp) | +| 0869 | Medium | [Reordered Power of 2](Problems/0869.cpp) | | 0872 | Easy | [Leaf-Similar Trees](Problems/0872.cpp) | | 0875 | Medium | [Koko Eating Bananas](Problems/0875.cpp) | | 0876 | Easy | [Middle of the Linked List](Problems/0876.cpp) | @@ -529,6 +533,7 @@ for solving problems. | 1232 | Easy | [Check If It Is a Straight Line](Problems/1232.cpp) | | 1233 | Medium | [Remove Sub-Folders from the Filesystem](Problems/1233.cpp) | | 1237 | Medium | [Find Positive Integer Solution for a Given Equation](Problems/1237.cpp) | +| 1238 | Medium | [Circular Permutation in Binary Representation](Problems/1238.cpp) | | 1247 | Medium | [Minimum Swaps to Make Strings Equal](Problems/1247.cpp) | | 1248 | Medium | [Count Number of Nice Subarrays](Problems/1248.cpp) | | 1249 | Medium | [Minimum Remove to Make Valid Parentheses](Problems/1249.cpp) | @@ -794,6 +799,7 @@ for solving problems. | 2465 | Easy | [Number of Distinct Averages](Problems/2465.cpp) | | 2466 | Medium | [Count Ways To Build Good Strings](Problems/2466.cpp) | | 2467 | Medium | [Most Profitable Path in a Tree](Problems/2467.cpp) | +| 2471 | Medium | [Minimum Number of Operations to Sort a Binary Tree by Level](Problems/2471.cpp) | | 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) |