Skip to content

Commit 4e93b8c

Browse files
committed
Add solution #975
1 parent 8ef9ac7 commit 4e93b8c

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-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,058 LeetCode solutions in JavaScript
1+
# 1,059 LeetCode solutions in JavaScript
22

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

@@ -783,6 +783,7 @@
783783
972|[Equal Rational Numbers](./solutions/0972-equal-rational-numbers.js)|Hard|
784784
973|[K Closest Points to Origin](./solutions/0973-k-closest-points-to-origin.js)|Medium|
785785
974|[Subarray Sums Divisible by K](./solutions/0974-subarray-sums-divisible-by-k.js)|Medium|
786+
975|[Odd Even Jump](./solutions/0975-odd-even-jump.js)|Hard|
786787
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
787788
977|[Squares of a Sorted Array](./solutions/0977-squares-of-a-sorted-array.js)|Easy|
788789
985|[Sum of Even Numbers After Queries](./solutions/0985-sum-of-even-numbers-after-queries.js)|Easy|

solutions/0975-odd-even-jump.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* 975. Odd Even Jump
3+
* https://leetcode.com/problems/odd-even-jump/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer array arr. From some starting index, you can make a series of jumps.
7+
* The (1st, 3rd, 5th, ...) jumps in the series are called odd-numbered jumps, and the (2nd,
8+
* 4th, 6th, ...) jumps in the series are called even-numbered jumps. Note that the jumps are
9+
* numbered, not the indices.
10+
*
11+
* You may jump forward from index i to index j (with i < j) in the following way:
12+
* - During odd-numbered jumps (i.e., jumps 1, 3, 5, ...), you jump to the index j such that
13+
* arr[i] <= arr[j] and arr[j] is the smallest possible value. If there are multiple such
14+
* indices j, you can only jump to the smallest such index j.
15+
* - During even-numbered jumps (i.e., jumps 2, 4, 6, ...), you jump to the index j such that
16+
* arr[i] >= arr[j] and arr[j] is the largest possible value. If there are multiple such
17+
* indices j, you can only jump to the smallest such index j.
18+
* - It may be the case that for some index i, there are no legal jumps.
19+
*
20+
* A starting index is good if, starting from that index, you can reach the end of the array (index
21+
* arr.length - 1) by jumping some number of times (possibly 0 or more than once).
22+
*
23+
* Return the number of good starting indices.
24+
*/
25+
26+
/**
27+
* @param {number[]} arr
28+
* @return {number}
29+
*/
30+
var oddEvenJumps = function(arr) {
31+
const oddCanReach = new Array(arr.length).fill(false);
32+
const evenCanReach = new Array(arr.length).fill(false);
33+
oddCanReach[arr.length - 1] = true;
34+
evenCanReach[arr.length - 1] = true;
35+
let result = 1;
36+
37+
const sortedIndices = Array.from({ length: arr.length }, (_, i) => i);
38+
sortedIndices.sort((a, b) => arr[a] - arr[b] || a - b);
39+
40+
const oddNext = new Array(arr.length);
41+
const evenNext = new Array(arr.length);
42+
const stack = [];
43+
44+
for (const i of sortedIndices) {
45+
while (stack.length && stack[stack.length - 1] < i) {
46+
oddNext[stack.pop()] = i;
47+
}
48+
stack.push(i);
49+
}
50+
51+
sortedIndices.sort((a, b) => arr[b] - arr[a] || a - b);
52+
stack.length = 0;
53+
54+
for (const i of sortedIndices) {
55+
while (stack.length && stack[stack.length - 1] < i) {
56+
evenNext[stack.pop()] = i;
57+
}
58+
stack.push(i);
59+
}
60+
61+
for (let i = arr.length - 2; i >= 0; i--) {
62+
if (oddNext[i] !== undefined) {
63+
oddCanReach[i] = evenCanReach[oddNext[i]];
64+
if (oddCanReach[i]) result++;
65+
}
66+
if (evenNext[i] !== undefined) {
67+
evenCanReach[i] = oddCanReach[evenNext[i]];
68+
}
69+
}
70+
71+
return result;
72+
};

0 commit comments

Comments
 (0)