leetcode

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

commit e07bdc47dda3dd941caa6dd0c302dc6d9c0b3da0
parent a80f7987d0f3e280b8177175305088d7bcffc29e
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu, 17 Aug 2023 22:08:25 +0200

5 Random Problems

Diffstat:
AProblems/1347.cpp | 14++++++++++++++
AProblems/1561.cpp | 13+++++++++++++
AProblems/2221.cpp | 11+++++++++++
AProblems/2317.cpp | 8++++++++
AProblems/2442.cpp | 14++++++++++++++
MREADME.md | 5+++++
6 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/Problems/1347.cpp b/Problems/1347.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int minSteps(const string &s, const string &t) { + int res = 0, count[27] = {0}; + for (int i = 0; i < s.size(); i++) { + count[s[i] & 0x1F]++; + count[t[i] & 0x1F]--; + } + for (int i = 1; i < 27; i++) { + if (count[i] > 0) res += count[i]; + } + return res; + } +}; diff --git a/Problems/1561.cpp b/Problems/1561.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int maxCoins(vector<int> &piles) { + sort(piles.begin(), piles.end()); + + int res = 0; + for (int i = piles.size() / 3; i < piles.size(); i += 2) { + res += piles[i]; + } + + return res; + } +}; diff --git a/Problems/2221.cpp b/Problems/2221.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int triangularSum(vector<int> &nums) { + for (int k = 0; k < nums.size() - 1; k++) { + for (int i = 1; i < nums.size() - k; i++) { + nums[i - 1] = (nums[i - 1] + nums[i]) % 10; + } + } + return nums.front(); + } +}; diff --git a/Problems/2317.cpp b/Problems/2317.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + int maximumXOR(vector<int> &nums) { + int res = 0; + for (const int n : nums) res |= n; + return res; + } +}; diff --git a/Problems/2442.cpp b/Problems/2442.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int countDistinctIntegers(const vector<int> &nums) { + unordered_set<int> us(nums.begin(), nums.end()); + for (int n : nums) { + int rev = 0; + while (n % 10 == 0) n /= 10; + do rev = (rev * 10) + n % 10; + while ((n /= 10) > 0); + us.insert(rev); + } + return us.size(); + } +}; diff --git a/README.md b/README.md @@ -477,6 +477,7 @@ for solving problems. | 1342 | Easy | [Number of Steps to Reduce a Number to Zero](Problems/1342.cpp) | | 1345 | Hard | [Jump Game IV](Problems/1345.cpp) | | 1346 | Easy | [Check if N and Its Double Exist](Problems/1346.cpp) | +| 1347 | Medium | [Minimum Number of Steps to Make Two Strings Anagram](Problems/1347.cpp) | | 1351 | Easy | [Count Negative Numbers in a Sorted Matrix](Problems/1351.cpp) | | 1361 | Medium | [Validate Binary Tree Nodes](Problems/1361.cpp) | | 1367 | Medium | [Linked List in Binary Tree ](Problems/1367.cpp) | @@ -516,6 +517,7 @@ for solving problems. | 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) | +| 1561 | Medium | [Maximum Number of Coins You Can Get](Problems/1561.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) | | 1572 | Easy | [Matrix Diagonal Sum](Problems/1572.cpp) | @@ -589,6 +591,7 @@ for solving problems. | 2192 | Medium | [All Ancestors of a Node in a Directed Acyclic Graph](Problems/2192.cpp) | | 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) | | 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) | @@ -602,6 +605,7 @@ for solving problems. | 2305 | Medium | [Fair Distribution of Cookies](Problems/2305.cpp) | | 2306 | Hard | [Naming a Company](Problems/2306.cpp) | | 2316 | Medium | [Count Unreachable Pairs of Nodes in an Undirected Graph](Problems/2316.cpp) | +| 2317 | Medium | [Maximum XOR After Operations](Problems/2317.cpp) | | 2326 | Medium | [Spiral Matrix IV](Problems/2326.cpp) | | 2328 | Hard | [Number of Increasing Paths in a Grid](Problems/2328.cpp) | | 2331 | Easy | [Evaluate Boolean Binary Tree](Problems/2331.cpp) | @@ -621,6 +625,7 @@ for solving problems. | 2421 | Medium | [Number of Good Paths](Problems/2421.cpp) | | 2433 | Medium | [Find The Original Array of Prefix Xor](Problems/2433.cpp) | | 2439 | Medium | [Minimize Maximum of Array](Problems/2439.cpp) | +| 2442 | Medium | [Count Number of Distinct Integers After Reverse Operations](Problems/2442.cpp) | | 2444 | Hard | [Count Subarrays With Fixed Bounds](Problems/2444.cpp) | | 2448 | Hard | [Minimum Cost to Make Array Equal](Problems/2448.cpp) | | 2461 | Medium | [Maximum Sum of Distinct Subarrays With Length K](Problems/2461.cpp) |