Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created September 9, 2022 13:15
Show Gist options
  • Select an option

  • Save codecakes/84605f3814143bd06372cba9bc1e94c3 to your computer and use it in GitHub Desktop.

Select an option

Save codecakes/84605f3814143bd06372cba9bc1e94c3 to your computer and use it in GitHub Desktop.
First missing positive number in an array
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