forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
62 lines (45 loc) · 1.22 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
/// Source : https://leetcode.com/problems/delete-columns-to-make-sorted-iii/
/// Author : liuyubobobo
/// Time : 2018-12-15
#include <iostream>
#include <vector>
using namespace std;
/// Memory Search
/// Time Complexity: O(m * n * n)
/// Space Complexity: O(m * n)
class Solution {
private:
int m, n;
public:
int minDeletionSize(vector<string>& A) {
for(string& s: A)
s = "a" + s;
m = A.size();
n = A[0].size();
vector<vector<int>> dp(n, vector<int>(n, -1));
return dfs(A, 0, 0, dp);
}
private:
int dfs(const vector<string>& A, int index, int prev,
vector<vector<int>>& dp){
if(index == n)
return 0;
if(dp[index][prev] != -1)
return dp[index][prev];
int res = 1 + dfs(A, index + 1, prev, dp);
bool ok = true;
for(int i = 0; i < m; i ++)
if(A[i][index] < A[i][prev]){
ok = false;
break;
}
if(ok)
res = min(res, dfs(A, index + 1, index, dp));
return dp[index][prev] = res;
}
};
int main() {
vector<string> A1 = {"babca","bbazb"};
cout << Solution().minDeletionSize(A1) << endl;
return 0;
}