Skip to content

Commit 811bf6e

Browse files
committed
+ problem 1425
1 parent 7754e39 commit 811bf6e

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 1425. Constrained Subsequence Sum
2+
Given an integer array `nums` and an integer `k`, return the maximum sum of a **non-empty** subsequence of that array such that for every two **consecutive** integers in the subsequence, `nums[i]` and `nums[j]`, where `i < j`, the condition `j - i <= k` is satisfied.
3+
4+
A *subsequence* of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> nums = [10,2,-10,5,20], k = 2
9+
<strong>Output:</strong> 37
10+
<strong>Explanation:</strong> The subsequence is [10, 2, 5, 20].
11+
</pre>
12+
13+
#### Example 2:
14+
<pre>
15+
<strong>Input:</strong> nums = [-1,-2,-3], k = 1
16+
<strong>Output:</strong> -1
17+
<strong>Explanation:</strong> The subsequence must be non-empty, so we choose the largest number.
18+
</pre>
19+
20+
#### Example 3:
21+
<pre>
22+
<strong>Input:</strong> nums = [10,-2,-10,-5,20], k = 2
23+
<strong>Output:</strong> 23
24+
<strong>Explanation:</strong> The subsequence is [10, -2, -5, 20].
25+
</pre>
26+
27+
#### Constraints:
28+
* <code>1 <= k <= nums.length <= 10<sup>5</sup></code>
29+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
30+
31+
## Solutions (Rust)
32+
33+
### 1. Solution
34+
```Rust
35+
use std::collections::VecDeque;
36+
37+
impl Solution {
38+
pub fn constrained_subset_sum(nums: Vec<i32>, k: i32) -> i32 {
39+
let k = k as usize;
40+
let mut deque = VecDeque::new();
41+
let mut ret = *nums.iter().max().unwrap();
42+
43+
for i in 0..nums.len() {
44+
if i - deque.front().unwrap_or(&(i, 0)).0 > k {
45+
deque.pop_front();
46+
}
47+
48+
let x = nums[i] + deque.front().unwrap_or(&(0, 0)).1;
49+
50+
if x > 0 {
51+
while deque.back().unwrap_or(&(0, i32::MAX)).1 <= x {
52+
deque.pop_back();
53+
}
54+
deque.push_back((i, x));
55+
ret = ret.max(x);
56+
}
57+
}
58+
59+
ret
60+
}
61+
}
62+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 1425. 带限制的子序列和
2+
给你一个整数数组 `nums` 和一个整数 `k` ,请你返回 **非空** 子序列元素和的最大值,子序列需要满足:子序列中每两个 **相邻** 的整数 `nums[i]``nums[j]` ,它们在原数组中的下标 `i``j` 满足 `i < j``j - i <= k`
3+
4+
数组的子序列定义为:将数组中的若干个数字删除(可以删除 0 个数字),剩下的数字按照原本的顺序排布。
5+
6+
#### 示例 1:
7+
<pre>
8+
<strong>输入:</strong> nums = [10,2,-10,5,20], k = 2
9+
<strong>输出:</strong> 37
10+
<strong>解释:</strong> 子序列为 [10, 2, 5, 20] 。
11+
</pre>
12+
13+
#### 示例 2:
14+
<pre>
15+
<strong>输入:</strong> nums = [-1,-2,-3], k = 1
16+
<strong>输出:</strong> -1
17+
<strong>解释:</strong> 子序列必须是非空的,所以我们选择最大的数字。
18+
</pre>
19+
20+
#### 示例 3:
21+
<pre>
22+
<strong>输入:</strong> nums = [10,-2,-10,-5,20], k = 2
23+
<strong>输出:</strong> 23
24+
<strong>解释:</strong> 子序列为 [10, -2, -5, 20] 。
25+
</pre>
26+
27+
#### 提示:
28+
* <code>1 <= k <= nums.length <= 10<sup>5</sup></code>
29+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
30+
31+
## 题解 (Rust)
32+
33+
### 1. 题解
34+
```Rust
35+
use std::collections::VecDeque;
36+
37+
impl Solution {
38+
pub fn constrained_subset_sum(nums: Vec<i32>, k: i32) -> i32 {
39+
let k = k as usize;
40+
let mut deque = VecDeque::new();
41+
let mut ret = *nums.iter().max().unwrap();
42+
43+
for i in 0..nums.len() {
44+
if i - deque.front().unwrap_or(&(i, 0)).0 > k {
45+
deque.pop_front();
46+
}
47+
48+
let x = nums[i] + deque.front().unwrap_or(&(0, 0)).1;
49+
50+
if x > 0 {
51+
while deque.back().unwrap_or(&(0, i32::MAX)).1 <= x {
52+
deque.pop_back();
53+
}
54+
deque.push_back((i, x));
55+
ret = ret.max(x);
56+
}
57+
}
58+
59+
ret
60+
}
61+
}
62+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::collections::VecDeque;
2+
3+
impl Solution {
4+
pub fn constrained_subset_sum(nums: Vec<i32>, k: i32) -> i32 {
5+
let k = k as usize;
6+
let mut deque = VecDeque::new();
7+
let mut ret = *nums.iter().max().unwrap();
8+
9+
for i in 0..nums.len() {
10+
if i - deque.front().unwrap_or(&(i, 0)).0 > k {
11+
deque.pop_front();
12+
}
13+
14+
let x = nums[i] + deque.front().unwrap_or(&(0, 0)).1;
15+
16+
if x > 0 {
17+
while deque.back().unwrap_or(&(0, i32::MAX)).1 <= x {
18+
deque.pop_back();
19+
}
20+
deque.push_back((i, x));
21+
ret = ret.max(x);
22+
}
23+
}
24+
25+
ret
26+
}
27+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@
826826
[1422][1422l]|[Maximum Score After Splitting a String][1422] |![rs]
827827
[1423][1423l]|[Maximum Points You Can Obtain from Cards][1423] |![rs]
828828
[1424][1424l]|[Diagonal Traverse II][1424] |![rb]
829+
[1425][1425l]|[Constrained Subsequence Sum][1425] |![rs]
829830
[1431][1431l]|[Kids With the Greatest Number of Candies][1431] |![rs]
830831
[1432][1432l]|[Max Difference You Can Get From Changing an Integer][1432] |![rs]
831832
[1433][1433l]|[Check If a String Can Break Another String][1433] |![rs]
@@ -2208,6 +2209,7 @@
22082209
[1422]:Problemset/1422-Maximum%20Score%20After%20Splitting%20a%20String/README.md#1422-maximum-score-after-splitting-a-string
22092210
[1423]:Problemset/1423-Maximum%20Points%20You%20Can%20Obtain%20from%20Cards/README.md#1423-maximum-points-you-can-obtain-from-cards
22102211
[1424]:Problemset/1424-Diagonal%20Traverse%20II/README.md#1424-diagonal-traverse-ii
2212+
[1425]:Problemset/1425-Constrained%20Subsequence%20Sum/README.md#1425-constrained-subsequence-sum
22112213
[1431]:Problemset/1431-Kids%20With%20the%20Greatest%20Number%20of%20Candies/README.md#1431-kids-with-the-greatest-number-of-candies
22122214
[1432]:Problemset/1432-Max%20Difference%20You%20Can%20Get%20From%20Changing%20an%20Integer/README.md#1432-max-difference-you-can-get-from-changing-an-integer
22132215
[1433]:Problemset/1433-Check%20If%20a%20String%20Can%20Break%20Another%20String/README.md#1433-check-if-a-string-can-break-another-string
@@ -3590,6 +3592,7 @@
35903592
[1422l]:https://leetcode.com/problems/maximum-score-after-splitting-a-string/
35913593
[1423l]:https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/
35923594
[1424l]:https://leetcode.com/problems/diagonal-traverse-ii/
3595+
[1425l]:https://leetcode.com/problems/constrained-subsequence-sum/
35933596
[1431l]:https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/
35943597
[1432l]:https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/
35953598
[1433l]:https://leetcode.com/problems/check-if-a-string-can-break-another-string/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@
826826
[1422][1422l]|[分割字符串的最大得分][1422] |![rs]
827827
[1423][1423l]|[可获得的最大点数][1423] |![rs]
828828
[1424][1424l]|[对角线遍历 II][1424] |![rb]
829+
[1425][1425l]|[带限制的子序列和][1425] |![rs]
829830
[1431][1431l]|[拥有最多糖果的孩子][1431] |![rs]
830831
[1432][1432l]|[改变一个整数能得到的最大差值][1432] |![rs]
831832
[1433][1433l]|[检查一个字符串是否可以打破另一个字符串][1433] |![rs]
@@ -2208,6 +2209,7 @@
22082209
[1422]:Problemset/1422-Maximum%20Score%20After%20Splitting%20a%20String/README_CN.md#1422-分割字符串的最大得分
22092210
[1423]:Problemset/1423-Maximum%20Points%20You%20Can%20Obtain%20from%20Cards/README_CN.md#1423-可获得的最大点数
22102211
[1424]:Problemset/1424-Diagonal%20Traverse%20II/README_CN.md#1424-对角线遍历-ii
2212+
[1425]:Problemset/1425-Constrained%20Subsequence%20Sum/README_CN.md#1425-带限制的子序列和
22112213
[1431]:Problemset/1431-Kids%20With%20the%20Greatest%20Number%20of%20Candies/README_CN.md#1431-拥有最多糖果的孩子
22122214
[1432]:Problemset/1432-Max%20Difference%20You%20Can%20Get%20From%20Changing%20an%20Integer/README_CN.md#1432-改变一个整数能得到的最大差值
22132215
[1433]:Problemset/1433-Check%20If%20a%20String%20Can%20Break%20Another%20String/README_CN.md#1433-检查一个字符串是否可以打破另一个字符串
@@ -3590,6 +3592,7 @@
35903592
[1422l]:https://leetcode.cn/problems/maximum-score-after-splitting-a-string/
35913593
[1423l]:https://leetcode.cn/problems/maximum-points-you-can-obtain-from-cards/
35923594
[1424l]:https://leetcode.cn/problems/diagonal-traverse-ii/
3595+
[1425l]:https://leetcode.cn/problems/constrained-subsequence-sum/
35933596
[1431l]:https://leetcode.cn/problems/kids-with-the-greatest-number-of-candies/
35943597
[1432l]:https://leetcode.cn/problems/max-difference-you-can-get-from-changing-an-integer/
35953598
[1433l]:https://leetcode.cn/problems/check-if-a-string-can-break-another-string/

0 commit comments

Comments
 (0)