Skip to content

Commit

Permalink
add 1425
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed May 4, 2020
1 parent eac3cf0 commit ce8b21c
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,4 +718,5 @@ LeetCode
|1420|[Build Array Where You Can Find The Maximum Exactly K Comparisons](https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/)|c|[c++](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.cpp)|[python](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.py)|[go](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.go)|[js](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.js)|[java](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.java)|Hard|
|1422|[Maximum Score After Splitting a String](https://leetcode.com/problems/maximum-score-after-splitting-a-string/)|c|[c++](./src/1422-Maximum-Score-After-Splitting-a-String/1422.cpp)|[python](./src/1422-Maximum-Score-After-Splitting-a-String/1422.py)|[go](./src/1422-Maximum-Score-After-Splitting-a-String/1422.go)|[js](./src/1422-Maximum-Score-After-Splitting-a-String/1422.js)|[java](./src/1422-Maximum-Score-After-Splitting-a-String/1422.java)|Easy|
|1423|[Maximum Points You Can Obtain from Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/)|c|[c++](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.cpp)|[python](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.py)|[go](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.go)|[js](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.js)|[java](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.java)|Medium|
|1424|[Diagonal Traverse II](https://leetcode.com/problems/diagonal-traverse-ii/)|c|[c++](./src/1424-Diagonal-Traverse-II/1424.cpp)|[python](./src/1424-Diagonal-Traverse-II/1424.py)|[go](./src/1424-Diagonal-Traverse-II/1424.go)|[js](./src/1424-Diagonal-Traverse-II/1424.js)|[java](./src/1424-Diagonal-Traverse-II/1424.java)|Medium|
|1424|[Diagonal Traverse II](https://leetcode.com/problems/diagonal-traverse-ii/)|c|[c++](./src/1424-Diagonal-Traverse-II/1424.cpp)|[python](./src/1424-Diagonal-Traverse-II/1424.py)|[go](./src/1424-Diagonal-Traverse-II/1424.go)|[js](./src/1424-Diagonal-Traverse-II/1424.js)|[java](./src/1424-Diagonal-Traverse-II/1424.java)|Medium|
|1425|[Constrained Subsequence Sum](https://leetcode.com/problems/constrained-subsequence-sum/)|c|[c++](./src/1425-Constrained-Subsequence-Sum/1425.cpp)|[python](./src/1425-Constrained-Subsequence-Sum/1425.py)|[go](./src/1425-Constrained-Subsequence-Sum/1425.go)|[js](./src/1425-Constrained-Subsequence-Sum/1425.js)|[java](./src/1425-Constrained-Subsequence-Sum/1425.java)|Hard|
17 changes: 17 additions & 0 deletions src/1425-Constrained-Subsequence-Sum/1425.cpp
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;
}
};
27 changes: 27 additions & 0 deletions src/1425-Constrained-Subsequence-Sum/1425.go
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
}
16 changes: 16 additions & 0 deletions src/1425-Constrained-Subsequence-Sum/1425.java
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;
}
}
13 changes: 13 additions & 0 deletions src/1425-Constrained-Subsequence-Sum/1425.js
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;
};
13 changes: 13 additions & 0 deletions src/1425-Constrained-Subsequence-Sum/1425.py
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
8 changes: 4 additions & 4 deletions src/addProb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import os, bisect

# 题目名称
name = "Diagonal Traverse II"
ID = 1424
url = "https://leetcode.com/problems/diagonal-traverse-ii/"
difficult = "Medium"
name = "Constrained Subsequence Sum"
ID = 1425
url = "https://leetcode.com/problems/constrained-subsequence-sum/"
difficult = "Hard"
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']


Expand Down

0 comments on commit ce8b21c

Please sign in to comment.