-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path955.delete-columns-to-make-sorted-ii.cpp
118 lines (116 loc) · 2.42 KB
/
955.delete-columns-to-make-sorted-ii.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* @lc app=leetcode id=955 lang=cpp
*
* [955] Delete Columns to Make Sorted II
*
* https://leetcode.com/problems/delete-columns-to-make-sorted-ii/description/
*
* algorithms
* Medium (27.09%)
* Total Accepted: 2.2K
* Total Submissions: 7.9K
* Testcase Example: '["ca","bb","ac"]'
*
* We are given an array A of N lowercase letter strings, all of the same
* length.
*
* Now, we may choose any set of deletion indices, and for each string, we
* delete all the characters in those indices.
*
* For example, if we have an array A = ["abcdef","uvwxyz"] and deletion
* indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"].
*
* Suppose we chose a set of deletion indices D such that after deletions, the
* final array has its elements in lexicographic order (A[0] <= A[1] <= A[2]
* ... <= A[A.length - 1]).
*
* Return the minimum possible value of D.length.
*
*
*
*
*
*
*
*
*
*
*
* Example 1:
*
*
* Input: ["ca","bb","ac"]
* Output: 1
* Explanation:
* After deleting the first column, A = ["a", "b", "c"].
* Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]).
* We require at least 1 deletion since initially A was not in lexicographic
* order, so the answer is 1.
*
*
*
* Example 2:
*
*
* Input: ["xc","yb","za"]
* Output: 0
* Explanation:
* A is already in lexicographic order, so we don't need to delete anything.
* Note that the rows of A are not necessarily in lexicographic order:
* ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...)
*
*
*
* Example 3:
*
*
* Input: ["zyx","wvu","tsr"]
* Output: 3
* Explanation:
* We have to delete every column.
*
*
*
*
*
*
* Note:
*
*
* 1 <= A.length <= 100
* 1 <= A[i].length <= 100
*
*
*
*
*
*
*
*/
class Solution {
public:
int minDeletionSize(vector<string>& A) {
int count = 0;
int n = A.size();
int m = A[0].size();
bool same[n]; fill_n(same, n, true);
for(int i=0;i<m;i++){
bool charAt_i_Ok = true;
vector<int> tp;
for(int j=0;j<n-1;j++){
if(same[j] && A[j][i] > A[j+1][i]){
charAt_i_Ok = false;
}else if(same[j] && A[j][i] < A[j+1][i]){
same[j] = false;
tp.push_back(j);
}
}
if(!charAt_i_Ok){
for(auto e: tp)
same[e] = true;
count++;
}
}
return count;
}
};