-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
find-if-array-can-be-sorted.py
66 lines (55 loc) · 1.63 KB
/
find-if-array-can-be-sorted.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
62
63
64
65
66
# Time: O(n)
# Space: O(1)
# sort
class Solution(object):
def canSortArray(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
def popcount(x):
return bin(x).count("1")
left = mx = 0
for right in xrange(len(nums)):
if right+1 != len(nums) and popcount(nums[right+1]) == popcount(nums[right]):
continue
if mx > min(nums[i] for i in xrange(left, right+1)):
return False
mx = max(nums[i] for i in xrange(left, right+1))
left = right+1
return True
# Time: O(n)
# Space: O(n)
import itertools
# sort
class Solution2(object):
def canSortArray(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
def popcount(x):
return bin(x).count("1")
def pairwise(it):
a, b = tee(it)
next(b, None)
return itertools.izip(a, b)
return all(max(a) <= min(b) for a, b in pairwise(list(it) for key, it in groupby(nums, popcount)))
# Time: O(nlogn)
# Space: O(n)
# sort
class Solution3(object):
def canSortArray(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
def popcount(x):
return bin(x).count("1")
left = 0
for right in xrange(len(nums)):
if right+1 != len(nums) and popcount(nums[right+1]) == popcount(nums[right]):
continue
nums[left:right+1] = sorted(nums[left:right+1])
left = right+1
return all(nums[i] <= nums[i+1] for i in xrange(len(nums)-1))