forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
67 lines (51 loc) · 1.61 KB
/
main2.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
/// Source : https://leetcode.com/problems/delete-columns-to-make-sorted-ii/
/// Author : liuyubobobo
/// Time : 2018-12-16
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
/// Greedy and check in place
/// Time Complexity: O(m * n)
/// Space Complexity: O(m)
class Solution {
public:
int minDeletionSize(vector<string>& A) {
int m = A.size(), n = A[0].size(), res = 0;
vector<bool> check(m - 1, false);
for(int j = 0; j < n; j ++){
bool ok = true;
for(int i = 1; i < m; i ++)
if(!check[i - 1] && A[i][j] < A[i - 1][j]){
ok = false;
break;
}
if(ok){
for(int i = 1; i < m; i ++)
if(!check[i - 1] && A[i][j] > A[i - 1][j])
check[i - 1] = true;
}
else
res ++;
}
return res;
}
};
int main() {
vector<string> A1 = {"ca","bb","ac"};
cout << Solution().minDeletionSize(A1) << endl;
// 1
vector<string> A2 = {"xc","yb","za"};
cout << Solution().minDeletionSize(A2) << endl;
// 0
vector<string> A3 = {"zyx","wvu","tsr"};
cout << Solution().minDeletionSize(A3) << endl;
// 3
vector<string> A4 = {"xga","xfb","yfa"};
cout << Solution().minDeletionSize(A4) << endl;
//1
vector<string> A5 = {"bwwdyeyfhc","bchpphbtkh","hmpudwfkpw","lqeoyqkqwe","riobghmpaa","stbheblgao","snlaewujlc","tqlzolljas","twdkexzvfx","wacnnhjdis"};
cout << Solution().minDeletionSize(A5) << endl;
//4
return 0;
}