Skip to content

Commit 4b10648

Browse files
authored
Update 1673-find-the-most-competitive-subsequence.js
1 parent 4b5d92b commit 4b10648

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1673-find-the-most-competitive-subsequence.js

+24
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,27 @@ const mostCompetitive = function (nums, k) {
4343
}
4444
return stack
4545
}
46+
47+
// another
48+
49+
/**
50+
* @param {number[]} nums
51+
* @param {number} k
52+
* @return {number[]}
53+
*/
54+
const mostCompetitive = function (nums, k) {
55+
const n = nums.length, stack = []
56+
for(let i = 0; i < n; i++) {
57+
const ch = nums[i]
58+
while(
59+
stack.length &&
60+
ch < stack[stack.length - 1] &&
61+
stack.length + (n - 1 - i) >= k
62+
) {
63+
stack.pop()
64+
}
65+
if(stack.length < k) stack.push(ch)
66+
}
67+
return stack
68+
}
69+

0 commit comments

Comments
 (0)