Created
September 9, 2022 13:15
-
-
Save codecakes/84605f3814143bd06372cba9bc1e94c3 to your computer and use it in GitHub Desktop.
First missing positive number in an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def firstMissingPositive(self, nums: List[int]) -> int: | |
| # filter non negative, new size n | |
| arr = sorted(filter(lambda x: x > 0, nums)) | |
| count = len(arr) | |
| # filter all num <= n to a hash set and keep new count, count. | |
| arr = filter(lambda x: x <= count, set(arr)) | |
| greater_positive_num = 1 | |
| for num in arr: | |
| if greater_positive_num == num: | |
| greater_positive_num += 1 | |
| else: | |
| break | |
| return greater_positive_num |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment