-
Notifications
You must be signed in to change notification settings - Fork 43
/
delete-columns-to-make-sorted.py
49 lines (46 loc) · 1.09 KB
/
delete-columns-to-make-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
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/84206638
class Solution:
def minDeletionSize(self, A):
"""
:type A: List[str]
:rtype: int
"""
res = 0
N = len(A[0])
for i in range(N):
col = [a[i] for a in A]
if col != sorted(col):
res += 1
return res
# V2
# Time: O(n * l)
# Space: O(1)
class Solution(object):
def minDeletionSize(self, A):
"""
:type A: List[str]
:rtype: int
"""
result = 0
for c in range(len(A[0])):
for r in range(1, len(A)):
if A[r-1][c] > A[r][c]:
result += 1
break
return result
# Time: O(n * l)
# Space: O(n)
import itertools
class Solution2(object):
def minDeletionSize(self, A):
"""
:type A: List[str]
:rtype: int
"""
result = 0
for col in itertools.izip(*A):
if any(col[i] > col[i+1] for i in range(len(col)-1)):
result += 1
return result