Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Last active December 16, 2018 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/7168903d18fc907a6be174c4319b234d to your computer and use it in GitHub Desktop.
Save fpdjsns/7168903d18fc907a6be174c4319b234d to your computer and use it in GitHub Desktop.
[leetcode] 955. Delete Columns to Make Sorted II : https://leetcode.com/problems/delete-columns-to-make-sorted-ii/
class Solution {
public:
int minDeletionSize(vector<string>& A) {
int n = A.size();
int m = A[0].size();
int ans = 0;
vector<bool> ordered(m, false);
for(int j=0;j<m;j++){
bool deletedRow = false;
for(int i=1;i<n;i++){
if(ordered[i] || A[i-1][j] <= A[i][j]) continue;
ans++;
deletedRow = true;
break;
}
if(deletedRow) continue;
for(int i=1;i<n;i++){
ordered[i] = ordered[i] || (A[i-1][j] < A[i][j]);
}
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment