首页 > 其他 > 详细

LC 955. Delete Columns to Make Sorted II

时间:2019-02-04 17:55:18      阅读:283      评论:0      收藏:0      [点我收藏+]

We are given an array A of N lowercase letter strings, all of the same length.

Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.

For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"].

Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]).

Return the minimum possible value of D.length.

 

Runtime: 8 ms, faster than 100.00% of C++ online submissions for Delete Columns to Make Sorted II.
Memory Usage: 819.2 KB, less than 48.15% of C++ online submissions for Delete Columns to Make Sorted II.

 

class Solution {
public:
  int minDeletionSize(vector<string>& A) {
    int ret = 0;
    bool sorted[A.size()];
    for(int i=0; i<A.size(); i++) sorted[i] = false;
    for(int i=0; i<A[0].size(); i++) {
      int j = 0;
      for(; j<A.size()-1; j++) {
        if(!sorted[j] && (A[j][i] > A[j+1][i])) {
          ret++;
          break;
        }
      }
      if(j < A.size()-1) continue;
      j = 0;
      for(; j<A.size()-1; j++) {
        if(A[j][i] < A[j+1][i]) sorted[j] = true;
      }
    }
    return ret;
  }
};

 

LC 955. Delete Columns to Make Sorted II

原文:https://www.cnblogs.com/ethanhong/p/10351934.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!