LeetCode Solutions

944. Delete Columns to Make Sorted

Time: $O(n \cdot |\texttt{A}|)$, where $n = |\texttt{A[0]}|$

Space: $O(1)$

			

class Solution {
 public:
  int minDeletionSize(vector<string>& A) {
    const int n = A[0].length();
    int ans = 0;

    for (int j = 0; j < n; ++j)
      for (int i = 0; i + 1 < A.size(); ++i)
        if (A[i][j] > A[i + 1][j]) {
          ++ans;
          break;
        }

    return ans;
  }
};
			

class Solution {
  public int minDeletionSize(String[] A) {
    final int n = A[0].length();
    int ans = 0;

    for (int j = 0; j < n; ++j)
      for (int i = 0; i + 1 < A.length; ++i)
        if (A[i].charAt(j) > A[i + 1].charAt(j)) {
          ++ans;
          break;
        }

    return ans;
  }
}