diff --git a/README.md b/README.md index a90ef380..dec7d1de 100644 --- a/README.md +++ b/README.md @@ -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| \ No newline at end of file +|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| \ No newline at end of file diff --git a/src/1425-Constrained-Subsequence-Sum/1425.cpp b/src/1425-Constrained-Subsequence-Sum/1425.cpp new file mode 100644 index 00000000..468fd933 --- /dev/null +++ b/src/1425-Constrained-Subsequence-Sum/1425.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int constrainedSubsetSum(vector& nums, int k) { + deque 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; + } +}; \ No newline at end of file diff --git a/src/1425-Constrained-Subsequence-Sum/1425.go b/src/1425-Constrained-Subsequence-Sum/1425.go new file mode 100644 index 00000000..df4e26ff --- /dev/null +++ b/src/1425-Constrained-Subsequence-Sum/1425.go @@ -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 +} \ No newline at end of file diff --git a/src/1425-Constrained-Subsequence-Sum/1425.java b/src/1425-Constrained-Subsequence-Sum/1425.java new file mode 100644 index 00000000..7c0c2b65 --- /dev/null +++ b/src/1425-Constrained-Subsequence-Sum/1425.java @@ -0,0 +1,16 @@ +class Solution { + public int constrainedSubsetSum(int[] nums, int k) { + Deque 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; + } +} \ No newline at end of file diff --git a/src/1425-Constrained-Subsequence-Sum/1425.js b/src/1425-Constrained-Subsequence-Sum/1425.js new file mode 100644 index 00000000..60c24920 --- /dev/null +++ b/src/1425-Constrained-Subsequence-Sum/1425.js @@ -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; +}; \ No newline at end of file diff --git a/src/1425-Constrained-Subsequence-Sum/1425.py b/src/1425-Constrained-Subsequence-Sum/1425.py new file mode 100644 index 00000000..72974715 --- /dev/null +++ b/src/1425-Constrained-Subsequence-Sum/1425.py @@ -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 \ No newline at end of file diff --git a/src/addProb.py b/src/addProb.py index fc469a0d..cb075056 100644 --- a/src/addProb.py +++ b/src/addProb.py @@ -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']