forked from luliyucoordinate/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.
- Loading branch information
1 parent
eac3cf0
commit ce8b21c
Showing
7 changed files
with
92 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public: | ||
int constrainedSubsetSum(vector<int>& nums, int k) { | ||
deque<int> q; | ||
int res = -10000, n = nums.size(); | ||
|
||
for (int i = 0; i < n; i++) { | ||
nums[i] = max(nums[i], nums[i] + (q.size() ? q.front() : 0)); | ||
while (q.size() && nums[i] > q.back()) q.pop_back(); | ||
q.emplace_back(nums[i]); | ||
|
||
if (i >= k && q.size() && q.front() == nums[i - k]) q.pop_front(); | ||
res = max(res, nums[i]); | ||
} | ||
return res; | ||
} | ||
}; |
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,27 @@ | ||
func constrainedSubsetSum(nums []int, k int) int { | ||
q := []int{} | ||
res, n := -10000, len(nums) | ||
|
||
for i := 0; i < n; i++ { | ||
if len(q) > 0 && q[0] > 0 { | ||
nums[i] += q[0] | ||
} | ||
for len(q) > 0 && nums[i] > q[len(q) - 1] { | ||
q = q[:len(q) - 1] | ||
} | ||
q = append(q, nums[i]) | ||
|
||
if i >= k && len(q) > 0 && q[0] == nums[i - k] { | ||
q = q[1:] | ||
} | ||
res = max(res, nums[i]) | ||
} | ||
return res | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} |
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 constrainedSubsetSum(int[] nums, int k) { | ||
Deque<Integer> q = new ArrayDeque(); | ||
int res = -10000, n = nums.length; | ||
|
||
for (int i = 0; i < n; i++) { | ||
nums[i] = Math.max(nums[i], nums[i] + (q.isEmpty() ? 0 : q.peek())); | ||
while (!q.isEmpty() && nums[i] > q.peekLast()) q.pollLast(); | ||
q.offer(nums[i]); | ||
|
||
if (i >= k && !q.isEmpty() && q.peek() == nums[i - k]) q.poll(); | ||
res = Math.max(res, nums[i]); | ||
} | ||
return res; | ||
} | ||
} |
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 @@ | ||
var constrainedSubsetSum = function(nums, k) { | ||
let q = [], res = -10000, n = nums.length; | ||
|
||
for (let i = 0; i < n; i++) { | ||
nums[i] = Math.max(nums[i], nums[i] + (q.length ? q[0] : 0)); | ||
while (q.length && nums[i] > q[q.length - 1]) q.pop(); | ||
q.push(nums[i]); | ||
|
||
if (i >= k && q.length && q[0] == nums[i - k]) q.shift(); | ||
res = Math.max(res, nums[i]); | ||
} | ||
return res; | ||
}; |
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 constrainedSubsetSum(self, nums: List[int], k: int) -> int: | ||
q, res = [], float("-inf") | ||
for i, v in enumerate(nums): | ||
nums[i] = max(v, v + (q[0] if q else 0)) | ||
while q and nums[i] > q[-1]: | ||
q.pop() | ||
q.append(nums[i]) | ||
|
||
if i >= k and q and q[0] == nums[i - k]: | ||
q.pop(0) | ||
res = max(res, nums[i]) | ||
return res |
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