Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b737104

Browse files
committed
Add solution #1673
1 parent c6f83d8 commit b737104

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,464 LeetCode solutions in JavaScript
1+
# 1,465 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1289,6 +1289,7 @@
12891289
1670|[Design Front Middle Back Queue](./solutions/1670-design-front-middle-back-queue.js)|Medium|
12901290
1671|[Minimum Number of Removals to Make Mountain Array](./solutions/1671-minimum-number-of-removals-to-make-mountain-array.js)|Hard|
12911291
1672|[Richest Customer Wealth](./solutions/1672-richest-customer-wealth.js)|Easy|
1292+
1673|[Find the Most Competitive Subsequence](./solutions/1673-find-the-most-competitive-subsequence.js)|Medium|
12921293
1679|[Max Number of K-Sum Pairs](./solutions/1679-max-number-of-k-sum-pairs.js)|Medium|
12931294
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
12941295
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 1673. Find the Most Competitive Subsequence
3+
* https://leetcode.com/problems/find-the-most-competitive-subsequence/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums and a positive integer k, return the most competitive subsequence
7+
* of nums of size k.
8+
*
9+
* An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements
10+
* from the array.
11+
*
12+
* We define that a subsequence a is more competitive than a subsequence b (of the same length) if
13+
* in the first position where a and b differ, subsequence a has a number less than the
14+
* corresponding number in b. For example, [1,3,4] is more competitive than [1,3,5] because the
15+
* first position they differ is at the final number, and 4 is less than 5.
16+
*/
17+
18+
/**
19+
* @param {number[]} nums
20+
* @param {number} k
21+
* @return {number[]}
22+
*/
23+
var mostCompetitive = function(nums, k) {
24+
const result = [];
25+
const n = nums.length;
26+
27+
for (let i = 0; i < n; i++) {
28+
while (result.length && result[result.length - 1] > nums[i] && result.length + n - i > k) {
29+
result.pop();
30+
}
31+
if (result.length < k) {
32+
result.push(nums[i]);
33+
}
34+
}
35+
36+
return result;
37+
};

0 commit comments

Comments
 (0)