Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1696
Browse files Browse the repository at this point in the history
No.1696.Jump Game VI
  • Loading branch information
yanglbme committed Dec 5, 2022
1 parent 46e6dcf commit 9b12342
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 2 deletions.
88 changes: 87 additions & 1 deletion solution/1600-1699/1696.Jump Game VI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,108 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:动态规划 + 单调队列优化**

我们定义 $f[i]$ 表示到达下标 $i$ 的最大得分,那么 $f[i]$ 的值可以从 $f[j]$ 转移而来,其中 $j$ 满足 $i - k \leq j \leq i - 1$。因此我们可以使用动态规划求解。

状态转移方程为:

$$
f[i] = \max_{j \in [i - k, i - 1]} f[j] + nums[i]
$$

我们可以使用单调队列优化状态转移方程,具体做法是维护一个单调递减的队列,队列中存储的是下标 $j$,并且队列中的下标对应的 $f[j]$ 值是单调递减的。在进行状态转移时,我们只需要取出队首的下标 $j$,即可得到 $f[j]$ 的最大值,然后将 $f[i]$ 的值更新为 $f[j] + nums[i]$ 即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def maxResult(self, nums: List[int], k: int) -> int:
n = len(nums)
f = [0] * n
q = deque([0])
for i in range(n):
if i - q[0] > k:
q.popleft()
f[i] = nums[i] + f[q[0]]
while q and f[q[-1]] <= f[i]:
q.pop()
q.append(i)
return f[-1]
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int maxResult(int[] nums, int k) {
int n = nums.length;
int[] f = new int[n];
Deque<Integer> q = new ArrayDeque<>();
q.offer(0);
for (int i = 0; i < n; ++i) {
if (i - q.peekFirst() > k) {
q.pollFirst();
}
f[i] = nums[i] + f[q.peekFirst()];
while (!q.isEmpty() && f[q.peekLast()] <= f[i]) {
q.pollLast();
}
q.offerLast(i);
}
return f[n - 1];
}
}
```

### **C++**

```cpp
class Solution {
public:
int maxResult(vector<int>& nums, int k) {
int n = nums.size();
int f[n];
f[0] = 0;
deque<int> q = {0};
for (int i = 0; i < n; ++i) {
if (i - q.front() > k) q.pop_front();
f[i] = nums[i] + f[q.front()];
while (!q.empty() && f[q.back()] <= f[i]) q.pop_back();
q.push_back(i);
}
return f[n - 1];
}
};
```
### **Go**
```go
func maxResult(nums []int, k int) int {
n := len(nums)
f := make([]int, n)
q := []int{0}
for i, v := range nums {
if i-q[0] > k {
q = q[1:]
}
f[i] = v + f[q[0]]
for len(q) > 0 && f[q[len(q)-1]] <= f[i] {
q = q[:len(q)-1]
}
q = append(q, i)
}
return f[n-1]
}
```

### **...**
Expand Down
74 changes: 73 additions & 1 deletion solution/1600-1699/1696.Jump Game VI/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,85 @@
### **Python3**

```python

class Solution:
def maxResult(self, nums: List[int], k: int) -> int:
n = len(nums)
f = [0] * n
q = deque([0])
for i in range(n):
if i - q[0] > k:
q.popleft()
f[i] = nums[i] + f[q[0]]
while q and f[q[-1]] <= f[i]:
q.pop()
q.append(i)
return f[-1]
```

### **Java**

```java
class Solution {
public int maxResult(int[] nums, int k) {
int n = nums.length;
int[] f = new int[n];
Deque<Integer> q = new ArrayDeque<>();
q.offer(0);
for (int i = 0; i < n; ++i) {
if (i - q.peekFirst() > k) {
q.pollFirst();
}
f[i] = nums[i] + f[q.peekFirst()];
while (!q.isEmpty() && f[q.peekLast()] <= f[i]) {
q.pollLast();
}
q.offerLast(i);
}
return f[n - 1];
}
}
```

### **C++**

```cpp
class Solution {
public:
int maxResult(vector<int>& nums, int k) {
int n = nums.size();
int f[n];
f[0] = 0;
deque<int> q = {0};
for (int i = 0; i < n; ++i) {
if (i - q.front() > k) q.pop_front();
f[i] = nums[i] + f[q.front()];
while (!q.empty() && f[q.back()] <= f[i]) q.pop_back();
q.push_back(i);
}
return f[n - 1];
}
};
```
### **Go**
```go
func maxResult(nums []int, k int) int {
n := len(nums)
f := make([]int, n)
q := []int{0}
for i, v := range nums {
if i-q[0] > k {
q = q[1:]
}
f[i] = v + f[q[0]]
for len(q) > 0 && f[q[len(q)-1]] <= f[i] {
q = q[:len(q)-1]
}
q = append(q, i)
}
return f[n-1]
}
```

### **...**
Expand Down
16 changes: 16 additions & 0 deletions solution/1600-1699/1696.Jump Game VI/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int maxResult(vector<int>& nums, int k) {
int n = nums.size();
int f[n];
f[0] = 0;
deque<int> q = {0};
for (int i = 0; i < n; ++i) {
if (i - q.front() > k) q.pop_front();
f[i] = nums[i] + f[q.front()];
while (!q.empty() && f[q.back()] <= f[i]) q.pop_back();
q.push_back(i);
}
return f[n - 1];
}
};
16 changes: 16 additions & 0 deletions solution/1600-1699/1696.Jump Game VI/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func maxResult(nums []int, k int) int {
n := len(nums)
f := make([]int, n)
q := []int{0}
for i, v := range nums {
if i-q[0] > k {
q = q[1:]
}
f[i] = v + f[q[0]]
for len(q) > 0 && f[q[len(q)-1]] <= f[i] {
q = q[:len(q)-1]
}
q = append(q, i)
}
return f[n-1]
}
19 changes: 19 additions & 0 deletions solution/1600-1699/1696.Jump Game VI/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int maxResult(int[] nums, int k) {
int n = nums.length;
int[] f = new int[n];
Deque<Integer> q = new ArrayDeque<>();
q.offer(0);
for (int i = 0; i < n; ++i) {
if (i - q.peekFirst() > k) {
q.pollFirst();
}
f[i] = nums[i] + f[q.peekFirst()];
while (!q.isEmpty() && f[q.peekLast()] <= f[i]) {
q.pollLast();
}
q.offerLast(i);
}
return f[n - 1];
}
}
13 changes: 13 additions & 0 deletions solution/1600-1699/1696.Jump Game VI/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def maxResult(self, nums: List[int], k: int) -> int:
n = len(nums)
f = [0] * n
q = deque([0])
for i in range(n):
if i - q[0] > k:
q.popleft()
f[i] = nums[i] + f[q[0]]
while q and f[q[-1]] <= f[i]:
q.pop()
q.append(i)
return f[-1]

0 comments on commit 9b12342

Please sign in to comment.