Skip to content

Commit 57c6b35

Browse files
committed
+ problem 1871
1 parent 0b76b7c commit 57c6b35

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# 1871. Jump Game VII
2+
You are given a **0-indexed** binary string `s` and two integers `minJump` and `maxJump`. In the beginning, you are standing at index `0`, which is equal to `'0'`. You can move from index `i` to index `j` if the following conditions are fulfilled:
3+
4+
* `i + minJump <= j <= min(i + maxJump, s.length - 1)`, and
5+
* `s[j] == '0'`.
6+
7+
Return `true` *if you can reach index* `s.length - 1` *in* `s`*, or* `false` *otherwise*.
8+
9+
#### Example 1:
10+
<pre>
11+
<strong>Input:</strong> s = "011010", minJump = 2, maxJump = 3
12+
<strong>Output:</strong> true
13+
<strong>Explanation:</strong>
14+
In the first step, move from index 0 to index 3.
15+
In the second step, move from index 3 to index 5.
16+
</pre>
17+
18+
#### Example 2:
19+
<pre>
20+
<strong>Input:</strong> s = "01101110", minJump = 2, maxJump = 3
21+
<strong>Output:</strong> false
22+
</pre>
23+
24+
#### Constraints:
25+
* <code>2 <= s.length <= 10<sup>5</sup></code>
26+
* `s[i]` is either `'0'` or `'1'`.
27+
* `s[0] == '0'`
28+
* `1 <= minJump <= maxJump < s.length`
29+
30+
## Solutions (Rust)
31+
32+
### 1. Solution
33+
```Rust
34+
impl Solution {
35+
pub fn can_reach(s: String, min_jump: i32, max_jump: i32) -> bool {
36+
let mut indices = vec![0];
37+
38+
for (i, _) in s.chars().enumerate().skip(1).filter(|&(_, c)| c == '0') {
39+
match indices.binary_search(&(i as i32 - max_jump)) {
40+
Err(j) if j == indices.len() || indices[j] > i as i32 - min_jump => (),
41+
_ => indices.push(i as i32),
42+
}
43+
}
44+
45+
*indices.last().unwrap() == s.len() as i32 - 1
46+
}
47+
}
48+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# 1871. 跳跃游戏 VII
2+
给你一个下标从 **0** 开始的二进制字符串 `s` 和两个整数 `minJump``maxJump` 。一开始,你在下标 `0` 处,且该位置的值一定为 `'0'` 。当同时满足如下条件时,你可以从下标 `i` 移动到下标 `j` 处:
3+
4+
* `i + minJump <= j <= min(i + maxJump, s.length - 1)`
5+
* `s[j] == '0'`.
6+
7+
如果你可以到达 `s` 的下标 `s.length - 1` 处,请你返回 `true` ,否则返回 `false`
8+
9+
#### 示例 1:
10+
<pre>
11+
<strong>输入:</strong> s = "011010", minJump = 2, maxJump = 3
12+
<strong>输出:</strong> true
13+
<strong>解释:</strong>
14+
第一步,从下标 0 移动到下标 3 。
15+
第二步,从下标 3 移动到下标 5 。
16+
</pre>
17+
18+
#### 示例 2:
19+
<pre>
20+
<strong>输入:</strong> s = "01101110", minJump = 2, maxJump = 3
21+
<strong>输出:</strong> false
22+
</pre>
23+
24+
#### 提示:
25+
* <code>2 <= s.length <= 10<sup>5</sup></code>
26+
* `s[i]` 要么是 `'0'` ,要么是 `'1'`
27+
* `s[0] == '0'`
28+
* `1 <= minJump <= maxJump < s.length`
29+
30+
## 题解 (Rust)
31+
32+
### 1. 题解
33+
```Rust
34+
impl Solution {
35+
pub fn can_reach(s: String, min_jump: i32, max_jump: i32) -> bool {
36+
let mut indices = vec![0];
37+
38+
for (i, _) in s.chars().enumerate().skip(1).filter(|&(_, c)| c == '0') {
39+
match indices.binary_search(&(i as i32 - max_jump)) {
40+
Err(j) if j == indices.len() || indices[j] > i as i32 - min_jump => (),
41+
_ => indices.push(i as i32),
42+
}
43+
}
44+
45+
*indices.last().unwrap() == s.len() as i32 - 1
46+
}
47+
}
48+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn can_reach(s: String, min_jump: i32, max_jump: i32) -> bool {
3+
let mut indices = vec![0];
4+
5+
for (i, _) in s.chars().enumerate().skip(1).filter(|&(_, c)| c == '0') {
6+
match indices.binary_search(&(i as i32 - max_jump)) {
7+
Err(j) if j == indices.len() || indices[j] > i as i32 - min_jump => (),
8+
_ => indices.push(i as i32),
9+
}
10+
}
11+
12+
*indices.last().unwrap() == s.len() as i32 - 1
13+
}
14+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@
11071107
[1866][1866l]|[Number of Ways to Rearrange Sticks With K Sticks Visible][1866] |![rs]
11081108
[1869][1869l]|[Longer Contiguous Segments of Ones than Zeros][1869] |![rs]
11091109
[1870][1870l]|[Minimum Speed to Arrive on Time][1870] |![py]
1110+
[1871][1871l]|[Jump Game VII][1871] |![rs]
11101111
[1876][1876l]|[Substrings of Size Three with Distinct Characters][1876] |![rs]
11111112
[1877][1877l]|[Minimize Maximum Pair Sum in Array][1877] |![rs]
11121113
[1878][1878l]|[Get Biggest Three Rhombus Sums in a Grid][1878] |![rs]
@@ -2574,6 +2575,7 @@
25742575
[1866]:Problemset/1866-Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README.md#1866-number-of-ways-to-rearrange-sticks-with-k-sticks-visible
25752576
[1869]:Problemset/1869-Longer%20Contiguous%20Segments%20of%20Ones%20than%20Zeros/README.md#1869-longer-contiguous-segments-of-ones-than-zeros
25762577
[1870]:Problemset/1870-Minimum%20Speed%20to%20Arrive%20on%20Time/README.md#1870-minimum-speed-to-arrive-on-time
2578+
[1871]:Problemset/1871-Jump%20Game%20VII/README.md#1871-jump-game-vii
25772579
[1876]:Problemset/1876-Substrings%20of%20Size%20Three%20with%20Distinct%20Characters/README.md#1876-substrings-of-size-three-with-distinct-characters
25782580
[1877]:Problemset/1877-Minimize%20Maximum%20Pair%20Sum%20in%20Array/README.md#1877-minimize-maximum-pair-sum-in-array
25792581
[1878]:Problemset/1878-Get%20Biggest%20Three%20Rhombus%20Sums%20in%20a%20Grid/README.md#1878-get-biggest-three-rhombus-sums-in-a-grid
@@ -4040,6 +4042,7 @@
40404042
[1866l]:https://leetcode.com/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible/
40414043
[1869l]:https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/
40424044
[1870l]:https://leetcode.com/problems/minimum-speed-to-arrive-on-time/
4045+
[1871l]:https://leetcode.com/problems/jump-game-vii/
40434046
[1876l]:https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/
40444047
[1877l]:https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/
40454048
[1878l]:https://leetcode.com/problems/get-biggest-three-rhombus-sums-in-a-grid/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@
11071107
[1866][1866l]|[恰有 K 根木棍可以看到的排列数目][1866] |![rs]
11081108
[1869][1869l]|[哪种连续子字符串更长][1869] |![rs]
11091109
[1870][1870l]|[准时到达的列车最小时速][1870] |![py]
1110+
[1871][1871l]|[跳跃游戏 VII][1871] |![rs]
11101111
[1876][1876l]|[长度为三且各字符不同的子字符串][1876] |![rs]
11111112
[1877][1877l]|[数组中最大数对和的最小值][1877] |![rs]
11121113
[1878][1878l]|[矩阵中最大的三个菱形和][1878] |![rs]
@@ -2574,6 +2575,7 @@
25742575
[1866]:Problemset/1866-Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README_CN.md#1866-恰有-k-根木棍可以看到的排列数目
25752576
[1869]:Problemset/1869-Longer%20Contiguous%20Segments%20of%20Ones%20than%20Zeros/README_CN.md#1869-哪种连续子字符串更长
25762577
[1870]:Problemset/1870-Minimum%20Speed%20to%20Arrive%20on%20Time/README_CN.md#1870-准时到达的列车最小时速
2578+
[1871]:Problemset/1871-Jump%20Game%20VII/README_CN.md#1871-跳跃游戏-vii
25772579
[1876]:Problemset/1876-Substrings%20of%20Size%20Three%20with%20Distinct%20Characters/README_CN.md#1876-长度为三且各字符不同的子字符串
25782580
[1877]:Problemset/1877-Minimize%20Maximum%20Pair%20Sum%20in%20Array/README_CN.md#1877-数组中最大数对和的最小值
25792581
[1878]:Problemset/1878-Get%20Biggest%20Three%20Rhombus%20Sums%20in%20a%20Grid/README_CN.md#1878-矩阵中最大的三个菱形和
@@ -4040,6 +4042,7 @@
40404042
[1866l]:https://leetcode.cn/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible/
40414043
[1869l]:https://leetcode.cn/problems/longer-contiguous-segments-of-ones-than-zeros/
40424044
[1870l]:https://leetcode.cn/problems/minimum-speed-to-arrive-on-time/
4045+
[1871l]:https://leetcode.cn/problems/jump-game-vii/
40434046
[1876l]:https://leetcode.cn/problems/substrings-of-size-three-with-distinct-characters/
40444047
[1877l]:https://leetcode.cn/problems/minimize-maximum-pair-sum-in-array/
40454048
[1878l]:https://leetcode.cn/problems/get-biggest-three-rhombus-sums-in-a-grid/

0 commit comments

Comments
 (0)