forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
69 lines (52 loc) · 1.56 KB
/
main.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
/// Source : https://leetcode.com/problems/delete-columns-to-make-sorted-ii/
/// Author : liuyubobobo
/// Time : 2018-12-12
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
/// Greedy
/// Time Complexity: O(m * m * n)
/// Space Complexity: O(m * n)
class Solution {
public:
int minDeletionSize(vector<string>& A) {
int m = A.size(), n = A[0].size(), res = 0;
vector<string> cur(m);
for(int j = 0; j < n; j ++){
vector<string> cur2 = cur;
for(int i = 0; i < m; i ++)
cur2[i] += A[i][j];
if(!is_sort(cur2))
res ++;
else
cur = cur2;
}
return res;
}
private:
bool is_sort(const vector<string>& s){
for(int i = 1; i < s.size(); i ++)
if(s[i - 1] > s[i])
return false;
return true;
}
};
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;
}