forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1696
No.1696.Jump Game VI
- Loading branch information
Showing
6 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |