-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathDelete Columns to Make Sorted II.py
50 lines (40 loc) · 1.67 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
41
42
43
44
45
46
47
48
49
50
class Solution:
def minDeletionSize(self, strs: List[str]) -> int:
n = len(strs)
col_size = len(strs[0])
# a b c d e f g h i j k l m n o p q r s t u v w x y z
i = 0
ans = 0
def getRemoved(idx):
# removing the idx column
for x in range(n):
strs[x] = strs[x][:idx] + strs[x][idx+1:]
while i < col_size:
tmp = strs[0][:i+1]
flag = True
similar = False
for j in range(1,n):
if strs[j][:i+1] < tmp :
# previous element is larger ( unsorted )
flag = False
break
elif strs[j][:i+1] > tmp :
# previous element is smaller ( sorted )
tmp = strs[j][:i+1]
else:
# previous element is equal ( not clear )
tmp = strs[j][:i+1]
similar = True
if flag == True and similar == False:
# all are sorted and we are ready to return ans
return ans
elif flag == True and similar == True:
# all are sorted but can't be decided for further columns. check for next col
i += 1
elif flag == False:
# unsorted column = removal
getRemoved(i)
# increment the answer and since we removed i th col decrement col_size
ans += 1
col_size -= 1
return ans