leetcode

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

commit ce808bbaf2422358a38f0849274346a4071cb777
parent 694f0d79713b44a71674bb2a7f1c59fa474cf6dc
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Mon, 11 Mar 2024 18:41:41 +0000

1 Random Problem

Diffstat:
AProblems/2033.cpp | 25+++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/Problems/2033.cpp b/Problems/2033.cpp @@ -0,0 +1,25 @@ +class Solution { + public: + int minOperations(const vector<vector<int>> &grid, int x) const { + static int dp[100001]; + vector<int> vec; + + for (const auto &row : grid) + vec.insert(end(vec), begin(row), end(row)); + sort(begin(vec), end(vec)); + + for (int i = 0, acc = 0; i < size(vec) - 1; i++) { + const int crnt = vec[i + 1] - vec[i]; + if (crnt % x) return -1; + dp[i + 1] = acc += (i + 1) * crnt; + } + + int res = INT_MAX; + for (int i = size(vec) - 1, acc = 0; i > 0; i--) { + acc += (size(vec) - i) * (vec[i] - vec[i - 1]); + res = min(res, acc + dp[i - 1]); + } + + return res != INT_MAX ? res / x : 0; + } +}; diff --git a/README.md b/README.md @@ -965,6 +965,7 @@ for solving problems. | 2009 | Hard | [Minimum Number of Operations to Make Array Continuous](Problems/2009.cpp) | | 2023 | Medium | [Number of Pairs of Strings With Concatenation Equal to Target](Problems/2023.cpp) | | 2024 | Medium | [Maximize the Confusion of an Exam](Problems/2024.cpp) | +| 2033 | Medium | [Minimum Operations to Make a Uni-Value Grid](Problems/2033.cpp) | | 2038 | Medium | [Remove Colored Pieces if Both Neighbors are the Same Color](Problems/2038.cpp) | | 2039 | Medium | [The Time When the Network Becomes Idle](Problems/2039.cpp) | | 2043 | Medium | [Simple Bank System](Problems/2043.cpp) |