Skip to content

Commit 172d4f4

Browse files
committed
+ problem 975
1 parent 8fc6536 commit 172d4f4

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 975. Odd Even Jump
2+
You are given an integer array `arr`. From some starting index, you can make a series of jumps. The (1<sup>st</sup>, 3<sup>rd</sup>, 5<sup>th</sup>, ...) jumps in the series are called **odd-numbered jumps**, and the (2<sup>nd</sup>, 4<sup>th</sup>, 6<sup>th</sup>, ...) jumps in the series are called **even-numbered jumps**. Note that the **jumps** are numbered, not the indices.
3+
4+
You may jump forward from index `i` to index `j` (with `i < j`) in the following way:
5+
6+
* During **odd-numbered jumps** (i.e., jumps 1, 3, 5, ...), you jump to the index `j` such that `arr[i] <= arr[j]` and `arr[j]` is the smallest possible value. If there are multiple such indices `j`, you can only jump to the **smallest** such index `j`.
7+
* During **even-numbered jumps** (i.e., jumps 2, 4, 6, ...), you jump to the index j such that `arr[i] >= arr[j]` and `arr[j]` is the largest possible value. If there are multiple such indices `j`, you can only jump to the **smallest** such index `j`.
8+
* It may be the case that for some index `i`, there are no legal jumps.
9+
10+
A starting index is **good** if, starting from that index, you can reach the end of the array (index `arr.length - 1`) by jumping some number of times (possibly 0 or more than once).
11+
12+
Return *the number of **good** starting indices*.
13+
14+
#### Example 1:
15+
<pre>
16+
<strong>Input:</strong> arr = [10,13,12,14,15]
17+
<strong>Output:</strong> 2
18+
<strong>Explanation:</strong>
19+
From starting index i = 0, we can make our 1st jump to i = 2 (since arr[2] is the smallest among arr[1], arr[2], arr[3], arr[4] that is greater or equal to arr[0]), then we cannot jump any more.
20+
From starting index i = 1 and i = 2, we can make our 1st jump to i = 3, then we cannot jump any more.
21+
From starting index i = 3, we can make our 1st jump to i = 4, so we have reached the end.
22+
From starting index i = 4, we have reached the end already.
23+
In total, there are 2 different starting indices i = 3 and i = 4, where we can reach the end with some number of
24+
jumps.
25+
</pre>
26+
27+
#### Example 2:
28+
<pre>
29+
<strong>Input:</strong> arr = [2,3,1,1,4]
30+
<strong>Output:</strong> 3
31+
<strong>Explanation:</strong>
32+
From starting index i = 0, we make jumps to i = 1, i = 2, i = 3:
33+
During our 1st jump (odd-numbered), we first jump to i = 1 because arr[1] is the smallest value in [arr[1], arr[2], arr[3], arr[4]] that is greater than or equal to arr[0].
34+
During our 2nd jump (even-numbered), we jump from i = 1 to i = 2 because arr[2] is the largest value in [arr[2], arr[3], arr[4]] that is less than or equal to arr[1]. arr[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3
35+
During our 3rd jump (odd-numbered), we jump from i = 2 to i = 3 because arr[3] is the smallest value in [arr[3], arr[4]] that is greater than or equal to arr[2].
36+
We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good.
37+
In a similar manner, we can deduce that:
38+
From starting index i = 1, we jump to i = 4, so we reach the end.
39+
From starting index i = 2, we jump to i = 3, and then we can't jump anymore.
40+
From starting index i = 3, we jump to i = 4, so we reach the end.
41+
From starting index i = 4, we are already at the end.
42+
In total, there are 3 different starting indices i = 1, i = 3, and i = 4, where we can reach the end with some
43+
number of jumps.
44+
</pre>
45+
46+
#### Example 3:
47+
<pre>
48+
<strong>Input:</strong> arr = [5,1,3,4,2]
49+
<strong>Output:</strong> 3
50+
<strong>Explanation:</strong> We can reach the end from starting indices 1, 2, and 4.
51+
</pre>
52+
53+
#### Constraints:
54+
* <code>1 <= arr.length <= 2 * 10<sup>4</sup></code>
55+
* <code>0 <= arr[i] < 10<sup>5</sup></code>
56+
57+
## Solutions (Python)
58+
59+
### 1. Solution
60+
```Python
61+
from sortedcontainers import SortedList, SortedKeyList
62+
63+
64+
class Solution:
65+
def oddEvenJumps(self, arr: List[int]) -> int:
66+
n = len(arr)
67+
asc = SortedList()
68+
desc = SortedKeyList(key=lambda x: [-x[0], x[1]])
69+
dp = [[False, False] for _ in range(n)]
70+
dp[n - 1] = [True, True]
71+
ret = 0
72+
73+
for i in range(n)[::-1]:
74+
j = asc.bisect_left([arr[i], 0])
75+
j = asc[j][1] if j < len(asc) else n
76+
dp[i][0] |= j < n and dp[j][1]
77+
j = desc.bisect_left([arr[i], 0])
78+
j = desc[j][1] if j < len(desc) else n
79+
dp[i][1] |= j < n and dp[j][0]
80+
81+
asc.add([arr[i], i])
82+
desc.add([arr[i], i])
83+
84+
if dp[i][0]:
85+
ret += 1
86+
87+
return ret
88+
```
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# 975. 奇偶跳
2+
给定一个整数数组 `A`,你可以从某一起始索引出发,跳跃一定次数。在你跳跃的过程中,第 1、3、5... 次跳跃称为奇数跳跃,而第 2、4、6... 次跳跃称为偶数跳跃。
3+
4+
你可以按以下方式从索引 `i` 向后跳转到索引 `j`(其中 `i < j`):
5+
6+
* 在进行奇数跳跃时(如,第 1,3,5... 次跳跃),你将会跳到索引 `j`,使得 `A[i] <= A[j]``A[j]` 是可能的最小值。如果存在多个这样的索引 `j`,你只能跳到满足要求的**最小**索引 `j` 上。
7+
* 在进行偶数跳跃时(如,第 2,4,6... 次跳跃),你将会跳到索引 `j`,使得 `A[i] >= A[j]``A[j]` 是可能的最大值。如果存在多个这样的索引 `j`,你只能跳到满足要求的**最小**索引 `j` 上。
8+
* (对于某些索引 `i`,可能无法进行合乎要求的跳跃。)
9+
10+
如果从某一索引开始跳跃一定次数(可能是 0 次或多次),就可以到达数组的末尾(索引 `A.length - 1`),那么该索引就会被认为是好的起始索引。
11+
12+
返回好的起始索引的数量。
13+
14+
#### 示例 1:
15+
<pre>
16+
<strong>输入:</strong> arr = [10,13,12,14,15]
17+
<strong>输出:</strong> 2
18+
<strong>解释:</strong>
19+
从起始索引 i = 0 出发,我们可以跳到 i = 2,(因为 A[2] 是 A[1],A[2],A[3],A[4] 中大于或等于 A[0] 的最小值),然后我们就无法继续跳下去了。
20+
从起始索引 i = 1 和 i = 2 出发,我们可以跳到 i = 3,然后我们就无法继续跳下去了。
21+
从起始索引 i = 3 出发,我们可以跳到 i = 4,到达数组末尾。
22+
从起始索引 i = 4 出发,我们已经到达数组末尾。
23+
总之,我们可以从 2 个不同的起始索引(i = 3, i = 4)出发,通过一定数量的跳跃到达数组末尾。
24+
</pre>
25+
26+
#### 示例 2:
27+
<pre>
28+
<strong>输入:</strong> arr = [2,3,1,1,4]
29+
<strong>输出:</strong> 3
30+
<strong>解释:</strong>
31+
从起始索引 i=0 出发,我们依次可以跳到 i = 1,i = 2,i = 3:
32+
33+
在我们的第一次跳跃(奇数)中,我们先跳到 i = 1,因为 A[1] 是(A[1],A[2],A[3],A[4])中大于或等于 A[0] 的最小值。
34+
35+
在我们的第二次跳跃(偶数)中,我们从 i = 1 跳到 i = 2,因为 A[2] 是(A[2],A[3],A[4])中小于或等于 A[1] 的最大值。A[3] 也是最大的值,但 2 是一个较小的索引,所以我们只能跳到 i = 2,而不能跳到 i = 3。
36+
37+
在我们的第三次跳跃(奇数)中,我们从 i = 2 跳到 i = 3,因为 A[3] 是(A[3],A[4])中大于或等于 A[2] 的最小值。
38+
39+
我们不能从 i = 3 跳到 i = 4,所以起始索引 i = 0 不是好的起始索引。
40+
41+
类似地,我们可以推断:
42+
从起始索引 i = 1 出发, 我们跳到 i = 4,这样我们就到达数组末尾。
43+
从起始索引 i = 2 出发, 我们跳到 i = 3,然后我们就不能再跳了。
44+
从起始索引 i = 3 出发, 我们跳到 i = 4,这样我们就到达数组末尾。
45+
从起始索引 i = 4 出发,我们已经到达数组末尾。
46+
总之,我们可以从 3 个不同的起始索引(i = 1, i = 3, i = 4)出发,通过一定数量的跳跃到达数组末尾。
47+
</pre>
48+
49+
#### 示例 3:
50+
<pre>
51+
<strong>输入:</strong> arr = [5,1,3,4,2]
52+
<strong>输出:</strong> 3
53+
<strong>解释:</strong>
54+
我们可以从起始索引 1,2,4 出发到达数组末尾。
55+
</pre>
56+
57+
#### 提示:
58+
1. `1 <= A.length <= 20000`
59+
2. `0 <= A[i] < 100000`
60+
61+
## 题解 (Python)
62+
63+
### 1. 题解
64+
```Python
65+
from sortedcontainers import SortedList, SortedKeyList
66+
67+
68+
class Solution:
69+
def oddEvenJumps(self, arr: List[int]) -> int:
70+
n = len(arr)
71+
asc = SortedList()
72+
desc = SortedKeyList(key=lambda x: [-x[0], x[1]])
73+
dp = [[False, False] for _ in range(n)]
74+
dp[n - 1] = [True, True]
75+
ret = 0
76+
77+
for i in range(n)[::-1]:
78+
j = asc.bisect_left([arr[i], 0])
79+
j = asc[j][1] if j < len(asc) else n
80+
dp[i][0] |= j < n and dp[j][1]
81+
j = desc.bisect_left([arr[i], 0])
82+
j = desc[j][1] if j < len(desc) else n
83+
dp[i][1] |= j < n and dp[j][0]
84+
85+
asc.add([arr[i], i])
86+
desc.add([arr[i], i])
87+
88+
if dp[i][0]:
89+
ret += 1
90+
91+
return ret
92+
```
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from sortedcontainers import SortedList, SortedKeyList
2+
3+
4+
class Solution:
5+
def oddEvenJumps(self, arr: List[int]) -> int:
6+
n = len(arr)
7+
asc = SortedList()
8+
desc = SortedKeyList(key=lambda x: [-x[0], x[1]])
9+
dp = [[False, False] for _ in range(n)]
10+
dp[n - 1] = [True, True]
11+
ret = 0
12+
13+
for i in range(n)[::-1]:
14+
j = asc.bisect_left([arr[i], 0])
15+
j = asc[j][1] if j < len(asc) else n
16+
dp[i][0] |= j < n and dp[j][1]
17+
j = desc.bisect_left([arr[i], 0])
18+
j = desc[j][1] if j < len(desc) else n
19+
dp[i][1] |= j < n and dp[j][0]
20+
21+
asc.add([arr[i], i])
22+
desc.add([arr[i], i])
23+
24+
if dp[i][0]:
25+
ret += 1
26+
27+
return ret

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@
512512
[970][970l] |[Powerful Integers][970] |![rs]
513513
[973][973l] |[K Closest Points to Origin][973] |![rs]
514514
[974][974l] |[Subarray Sums Divisible by K][974] |![rb]&nbsp;&nbsp;![rs]
515+
[975][975l] |[Odd Even Jump][975] |![py]
515516
[976][976l] |[Largest Perimeter Triangle][976] |![rs]
516517
[977][977l] |[Squares of a Sorted Array][977] |![rs]
517518
[978][978l] |[Longest Turbulent Subarray][978] |![rs]
@@ -1702,6 +1703,7 @@
17021703
[970]:Problemset/0970-Powerful%20Integers/README.md#970-powerful-integers
17031704
[973]:Problemset/0973-K%20Closest%20Points%20to%20Origin/README.md#973-k-closest-points-to-origin
17041705
[974]:Problemset/0974-Subarray%20Sums%20Divisible%20by%20K/README.md#974-subarray-sums-divisible-by-k
1706+
[975]:Problemset/0975-Odd%20Even%20Jump/README.md#975-odd-even-jump
17051707
[976]:Problemset/0976-Largest%20Perimeter%20Triangle/README.md#976-largest-perimeter-triangle
17061708
[977]:Problemset/0977-Squares%20of%20a%20Sorted%20Array/README.md#977-squares-of-a-sorted-array
17071709
[978]:Problemset/0978-Longest%20Turbulent%20Subarray/README.md#978-longest-turbulent-subarray
@@ -2896,6 +2898,7 @@
28962898
[970l]:https://leetcode.com/problems/powerful-integers/
28972899
[973l]:https://leetcode.com/problems/k-closest-points-to-origin/
28982900
[974l]:https://leetcode.com/problems/subarray-sums-divisible-by-k/
2901+
[975l]:https://leetcode.com/problems/odd-even-jump/
28992902
[976l]:https://leetcode.com/problems/largest-perimeter-triangle/
29002903
[977l]:https://leetcode.com/problems/squares-of-a-sorted-array/
29012904
[978l]:https://leetcode.com/problems/longest-turbulent-subarray/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@
512512
[970][970l] |[强整数][970] |![rs]
513513
[973][973l] |[最接近原点的 K 个点][973] |![rs]
514514
[974][974l] |[和可被 K 整除的子数组][974] |![rb]&nbsp;&nbsp;![rs]
515+
[975][975l] |[奇偶跳][975] |![py]
515516
[976][976l] |[三角形的最大周长][976] |![rs]
516517
[977][977l] |[有序数组的平方][977] |![rs]
517518
[978][978l] |[最长湍流子数组][978] |![rs]
@@ -1702,6 +1703,7 @@
17021703
[970]:Problemset/0970-Powerful%20Integers/README_CN.md#970-强整数
17031704
[973]:Problemset/0973-K%20Closest%20Points%20to%20Origin/README_CN.md#973-最接近原点的-K-个点
17041705
[974]:Problemset/0974-Subarray%20Sums%20Divisible%20by%20K/README_CN.md#974-和可被-k-整除的子数组
1706+
[975]:Problemset/0975-Odd%20Even%20Jump/README_CN.md#975-奇偶跳
17051707
[976]:Problemset/0976-Largest%20Perimeter%20Triangle/README_CN.md#976-三角形的最大周长
17061708
[977]:Problemset/0977-Squares%20of%20a%20Sorted%20Array/README_CN.md#977-有序数组的平方
17071709
[978]:Problemset/0978-Longest%20Turbulent%20Subarray/README_CN.md#978-最长湍流子数组
@@ -2896,6 +2898,7 @@
28962898
[970l]:https://leetcode.cn/problems/powerful-integers/
28972899
[973l]:https://leetcode.cn/problems/k-closest-points-to-origin/
28982900
[974l]:https://leetcode.cn/problems/subarray-sums-divisible-by-k/
2901+
[975l]:https://leetcode.cn/problems/odd-even-jump/
28992902
[976l]:https://leetcode.cn/problems/largest-perimeter-triangle/
29002903
[977l]:https://leetcode.cn/problems/squares-of-a-sorted-array/
29012904
[978l]:https://leetcode.cn/problems/longest-turbulent-subarray/

0 commit comments

Comments
 (0)