Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c381642

Browse files
committedApr 5, 2025
Add solution #1187
1 parent 129b38a commit c381642

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,172 LeetCode solutions in JavaScript
1+
# 1,173 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -920,6 +920,7 @@
920920
1184|[Distance Between Bus Stops](./solutions/1184-distance-between-bus-stops.js)|Easy|
921921
1185|[Day of the Week](./solutions/1185-day-of-the-week.js)|Easy|
922922
1186|[Maximum Subarray Sum with One Deletion](./solutions/1186-maximum-subarray-sum-with-one-deletion.js)|Medium|
923+
1187|[Make Array Strictly Increasing](./solutions/1187-make-array-strictly-increasing.js)|Hard|
923924
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
924925
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
925926
1206|[Design Skiplist](./solutions/1206-design-skiplist.js)|Hard|
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* 1187. Make Array Strictly Increasing
3+
* https://leetcode.com/problems/make-array-strictly-increasing/
4+
* Difficulty: Hard
5+
*
6+
* Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero)
7+
* needed to make arr1 strictly increasing.
8+
*
9+
* In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and
10+
* do the assignment arr1[i] = arr2[j].
11+
*
12+
* If there is no way to make arr1 strictly increasing, return -1.
13+
*/
14+
15+
/**
16+
* @param {number[]} arr1
17+
* @param {number[]} arr2
18+
* @return {number}
19+
*/
20+
var makeArrayIncreasing = function(arr1, arr2) {
21+
arr2 = [...new Set(arr2)].sort((a, b) => a - b);
22+
let dp = new Map([[-Infinity, 0]]);
23+
24+
for (let i = 0; i < arr1.length; i++) {
25+
const nextDp = new Map();
26+
let minSteps = Infinity;
27+
28+
for (const [prev, steps] of dp) {
29+
if (steps >= minSteps) continue;
30+
31+
if (arr1[i] > prev) {
32+
updateMin(nextDp, arr1[i], steps);
33+
minSteps = Math.min(minSteps, steps);
34+
}
35+
36+
const start = binarySearch(arr2, prev);
37+
for (let j = start; j < arr2.length; j++) {
38+
if (arr2[j] <= prev) continue;
39+
updateMin(nextDp, arr2[j], steps + 1);
40+
minSteps = Math.min(minSteps, steps + 1);
41+
if (arr2[j] >= arr1[i]) break;
42+
}
43+
}
44+
45+
if (nextDp.size === 0) return -1;
46+
dp = pruneStates(nextDp);
47+
}
48+
49+
return Math.min(...dp.values());
50+
};
51+
52+
function updateMin(map, key, value) {
53+
map.set(key, Math.min(value, map.get(key) ?? Infinity));
54+
}
55+
56+
function binarySearch(arr, target) {
57+
let left = 0;
58+
let right = arr.length;
59+
while (left < right) {
60+
const mid = Math.floor((left + right) / 2);
61+
if (arr[mid] <= target) left = mid + 1;
62+
else right = mid;
63+
}
64+
return left;
65+
}
66+
67+
function pruneStates(input) {
68+
const map = new Map();
69+
let minSteps = Infinity;
70+
for (const [val, steps] of [...input].sort((a, b) => a[0] - b[0])) {
71+
if (steps < minSteps) {
72+
map.set(val, steps);
73+
minSteps = steps;
74+
}
75+
}
76+
return map;
77+
}

0 commit comments

Comments
 (0)
Please sign in to comment.