Skip to content

Commit ec930a1

Browse files
committed
+ problem 1673
1 parent 05961fd commit ec930a1

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 1673. Find the Most Competitive Subsequence
2+
Given an integer array `nums` and a positive integer `k`, return *the most **competitive** subsequence of* `nums` *of size* `k`.
3+
4+
An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.
5+
6+
We define that a subsequence `a` is more **competitive** than a subsequence `b` (of the same length) if in the first position where `a` and `b` differ, subsequence `a` has a number **less** than the corresponding number in `b`. For example, `[1,3,4]` is more competitive than `[1,3,5]` because the first position they differ is at the final number, and `4` is less than `5`.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> nums = [3,5,2,6], k = 2
11+
<strong>Output:</strong> [2,6]
12+
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
13+
</pre>
14+
15+
#### Example 2:
16+
<pre>
17+
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
18+
<strong>Output:</strong> [2,3,3,4]
19+
</pre>
20+
21+
#### Constraints:
22+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
23+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
24+
* `1 <= k <= nums.length`
25+
26+
## Solutions (Rust)
27+
28+
### 1. Solution
29+
```Rust
30+
impl Solution {
31+
pub fn most_competitive(nums: Vec<i32>, k: i32) -> Vec<i32> {
32+
let k = k as usize;
33+
let mut ret = vec![];
34+
35+
for i in 0..nums.len() {
36+
while let Some(&num) = ret.last() {
37+
if num > nums[i] && k - ret.len() < nums.len() - i {
38+
ret.pop();
39+
} else {
40+
break;
41+
}
42+
}
43+
44+
if ret.len() < k {
45+
ret.push(nums[i]);
46+
}
47+
}
48+
49+
ret
50+
}
51+
}
52+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 1673. 找出最具竞争力的子序列
2+
给你一个整数数组 `nums` 和一个正整数 `k` ,返回长度为 `k` 且最具 **竞争力**`nums` 子序列。
3+
4+
数组的子序列是从数组中删除一些元素(可能不删除元素)得到的序列。
5+
6+
在子序列 `a` 和子序列 `b` 第一个不相同的位置上,如果 `a` 中的数字小于 `b` 中对应的数字,那么我们称子序列 `a` 比子序列 `b`(相同长度下)更具 **竞争力** 。 例如,`[1,3,4]``[1,3,5]` 更具竞争力,在第一个不相同的位置,也就是最后一个位置上, `4` 小于 `5`
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> nums = [3,5,2,6], k = 2
11+
<strong>输出:</strong> [2,6]
12+
<strong>解释:</strong> 在所有可能的子序列集合 {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]} 中,[2,6] 最具竞争力。
13+
</pre>
14+
15+
#### 示例 2:
16+
<pre>
17+
<strong>输入:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
18+
<strong>输出:</strong> [2,3,3,4]
19+
</pre>
20+
21+
#### 提示:
22+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
23+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
24+
* `1 <= k <= nums.length`
25+
26+
## 题解 (Rust)
27+
28+
### 1. 题解
29+
```Rust
30+
impl Solution {
31+
pub fn most_competitive(nums: Vec<i32>, k: i32) -> Vec<i32> {
32+
let k = k as usize;
33+
let mut ret = vec![];
34+
35+
for i in 0..nums.len() {
36+
while let Some(&num) = ret.last() {
37+
if num > nums[i] && k - ret.len() < nums.len() - i {
38+
ret.pop();
39+
} else {
40+
break;
41+
}
42+
}
43+
44+
if ret.len() < k {
45+
ret.push(nums[i]);
46+
}
47+
}
48+
49+
ret
50+
}
51+
}
52+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn most_competitive(nums: Vec<i32>, k: i32) -> Vec<i32> {
3+
let k = k as usize;
4+
let mut ret = vec![];
5+
6+
for i in 0..nums.len() {
7+
while let Some(&num) = ret.last() {
8+
if num > nums[i] && k - ret.len() < nums.len() - i {
9+
ret.pop();
10+
} else {
11+
break;
12+
}
13+
}
14+
15+
if ret.len() < k {
16+
ret.push(nums[i]);
17+
}
18+
}
19+
20+
ret
21+
}
22+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@
929929
[1669][1669l]|[Merge In Between Linked Lists][1669] |![rb]
930930
[1670][1670l]|[Design Front Middle Back Queue][1670] |![rs]
931931
[1672][1672l]|[Richest Customer Wealth][1672] |![rb]&nbsp;&nbsp;![rs]
932+
[1673][1673l]|[Find the Most Competitive Subsequence][1673] |![rs]
932933
[1678][1678l]|[Goal Parser Interpretation][1678] |![rs]
933934
[1679][1679l]|[Max Number of K-Sum Pairs][1679] |![rs]
934935
[1684][1684l]|[Count the Number of Consistent Strings][1684] |![rs]
@@ -2287,6 +2288,7 @@
22872288
[1669]:Problemset/1669-Merge%20In%20Between%20Linked%20Lists/README.md#1669-merge-in-between-linked-lists
22882289
[1670]:Problemset/1670-Design%20Front%20Middle%20Back%20Queue/README.md#1670-design-front-middle-back-queue
22892290
[1672]:Problemset/1672-Richest%20Customer%20Wealth/README.md#1672-richest-customer-wealth
2291+
[1673]:Problemset/1673-Find%20the%20Most%20Competitive%20Subsequence/README.md#1673-find-the-most-competitive-subsequence
22902292
[1678]:Problemset/1678-Goal%20Parser%20Interpretation/README.md#1678-goal-parser-interpretation
22912293
[1679]:Problemset/1679-Max%20Number%20of%20K-Sum%20Pairs/README.md#1679-max-number-of-k-sum-pairs
22922294
[1684]:Problemset/1684-Count%20the%20Number%20of%20Consistent%20Strings/README.md#1684-count-the-number-of-consistent-strings
@@ -3648,6 +3650,7 @@
36483650
[1669l]:https://leetcode.com/problems/merge-in-between-linked-lists/
36493651
[1670l]:https://leetcode.com/problems/design-front-middle-back-queue/
36503652
[1672l]:https://leetcode.com/problems/richest-customer-wealth/
3653+
[1673l]:https://leetcode.com/problems/find-the-most-competitive-subsequence/
36513654
[1678l]:https://leetcode.com/problems/goal-parser-interpretation/
36523655
[1679l]:https://leetcode.com/problems/max-number-of-k-sum-pairs/
36533656
[1684l]:https://leetcode.com/problems/count-the-number-of-consistent-strings/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@
929929
[1669][1669l]|[合并两个链表][1669] |![rb]
930930
[1670][1670l]|[设计前中后队列][1670] |![rs]
931931
[1672][1672l]|[最富有客户的资产总量][1672] |![rb]&nbsp;&nbsp;![rs]
932+
[1673][1673l]|[找出最具竞争力的子序列][1673] |![rs]
932933
[1678][1678l]|[设计 Goal 解析器][1678] |![rs]
933934
[1679][1679l]|[K 和数对的最大数目][1679] |![rs]
934935
[1684][1684l]|[统计一致字符串的数目][1684] |![rs]
@@ -2287,6 +2288,7 @@
22872288
[1669]:Problemset/1669-Merge%20In%20Between%20Linked%20Lists/README_CN.md#1669-合并两个链表
22882289
[1670]:Problemset/1670-Design%20Front%20Middle%20Back%20Queue/README_CN.md#1670-设计前中后队列
22892290
[1672]:Problemset/1672-Richest%20Customer%20Wealth/README_CN.md#1672-最富有客户的资产总量
2291+
[1673]:Problemset/1673-Find%20the%20Most%20Competitive%20Subsequence/README_CN.md#1673-找出最具竞争力的子序列
22902292
[1678]:Problemset/1678-Goal%20Parser%20Interpretation/README_CN.md#1678-设计-goal-解析器
22912293
[1679]:Problemset/1679-Max%20Number%20of%20K-Sum%20Pairs/README_CN.md#1679-k-和数对的最大数目
22922294
[1684]:Problemset/1684-Count%20the%20Number%20of%20Consistent%20Strings/README_CN.md#1684-统计一致字符串的数目
@@ -3648,6 +3650,7 @@
36483650
[1669l]:https://leetcode.cn/problems/merge-in-between-linked-lists/
36493651
[1670l]:https://leetcode.cn/problems/design-front-middle-back-queue/
36503652
[1672l]:https://leetcode.cn/problems/richest-customer-wealth/
3653+
[1673l]:https://leetcode.cn/problems/find-the-most-competitive-subsequence/
36513654
[1678l]:https://leetcode.cn/problems/goal-parser-interpretation/
36523655
[1679l]:https://leetcode.cn/problems/max-number-of-k-sum-pairs/
36533656
[1684l]:https://leetcode.cn/problems/count-the-number-of-consistent-strings/

0 commit comments

Comments
 (0)