Skip to content

Commit 6369f14

Browse files
authored
Merge pull request neetcode-gh#1218 from UdayGarg/41-First-Missing-Positive
Create: 41-First-Missing-Positive.py
2 parents f2a5095 + 26554e3 commit 6369f14

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def firstMissingPositive(self, nums: List[int]) -> int:
3+
A = nums
4+
for i in range(len(A)):
5+
if A[i] < 0:
6+
A[i] = 0
7+
8+
for i in range(len(A)):
9+
val = abs(A[i])
10+
if 1 <= val <= len(A):
11+
if A[val - 1] > 0:
12+
A[val - 1] *= -1
13+
elif A[val - 1] == 0:
14+
A[val - 1] = -1 * (len(A) + 1)
15+
16+
for i in range( 1, len(A)+ 1):
17+
if A[i -1] >= 0:
18+
return i
19+
20+
return len(A) + 1
21+
22+
def firstMissingPositive_2(self, nums: List[int]) -> int:
23+
new = set(nums)
24+
i = 1
25+
while i in new:
26+
i += 1
27+
return i

0 commit comments

Comments
 (0)