Skip to content

Commit 1af68ac

Browse files
committed
update first_missing_positive
1 parent f0eed3c commit 1af68ac

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

Kotlin/First_Missing_Positive.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Question: Given a list of ints, find the smallest missing positive number
2+
// Solution: Sort the list as you go through by placing every term on its index + 1th place.
3+
// Difficulty: Hard
4+
// Time Complexity: O(n)
5+
// Space Complexity: O(1)
6+
7+
fun firstMissingPositive(nums: IntArray): Int {
8+
var left = 0;
9+
var right = nums.size;
10+
11+
while (left < right) {
12+
// If the current number is equal to its index + 1, skip over as its already sorted
13+
// for example if the number 5 is in the 4th index/ 5th place
14+
if (nums[left] == left + 1) left++;
15+
16+
// Otherwise, if number not in the correct place
17+
// In othe words, if a number n is within our range and the number in the nth position isnt equal to this number, swap the two
18+
else if (nums[left] > 0 && nums[left] <= nums.size && nums[left] != nums[nums[left]-1]) {
19+
nums[nums[left]-1] = nums[left].also{ nums[left] = nums[nums[left]-1] };
20+
}
21+
22+
// Case if the number is a duplicate, or out of our range (1 to right). So place it at the end
23+
else nums[left] = nums[--right].also{ nums[right] = nums[left] };
24+
}
25+
return right + 1;
26+
}
27+
28+
fun main(args: Array<String>) {
29+
var arr: IntArray = intArrayOf(7,8,9,11,12);
30+
print(firstMissingPositive(arr));
31+
}

Python/First_Missing_Positive.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
#Question: Given a list of ints, find the smallest missing positive number
22
#Solution: Sort the list as you go through by placing every term on its index + 1th place.
33
#Difficulty: Hard
4+
#Time Complexity: O(n)
5+
#Space Complexity: O(1)
46

57
from typing import List
68

79
def firstMissingPositive(nums: List[int]) -> int:
8-
i, j = 0, len(nums)
9-
while i < j:
10+
i, j = 0, len(nums)
11+
while i < j:
1012

11-
#If the current number is equal to its index + 1, skip over as its already sorted
12-
#ie if the number 5 is in the 4th index/ 5th place
13-
if nums[i] == i+1: i += 1
13+
#If the current number is equal to its index + 1, skip over as its already sorted
14+
#for example if the number 5 is in the 4th index/ 5th place
15+
if nums[i] == i+1: i += 1
1416

15-
#Regular case, ie a number not in the correct place
16-
elif nums[i] > 0 and nums[i] <= j and nums[i] != nums[nums[i]-1]:
17-
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
17+
#Regular case, ie a number not in the correct place
18+
#In othe words, if a number n is within our range and the number in the nth position isnt equal to this number, swap the two
19+
elif nums[i] > 0 and nums[i] <= j and nums[nums[i]-1] != nums[i]:
20+
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
1821

19-
#Case if the number is a duplicate, or out of our range (1 to j). So place it at the end
20-
else:
21-
j -= 1
22-
nums[j], nums[i] = nums[i], nums[j]
22+
#Case if the number is a duplicate, or out of our range (1 to j). So place it at the end
23+
else:
24+
j -= 1
25+
nums[j], nums[i] = nums[i], nums[j]
2326

24-
return j + 1
27+
return j + 1
2528

2629
def main():
2730
print(firstMissingPositive([1, 5, 4, 3, 2, 7, 0]))

0 commit comments

Comments
 (0)