forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-columns-to-make-sorted-ii.py
40 lines (37 loc) · 1.03 KB
/
delete-columns-to-make-sorted-ii.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
# Time: O(n * l)
# Space: O(n)
class Solution(object):
def minDeletionSize(self, A):
"""
:type A: List[str]
:rtype: int
"""
result = 0
unsorted = set(range(len(A)-1))
for j in xrange(len(A[0])):
if any(A[i][j] > A[i+1][j] for i in unsorted):
result += 1
else:
unsorted -= set(i for i in unsorted if A[i][j] < A[i+1][j])
return result
# Time: O(n * m)
# Space: O(n)
class Solution2(object):
def minDeletionSize(self, A):
"""
:type A: List[str]
:rtype: int
"""
result = 0
is_sorted = [False]*(len(A)-1)
for j in xrange(len(A[0])):
tmp = is_sorted[:]
for i in xrange(len(A)-1):
if A[i][j] > A[i+1][j] and tmp[i] == False:
result += 1
break
if A[i][j] < A[i+1][j]:
tmp[i] = True
else:
is_sorted = tmp
return result