Skip to content

Commit 37f6b1e

Browse files
authored
Update make-array-strictly-increasing.cpp
1 parent e67b347 commit 37f6b1e

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

C++/make-array-strictly-increasing.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ class Solution {
55
public:
66
int makeArrayIncreasing(vector<int>& arr1, vector<int>& arr2) {
77
std::sort(arr2.begin(), arr2.end());
8-
arr2.erase(std::unique(arr2.begin(), arr2.end()), arr2.end());
9-
unordered_map<int, int> dp = {{0, arr1[0]}, {1, arr2[0]}}; // dp[min_cost] = end_with_val
10-
for (int i = 1; i < arr1.size(); ++i) {
8+
unordered_map<int, int> dp = {{0, -1}}; // dp[min_cost] = end_with_val
9+
for (const auto& val1 : arr1) {
1110
unordered_map<int, int> next_dp;
1211
for (const auto& [cost, val] : dp) {
13-
if (val < arr1[i]) {
12+
if (val < val1) {
1413
if (!next_dp.count(cost)) {
15-
next_dp[cost] = arr1[i];
14+
next_dp[cost] = val1;
1615
} else {
17-
next_dp[cost] = min(next_dp[cost], arr1[i]);
16+
next_dp[cost] = min(next_dp[cost], val1);
1817
}
1918
}
2019
const auto& it = upper_bound(arr2.cbegin(), arr2.cend(), val);

0 commit comments

Comments
 (0)