Leetcode_贪心算法

题目:

https://leetcode-cn.com/problems/delete-columns-to-make-sorted/description/

思路:

cba
daf
ghi

竖着分析

贪心策略所有不符合升序的这一列删除。

codes:

class Solution {
    public int minDeletionSize(String[] A) {
        if(A == null || A.length == 0) return 0;
        //所有小写字母串的长度都相同
        int count = 0;
        for(int i = 0;i < A[0].length();i++){
            for(int j = 0;j < A.length - 1;j++){
                if(A[j].charAt(i) > A[j+1].charAt(i)){
                    count++;
                    break;
                } 
            }            
        }
        
        return  count;
    }
}
发布了640 篇原创文章 · 获赞 12 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/xiamaocheng/article/details/104893771
今日推荐