-
Notifications
You must be signed in to change notification settings - Fork 0
/
0041.py
61 lines (54 loc) · 1.61 KB
/
0041.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Source: https://leetcode.com/problems/first-missing-positive
# Title: First Missing Positive
# Difficulty: Hard
# Author: Mu Yang <http://muyang.pro>
################################################################################################################################
# Given an unsorted integer array nums, find the smallest missing positive integer.
#
# Follow up: Could you implement an algorithm that runs in O(n) time and uses constant extra space.?
#
# Example 1:
#
# Input: nums = [1,2,0]
# Output: 3
#
# Example 2:
#
# Input: nums = [3,4,-1,1]
# Output: 2
#
# Example 3:
#
# Input: nums = [7,8,9,11,12]
# Output: 1
#
# Constraints:
#
# 0 <= nums.length <= 300
# -2^31 <= nums[i] <= 2^31 - 1
#
################################################################################################################################
class Solution:
"""
Idea:
1. let l be the length of the array
2. the missing value must be in [1, l]
3. use this array to store if each value is exist (+: not exist, -: exist)
"""
def firstMissingPositive(self, nums: List[int]) -> int:
# Get length
l = len(nums)
# Set all negative to l+1 (should be ignored)
for i, n in enumerate(nums):
if n <= 0:
nums[i] = l+1
# Flip signs
for i, n in enumerate(nums):
an = abs(n)
if an <= l:
nums[an-1] = -abs(nums[an-1])
# Find index of the positive value
for i, n in enumerate(nums):
if n > 0:
return i+1
return l+1