Skip to content

Commit 773623c

Browse files
chlng
1 parent 22e8ed5 commit 773623c

File tree

5 files changed

+106
-4
lines changed

5 files changed

+106
-4
lines changed

Challenges/2021/January-LeetCoding-Challenge.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ None
4141
|1679.|[Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/)|[Python](/Medium/1679.MaxNumberofK-SumPairs.py)|Medium|
4242
|5.|[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)|[Python](Medium/5.LongestPalindromicSubstring(bruteforce).py)|Medium|
4343
|20.|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)|[Python](/Easy/20.ValidParentheses(Stack).py)|Easy|
44-
44+
|1673.|[Find the Most Competitive Subsequence](https://leetcode.com/problems/find-the-most-competitive-subsequence/)|[Python](/Medium/1673.FindtheMostCompetitiveSubsequence.py)|Medium|
4545

4646
## License
4747
The code is open-source and licensed under the [MIT License](/LICENSE).

Challenges/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
- [December LeetCoding Challenge](/Challenges/2020/December-LeetCoding-Challenge.md) - 27/31
88

99
2021:
10-
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 18/31
10+
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 19/31
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Given an integer array nums and a positive integer k,
3+
return the most competitive subsequence of nums of size
4+
k.
5+
6+
An array's subsequence is a resulting sequence obtained
7+
by erasing some (possibly zero) elements from the array.
8+
9+
We define that a subsequence a is more competitive than
10+
a subsequence b (of the same length) if in the first
11+
position where a and b differ, subsequence a has a
12+
number less than the corresponding number in b. For
13+
example, [1,3,4] is more competitive than [1,3,5]
14+
because the first position they differ is at the final
15+
number, and 4 is less than 5.
16+
17+
Example:
18+
Input: nums = [3,5,2,6], k = 2
19+
Output: [2,6]
20+
Explanation: Among the set of every possible subsequence:
21+
{[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]},
22+
[2,6] is the most competitive.
23+
24+
Example:
25+
Input: nums = [2,4,3,3,5,4,9,6], k = 4
26+
Output: [2,3,3,4]
27+
28+
Constraints:
29+
- 1 <= nums.length <= 10^5
30+
- 0 <= nums[i] <= 10^9
31+
- 1 <= k <= nums.length
32+
"""
33+
#Difficulty: Medium
34+
#86 / 86 test cases passed.
35+
#Runtime: 1256 ms
36+
#Memory Usage: 27.1 MB
37+
38+
#Runtime: 1256 ms, faster than 78.12% of Python3 online submissions for Find the Most Competitive Subsequence.
39+
#Memory Usage: 27.1 MB, less than 81.07% of Python3 online submissions for Find the Most Competitive Subsequence.
40+
41+
class Solution:
42+
def mostCompetitive(self, nums: List[int], k: int) -> List[int]:
43+
length = len(nums) - k
44+
stack = []
45+
for num in nums:
46+
while stack and stack[-1] > num and length:
47+
stack.pop()
48+
length -= 1
49+
stack.append(num)
50+
return stack[:k]

Medium/402.RemoveKDigits.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Given a non-negative integer num represented as a string,
3+
remove k digits from the number so that the new number
4+
is the smallest possible.
5+
6+
Note:
7+
- The length of num is less than 10002 and will
8+
be ≥ k.
9+
- The given num does not contain any leading zero.
10+
11+
Example:
12+
Input: num = "1432219", k = 3
13+
Output: "1219"
14+
Explanation: Remove the three digits 4, 3, and 2 to
15+
form the new number 1219 which is the
16+
smallest.
17+
18+
Example:
19+
Input: num = "10200", k = 1
20+
Output: "200"
21+
Explanation: Remove the leading 1 and the number is 200.
22+
Note that the output must not contain
23+
leading zeroes.
24+
25+
Example:
26+
Input: num = "10", k = 2
27+
Output: "0"
28+
Explanation: Remove all the digits from the number and
29+
it is left with nothing which is 0.
30+
'''
31+
#Difficlty: Medium
32+
#33 / 33 test cases passed.
33+
#Runtime: 32 ms
34+
#Memory Usage: 14.1 MB
35+
36+
#Runtime: 32 ms, faster than 92.66% of Python3 online submissions for Remove K Digits.
37+
#Memory Usage: 14.1 MB, less than 99.94% of Python3 online submissions for Remove K Digits.
38+
39+
class Solution:
40+
def removeKdigits(self, num: str, k: int) -> str:
41+
length = len(num) - k
42+
stack = []
43+
for n in num:
44+
while stack and k and stack[-1] > n:
45+
stack.pop()
46+
k -= 1
47+
stack.append(n)
48+
while stack and stack[0] == '0':
49+
stack.pop(0)
50+
return ''.join(stack)[:length] if stack and length else '0'

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python solutions of LeetCode problems.
22
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
3-
![Problems Solved](https://img.shields.io/badge/problems%20solved-496%2F1571-orange)&nbsp;
3+
![Problems Solved](https://img.shields.io/badge/problems%20solved-499%2F1571-orange)&nbsp;
44
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
55
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
66
<br><br>
@@ -19,7 +19,7 @@ In this repository provided my Python solutions of LeetCode problems.
1919
- [December LeetCoding Challenge](/Challenges/2020/December-LeetCoding-Challenge.md) - 27/31
2020

2121
2021:
22-
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 18/31
22+
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 19/31
2323
<br><br>
2424
## Solutions
2525
*P.S. If you like this, please leave me a star.*
@@ -209,6 +209,7 @@ In this repository provided my Python solutions of LeetCode problems.
209209
|395.|[Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/)|[Python](/Medium/395.LongestSubstringwithAtLeastKRepeatingCharacters.py)|Medium|`collections`, `Recursion`|
210210
|398.|[Random Pick Index](https://leetcode.com/problems/random-pick-index/)|[Python](/Medium/398.RandomPickIndex.py)|Medium|`random.choice()`|
211211
|399.|[Evaluate Division](https://leetcode.com/problems/evaluate-division/)|[Python](/Medium/399.EvaluateDivision.py)|Medium|God bless youtube|
212+
|402.|[Remove K Digits](https://leetcode.com/problems/remove-k-digits/)|[Python](/Medium/402.RemoveKDigits.py)|Medium|`Stack`|
212213
|404.|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/)|[Python](/Easy/404.SumofLeftLeaves.py)|Easy|`Recursion`, preorder|
213214
|409.|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome/)|[Python](/Easy/409.LongestPalindrome.py)|Easy||
214215
|412.|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)|[Python](/Easy/412.FizzBuzz.py)|Easy||
@@ -524,6 +525,7 @@ In this repository provided my Python solutions of LeetCode problems.
524525
|1669.|[Merge In Between Linked Lists](https://leetcode.com/problems/merge-in-between-linked-lists/)|[Python](/Medium/1669.MergeInBetweenLinkedLists.py)|Medium|`Linked List`|
525526
|1670.|[Design Front Middle Back Queue](https://leetcode.com/problems/design-front-middle-back-queue/)|[Python](/Medium/1670.DesignFrontMiddleBackQueue.py)|Medium|`List`|
526527
|1672.|[Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/)|[Python](/Easy/1672.RichestCustomerWealth.py)|Easy||
528+
|1673.|[Find the Most Competitive Subsequence](https://leetcode.com/problems/find-the-most-competitive-subsequence/)|[Python](/Medium/1673.FindtheMostCompetitiveSubsequence.py)|Medium|`Stack`|
527529
|1678.|[Goal Parser Interpretation](https://leetcode.com/problems/goal-parser-interpretation/)|[Python](/Easy/1678.GoalParserInterpretation(replace).py)|Easy|`.replace()`|
528530
|1678.|[Goal Parser Interpretation](https://leetcode.com/problems/goal-parser-interpretation/)|[Python](/Easy/1678.GoalParserInterpretation.py)|Easy|iteratively|
529531
|1679.|[Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/)|[Python](/Medium/1679.MaxNumberofK-SumPairs.py)|Medium|`collections`, `Counter`|

0 commit comments

Comments
 (0)