Skip to content

Commit d013ca2

Browse files
committed
greedy jump games 41, 45
1 parent 7499c58 commit d013ca2

File tree

5 files changed

+114
-2
lines changed

5 files changed

+114
-2
lines changed

Hard/41-firstMissingPositive.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Solution: keep swapping elements until the element is in the right position.
3+
* e.g. [3,1,2] --> [2,1,3] -> [1,2,3], now 1 is in the right position.
4+
* special cases: the element is <=0, or is bigger the length of the array or
5+
* there is a duplicate in the position at nums[i] - 1 stop swapping, otherewise,
6+
* it will be an infinite loop.
7+
* if every element is in the right position, then just simpily return the last element + 1
8+
*
9+
* @param {number[]} nums
10+
* @return {number}
11+
*/
12+
var firstMissingPositive = function(nums) {
13+
if (nums.length <= 0) return 1;
14+
for (var i = 0; i < nums.length; i++) {
15+
while (nums[i] !== i + 1) {
16+
if (nums[i] <= 0) break;
17+
if (nums[i] > nums.length - 1 || nums[nums[i] - 1] === nums[i]) break;
18+
swap(nums, nums[i] - 1, i);
19+
}
20+
}
21+
for (var j = 0; j < nums.length; j++) {
22+
if (nums[j] !== j + 1) {
23+
return j + 1;
24+
}
25+
}
26+
// if every element is in the right position, then just simpily return the last element + 1
27+
return nums[nums.length - 1] + 1;
28+
};
29+
30+
var swap = function(nums, i, j) {
31+
var tmp = nums[i];
32+
nums[i] = nums[j];
33+
nums[j] = tmp;
34+
};

Hard/45-jumpGameII.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var jump = function(nums) {
6+
var jumpTo = 0;
7+
var lastReach = 0;
8+
var jumps = 0;
9+
for (var i = 0; i < nums.length && i <= jumpTo; i++) {
10+
//when last jump can not reach i, increase the step by 1
11+
if (i > lastReach) {
12+
jumps++;
13+
lastReach = jumpTo;
14+
}
15+
jumpTo = Math.max(jumpTo, nums[i] + i);
16+
}
17+
18+
return lastReach >= nums.length - 1 ? jumps : 0;
19+
};
20+
21+
// second solution, easy understanding. Nice solution. BFS
22+
// [2,3,1,1,4] 2 -> 3, 1 -> 4
23+
var jump = function(nums) {
24+
var curr = 0;
25+
var next = 0;
26+
var jumps = 0;
27+
var length = nums.length;
28+
for (var i = 0; i < length;) {
29+
if (curr >= length - 1) break;
30+
// find the next (max jump to) of every element before (including) current element
31+
// once the next is chosen, it is one step jump.
32+
while (i <= curr) {
33+
next = Math.max(i + nums[i], next);
34+
i++;
35+
}
36+
37+
jumps++;
38+
curr = next;
39+
}
40+
41+
return jumps;
42+
};

Hard/57-insertInterval.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
* }
77
*/
88
/**
9+
* Solutions: 3 scenarios,
10+
* 1) [[1,2], [5,6]] insert [3,4], push [1,2] (first if),
11+
* 2) continue step 1, push [3,4] (2nd if), push[5,6] (2nd if)
12+
* 3) another example, [[1,3], [3,5]] insert [2,4]
13+
*
914
* @param {Interval[]} intervals
1015
* @param {Interval} newInterval
1116
* @return {Interval[]}
@@ -18,7 +23,8 @@ var insert = function(intervals, newInterval) {
1823
} else if (intervals[i].start > newInterval.end) {
1924
result.push(newInterval);
2025
newInterval = intervals[i];
21-
} else if (intervals[i].end >= newInterval.start || intervals[i].start <= newInterval.end) {
26+
} else {
27+
// this case is (intervals[i].end >= newInterval.start || intervals[i].start <= newInterval.end)
2228
newInterval.start = Math.min(intervals[i].start, newInterval.start);
2329
newInterval.end = Math.max(intervals[i].end, newInterval.end);
2430
}

Medium/55-jumpGame.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Key: one scenario return false, is when you meet 0 and the max move can't reach to the end.
3-
* becaue no moves can be made when the element is 0.
3+
* becaue no moves can be made when the element is 0.
44
*
55
* @param {number[]} nums
66
* @return {boolean}
@@ -14,3 +14,31 @@ var canJump = function(nums) {
1414

1515
return true;
1616
};
17+
18+
// second try, not good
19+
var canJump = function(nums) {
20+
if (nums.length === 1) return true;
21+
var maxJump = 0;
22+
for (var i = 0; i < nums.length; i++) {
23+
maxJump = Math.max(maxJump, nums[i] + i);
24+
if (maxJump === i) return false;
25+
if (maxJump >= nums.length - 1) return true;
26+
}
27+
28+
return false;
29+
};
30+
31+
var canJump = function(nums) {
32+
if (nums.length < 1) return true;
33+
var maxNextJump = nums[0];
34+
35+
for (var i = 1; i < nums.length; i++) {
36+
// jumped one step
37+
maxNextJump--;
38+
if (maxNextJump < 0) return false;
39+
// if steps at positin i is larger than maxNextJump, then nums[i] is the next jump steps.
40+
if (maxNextJump < nums[i]) maxNextJump = nums[i];
41+
}
42+
43+
return true;
44+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@
155155
* [25. Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/) - [Solution](./Hard/25-reverseNodesInKGroup.js)
156156
* [32. Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) - [Solution](./Hard/32-longestValidParentheses.js)
157157
* [33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) - [Solution](./Hard/33-searchRotatedSortedArray.js)
158+
* [41. First Missing Positive](https://leetcode.com/problems/first-missing-positive/) - [Solution](./Hard/41-firstMissingPositive.js)
159+
* [45. Jump Game II](https://leetcode.com/problems/jump-game-ii/) - [Solution](./Hard/45-jumpGameII.js)
158160
* [56. Merge Intervals](https://leetcode.com/problems/merge-intervals/) - [Solution](./Hard/56-MergeIntervals.js)
159161
* [57. Insert Interval](https://leetcode.com/problems/insert-interval/) - [Solution](./Hard/57-insertInterval.js)
160162
* [123. Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/) - [Solution](./Hard/123-bestTimeBuySellStockIII.js)

0 commit comments

Comments
 (0)