Last active
December 16, 2018 05:34
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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