patternpythonMinor
Given an unsorted integer array, find the first missing positive integer
Viewed 0 times
themissingarrayunsortedfirstfindpositivegiveninteger
Problem
Here is the problem, any advice about code improvement, more efficient implementation in terms of time complexity, functional bugs, etc. are appreciated.
Given an unsorted integer array, find the first missing positive
integer.
For example,
Given
and
Your algorithm should run in O(n) time and use constant space.
Given an unsorted integer array, find the first missing positive
integer.
For example,
Given
[1,2,0] return 3,and
[3,4,-1,1] return 2.Your algorithm should run in O(n) time and use constant space.
def firstMissingPositive(nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 1
for i,v in enumerate(nums):
if v > len(nums):
nums[i]=-1
elif v len(nums) or nums[i] <=0:
nums[i] = -1
for i,v in enumerate(nums):
if nums[i] != i+1:
return i+1
return len(nums)+1
if __name__ == "__main__":
print firstMissingPositive([1,2,0])
print firstMissingPositive([3,4,-1,1])Solution
PEP8
Python uses underscore as naming separator in function and variable names, see PEP8
Naming
It's better to use some obvious names so instead of
Improvements
You've got a right idea on how to solve this, but there are some minor things in your implementation that can be improved.
this part can be simplified to
Now your while loop can make infinite number of cycles on such list
So in the end your code should look like this:
Python uses underscore as naming separator in function and variable names, see PEP8
Naming
for i,v in enumerate(nums)It's better to use some obvious names so instead of
i,v you should use index, valueImprovements
You've got a right idea on how to solve this, but there are some minor things in your implementation that can be improved.
for i,v in enumerate(nums):
if v > len(nums):
nums[i]=-1
elif v <= 0:
nums[i]=-1
else:this part can be simplified to
if 0 >= value > len(nums):
continueNow your while loop can make infinite number of cycles on such list
[3,4,3,-1] so you need to handle this, also you don't have to replace items that are = len(nums) with -1 you can just skip them.So in the end your code should look like this:
def first_missing_positive(nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 1
for index, value in enumerate(nums):
if len(nums) < value <= 0:
continue
while index + 1 != nums[index] and 0 < nums[index] <= len(nums):
v = nums[index]
nums[index], nums[v-1] = nums[v-1], nums[index]
nums[v-1] = v
# Don't create infinite loops
if nums[index] == nums[v-1]:
break
for index, value in enumerate(nums, 1):
if value != index:
return index
return len(nums) + 1Code Snippets
for i,v in enumerate(nums)for i,v in enumerate(nums):
if v > len(nums):
nums[i]=-1
elif v <= 0:
nums[i]=-1
else:if 0 >= value > len(nums):
continuedef first_missing_positive(nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 1
for index, value in enumerate(nums):
if len(nums) < value <= 0:
continue
while index + 1 != nums[index] and 0 < nums[index] <= len(nums):
v = nums[index]
nums[index], nums[v-1] = nums[v-1], nums[index]
nums[v-1] = v
# Don't create infinite loops
if nums[index] == nums[v-1]:
break
for index, value in enumerate(nums, 1):
if value != index:
return index
return len(nums) + 1Context
StackExchange Code Review Q#149270, answer score: 3
Revisions (0)
No revisions yet.