From 9b12342dd21d8cf1287f9402a6f5cdf524048f4f Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 5 Dec 2022 21:52:03 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1696 No.1696.Jump Game VI --- .../1600-1699/1696.Jump Game VI/README.md | 88 ++++++++++++++++++- .../1600-1699/1696.Jump Game VI/README_EN.md | 74 +++++++++++++++- .../1600-1699/1696.Jump Game VI/Solution.cpp | 16 ++++ .../1600-1699/1696.Jump Game VI/Solution.go | 16 ++++ .../1600-1699/1696.Jump Game VI/Solution.java | 19 ++++ .../1600-1699/1696.Jump Game VI/Solution.py | 13 +++ 6 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 solution/1600-1699/1696.Jump Game VI/Solution.cpp create mode 100644 solution/1600-1699/1696.Jump Game VI/Solution.go create mode 100644 solution/1600-1699/1696.Jump Game VI/Solution.java create mode 100644 solution/1600-1699/1696.Jump Game VI/Solution.py diff --git a/solution/1600-1699/1696.Jump Game VI/README.md b/solution/1600-1699/1696.Jump Game VI/README.md index c7b3ad953d2fb..a4125f1d0a91b 100644 --- a/solution/1600-1699/1696.Jump Game VI/README.md +++ b/solution/1600-1699/1696.Jump Game VI/README.md @@ -52,6 +52,20 @@ +**方法一:动态规划 + 单调队列优化** + +我们定义 $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$ 为数组的长度。 + ### **Python3** @@ -59,7 +73,19 @@ ```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** @@ -67,7 +93,67 @@ ```java +class Solution { + public int maxResult(int[] nums, int k) { + int n = nums.length; + int[] f = new int[n]; + Deque 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& nums, int k) { + int n = nums.size(); + int f[n]; + f[0] = 0; + deque 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] +} ``` ### **...** diff --git a/solution/1600-1699/1696.Jump Game VI/README_EN.md b/solution/1600-1699/1696.Jump Game VI/README_EN.md index 323863c564efd..ec7d1e60f7411 100644 --- a/solution/1600-1699/1696.Jump Game VI/README_EN.md +++ b/solution/1600-1699/1696.Jump Game VI/README_EN.md @@ -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 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& nums, int k) { + int n = nums.size(); + int f[n]; + f[0] = 0; + deque 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] +} ``` ### **...** diff --git a/solution/1600-1699/1696.Jump Game VI/Solution.cpp b/solution/1600-1699/1696.Jump Game VI/Solution.cpp new file mode 100644 index 0000000000000..53a3781ee1278 --- /dev/null +++ b/solution/1600-1699/1696.Jump Game VI/Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int maxResult(vector& nums, int k) { + int n = nums.size(); + int f[n]; + f[0] = 0; + deque 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]; + } +}; \ No newline at end of file diff --git a/solution/1600-1699/1696.Jump Game VI/Solution.go b/solution/1600-1699/1696.Jump Game VI/Solution.go new file mode 100644 index 0000000000000..7a381073f9b10 --- /dev/null +++ b/solution/1600-1699/1696.Jump Game VI/Solution.go @@ -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] +} \ No newline at end of file diff --git a/solution/1600-1699/1696.Jump Game VI/Solution.java b/solution/1600-1699/1696.Jump Game VI/Solution.java new file mode 100644 index 0000000000000..7191c75180cb9 --- /dev/null +++ b/solution/1600-1699/1696.Jump Game VI/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public int maxResult(int[] nums, int k) { + int n = nums.length; + int[] f = new int[n]; + Deque 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]; + } +} \ No newline at end of file diff --git a/solution/1600-1699/1696.Jump Game VI/Solution.py b/solution/1600-1699/1696.Jump Game VI/Solution.py new file mode 100644 index 0000000000000..e0433dd84cd02 --- /dev/null +++ b/solution/1600-1699/1696.Jump Game VI/Solution.py @@ -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]