leetcode

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

commit a80f7987d0f3e280b8177175305088d7bcffc29e
parent d3307deda38a3ca297497ee9c8c37b73da21ad72
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 16 Aug 2023 22:33:19 +0200

5 Random Problems

Diffstat:
AProblems/1630.cpp | 22++++++++++++++++++++++
AProblems/1817.cpp | 23+++++++++++++++++++++++
AProblems/1877.cpp | 9+++++++++
AProblems/2079.cpp | 15+++++++++++++++
AProblems/2482.cpp | 20++++++++++++++++++++
MREADME.md | 5+++++
6 files changed, 94 insertions(+), 0 deletions(-)

diff --git a/Problems/1630.cpp b/Problems/1630.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + vector<bool> checkArithmeticSubarrays(const vector<int> &nums, + const vector<int> &l, + const vector<int> &r) { + int n = nums.size(), m = l.size(); + vector<bool> res(m, true); + for (int q = 0; q < m; q++) { + if (r[q] - l[q] < 2) continue; + vector<int> range(nums.begin() + l[q], nums.begin() + r[q] + 1); + sort(range.begin(), range.end()); + const int diff = range[1] - range[0]; + for (int i = 2; i < range.size(); i++) { + if (range[i] - range[i - 1] != diff) { + res[q] = false; + break; + } + } + } + return res; + } +}; diff --git a/Problems/1817.cpp b/Problems/1817.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector<int> findingUsersActiveMinutes(vector<vector<int>> &logs, int k) { + sort(logs.begin(), logs.end()); + + vector<int> res(k, 0); + int crnt = logs.front()[0], val = logs.front()[1], count = 1; + for (const auto &log : logs) { + if (log[0] == crnt) { + if (log[1] == val) continue; + val = log[1]; + count++; + } else { + if (count >= 1 && count <= k) res[count - 1]++; + crnt = log[0]; + val = log[1]; + count = 1; + } + } + if (count >= 1 && count <= k) res[count - 1]++; + return res; + } +}; diff --git a/Problems/1877.cpp b/Problems/1877.cpp @@ -0,0 +1,9 @@ +class Solution { +public: + int minPairSum(vector<int> &nums) { + sort(nums.begin(), nums.end()); + int res = INT_MIN, i = 0, j = nums.size() - 1; + while (i < j) res = max(res, nums[i++] + nums[j--]); + return res; + } +}; diff --git a/Problems/2079.cpp b/Problems/2079.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int wateringPlants(const vector<int> &plants, int capacity) { + int res = plants.size(), crnt = capacity; + for (int i = 0; i < plants.size(); i++) { + if (crnt >= plants[i]) + crnt -= plants[i]; + else { + crnt = capacity - plants[i]; + res += 2 * i; + } + } + return res; + } +}; diff --git a/Problems/2482.cpp b/Problems/2482.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector<vector<int>> onesMinusZeros(vector<vector<int>> &grid) { + int n = grid.size(), m = grid[0].size(); + vector<int> col(n, 0), row(m, 0); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + col[i] += grid[i][j]; + row[j] += grid[i][j]; + } + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + grid[i][j] = 2 * (col[i] + row[j]) - (m + n); + } + } + + return grid; + } +}; diff --git a/README.md b/README.md @@ -527,6 +527,7 @@ for solving problems. | 1609 | Medium | [Even Odd Tree](Problems/1609.cpp) | | 1615 | Medium | [Maximal Network Rank](Problems/1615.cpp) | | 1626 | Medium | [Best Team With No Conflicts](Problems/1626.cpp) | +| 1630 | Medium | [Arithmetic Subarrays](Problems/1630.cpp) | | 1637 | Medium | [Widest Vertical Area Between Two Points Containing No Points](Problems/1637.cpp) | | 1639 | Hard | [Number of Ways to Form a Target String Given a Dictionary](Problems/1639.cpp) | | 1646 | Easy | [Get Maximum in Generated Array](Problems/1646.cpp) | @@ -549,6 +550,7 @@ for solving problems. | 1791 | Easy | [Find Center of Star Graph](Problems/1791.cpp) | | 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) | +| 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) | | 1828 | Medium | [Queries on Number of Points Inside a Circle](Problems/1828.cpp) | @@ -556,6 +558,7 @@ for solving problems. | 1834 | Medium | [Single-Threaded CPU](Problems/1834.cpp) | | 1857 | Hard | [Largest Color Value in a Directed Graph](Problems/1857.cpp) | | 1870 | Medium | [Minimum Speed to Arrive on Time](Problems/1870.cpp) | +| 1877 | Medium | [Minimize Maximum Pair Sum in Array](Problems/1877.cpp) | | 1926 | Medium | [Nearest Exit from Entrance in Maze](Problems/1926.cpp) | | 1962 | Medium | [Remove Stones to Minimize the Total](Problems/1962.cpp) | | 1964 | Hard | [Find the Longest Valid Obstacle Course at Each Position](Problems/1964.cpp) | @@ -566,6 +569,7 @@ for solving problems. | 2024 | Medium | [Maximize the Confusion of an Exam](Problems/2024.cpp) | | 2039 | Medium | [The Time When the Network Becomes Idle](Problems/2039.cpp) | | 2073 | Easy | [Time Needed to Buy Tickets](Problems/2073.cpp) | +| 2079 | Medium | [Watering Plants](Problems/2079.cpp) | | 2085 | Easy | [Count Common Words With One Occurrence](Problems/2085.cpp) | | 2090 | Medium | [K Radius Subarray Averages](Problems/2090.cpp) | | 2095 | Medium | [Delete the Middle Node of a Linked List](Problems/2095.cpp) | @@ -625,6 +629,7 @@ for solving problems. | 2466 | Medium | [Count Ways To Build Good Strings](Problems/2466.cpp) | | 2467 | Medium | [Most Profitable Path in a Tree](Problems/2467.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) | | 2492 | Medium | [Minimum Score of a Path Between Two Cities](Problems/2492.cpp) | | 2497 | Medium | [Maximum Star Sum of a Graph](Problems/2497.cpp) | | 2542 | Medium | [Maximum Subsequence Score](Problems/2542.cpp) |