forked from lilianweng/LeetcodePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_missing_pos.py
More file actions
52 lines (43 loc) · 1.16 KB
/
first_missing_pos.py
File metadata and controls
52 lines (43 loc) · 1.16 KB
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
#!/usr/bin/env python
'''
Leetcode: First missing positive
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 uses constant space.
'''
from __future__ import division
import random
## Use bitmap
def first_miss_pos(L):
bitmap = 0
for x in L:
if x <= 0: continue
bitmap |= (1 << (x-1))
print L, bin(bitmap)
pos = 0
while bitmap > 0:
bitmap,r = divmod(bitmap, 2)
pos += 1
if r == 0: return pos
return pos+1
## Switch x to pos x, amortized time ~O(n)
def first_miss_pos2(L):
n = len(L)
print L, '-->',
for i in range(len(L)):
while L[i] > 0 and L[i] < n and L[i] != L[L[i]]:
# switch tmp to postition tmp if it is valid (1 to n-1)
tmp = L[i]
L[i] = L[tmp]
L[tmp] = tmp
if L[i] >= n: L[i] = -1
print L
for i in range(1,n):
if L[i] < 0: return i
return n
if __name__ == '__main__':
print first_miss_pos2([1,2,0])
print first_miss_pos2([1,2,3])
print first_miss_pos2([3,4,-1,1])