leetcode

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

commit d3307deda38a3ca297497ee9c8c37b73da21ad72
parent 8592c2f4edacb5e90959d7230415fdbe4f60c63e
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 15 Aug 2023 10:39:05 +0200

5 Random Problems

Diffstat:
AProblems/1551.cpp | 8++++++++
AProblems/2120.cpp | 29+++++++++++++++++++++++++++++
AProblems/2125.cpp | 18++++++++++++++++++
AProblems/2149.cpp | 11+++++++++++
AProblems/2657.cpp | 25+++++++++++++++++++++++++
MREADME.md | 5+++++
6 files changed, 96 insertions(+), 0 deletions(-)

diff --git a/Problems/1551.cpp b/Problems/1551.cpp @@ -0,0 +1,8 @@ +// 1551. Minimum Operations to Make Array Equal +class Solution { +public: + int minOperations(int n) { + int k = n / 2; + return n % 2 ? k * (k + 1) : k * k; + } +}; diff --git a/Problems/2120.cpp b/Problems/2120.cpp @@ -0,0 +1,29 @@ +// 2120. Execution of All Suffix Instructions Staying in a Grid +class Solution { +public: + vector<int> executeInstructions(int n, const vector<int> &startPos, + string s) { + const auto valid = [n](int x, int y) { + return x >= 0 && x < n && y >= 0 && y < n; + }; + + vector<int> res; + res.reserve(s.size()); + for (int k = 0; k < s.size(); k++) { + int x = startPos[0], y = startPos[1]; + for (int i = k; i < s.size(); i++) { + if (s[i] == 'L') y--; + if (s[i] == 'R') y++; + if (s[i] == 'U') x--; + if (s[i] == 'D') x++; + if (!valid(x, y)) { + res.push_back(i - k); + goto next; + } + } + res.push_back(s.size() - k); + next:; + } + return res; + } +}; diff --git a/Problems/2125.cpp b/Problems/2125.cpp @@ -0,0 +1,18 @@ +// 2125. Number of Laser Beams in a Bank +class Solution { +public: + int numberOfBeams(const vector<string> &bank) { + vector<int> count; + count.reserve(bank.size()); + for (const auto &floor : bank) { + int cnt = 0; + for (char c : floor) + if (c == '1') cnt++; + if (cnt) count.push_back(cnt); + } + + int res = 0; + for (int i = 1; i < count.size(); i++) res += count[i] * count[i - 1]; + return res; + } +}; diff --git a/Problems/2149.cpp b/Problems/2149.cpp @@ -0,0 +1,11 @@ +// 2149. Rearrange Array Elements by Sign +class Solution { +public: + vector<int> rearrangeArray(const vector<int> &nums) { + vector<int> res(nums.size()); + int i = -2, j = -1; + for (int num : nums) (num > 0 ? res[i += 2] : res[j += 2]) = num; + + return res; + } +}; diff --git a/Problems/2657.cpp b/Problems/2657.cpp @@ -0,0 +1,25 @@ +// 2657. Find the Prefix Common Array of Two Arrays +class Solution { +public: + vector<int> findThePrefixCommonArray(const vector<int> &A, + const vector<int> &B) { + vector<int> res(A.size()); + unordered_set<int> setA, setB; + for (int i = 0, count = 0; i < A.size(); i++) { + if (A[i] == B[i]) + count++; + else { + if (setB.count(A[i])) + count++; + else + setA.insert(A[i]); + if (setA.count(B[i])) + count++; + else + setB.insert(B[i]); + } + res[i] = count; + } + return res; + } +}; diff --git a/README.md b/README.md @@ -514,6 +514,7 @@ for solving problems. | 1539 | Easy | [Kth Missing Positive Number](Problems/1539.cpp) | | 1544 | Easy | [Make The String Great](Problems/1544.cpp) | | 1547 | Hard | [Minimum Cost to Cut a Stick](Problems/1547.cpp) | +| 1551 | Medium | [Minimum Operations to Make Array Equal](Problems/1551.cpp) | | 1557 | Medium | [Minimum Number of Vertices to Reach All Nodes](Problems/1557.cpp) | | 1567 | Medium | [Maximum Length of Subarray With Positive Product](Problems/1567.cpp) | | 1569 | Hard | [Number of Ways to Reorder Array to Get Same BST](Problems/1569.cpp) | @@ -570,10 +571,13 @@ for solving problems. | 2095 | Medium | [Delete the Middle Node of a Linked List](Problems/2095.cpp) | | 2101 | Medium | [Detonate the Maximum Bombs](Problems/2101.cpp) | | 2115 | Medium | [Find All Possible Recipes from Given Supplies](Problems/2115.cpp) | +| 2120 | Medium | [Execution of All Suffix Instructions Staying in a Grid](Problems/2120.cpp) | +| 2125 | Medium | [Number of Laser Beams in a Bank](Problems/2125.cpp) | | 2130 | Medium | [Maximum Twin Sum of a Linked List](Problems/2130.cpp) | | 2131 | Medium | [Longest Palindrome by Concatenating Two Letter Words](Problems/2131.cpp) | | 2140 | Medium | [Solving Questions With Brainpower](Problems/2140.cpp) | | 2141 | Hard | [Maximum Running Time of N Computers](Problems/2141.cpp) | +| 2149 | Medium | [Rearrange Array Elements by Sign](Problems/2149.cpp) | | 2161 | Medium | [Partition Array According to Given Pivot](Problems/2161.cpp) | | 2177 | Medium | [Find Three Consecutive Integers That Sum to a Given Number](Problems/2177.cpp) | | 2181 | Medium | [Merge Nodes in Between Zeros](Problems/2181.cpp) | @@ -640,6 +644,7 @@ for solving problems. | 2635 | Easy | [Apply Transform Over Each Element in Array](Problems/2635.js) | | 2636 | Medium | [Promise Pool ](Problems/2636.js) | | 2637 | Easy | [Promise Time Limit](Problems/2637.js) | +| 2657 | Medium | [Find the Prefix Common Array of Two Arrays](Problems/2657.cpp) | | 2665 | Easy | [Counter II](Problems/2665.js) | | 2666 | Easy | [Allow One Function Call](Problems/2666.js) | | 2667 | Easy | [Create Hello World Function](Problems/2667.js) |