当前位置:网站首页>Summary of force deduction solution 944- deletion of sequence

Summary of force deduction solution 944- deletion of sequence

2022-06-12 02:08:00 Lost summer

Directory links :

Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog

GitHub Synchronous question brushing items :

https://github.com/September26/java-algorithms

Original link : Power button


describe :

Here you are n An array of lowercase character strings strs, Where each string is of equal length .

These strings can each be one line , Form a grid . for example ,strs = ["abc", "bce", "cae"] Can be arranged as :

abc
bce
cae
You need to find and delete Not in ascending dictionary order Column . In the above example ( Subscript from 0 Start ) in , Column 0('a', 'b', 'c') And column 2('c', 'e', 'e') They are arranged in ascending order , And column 1('b', 'c', 'a') No , So delete the column 1 .

Return the number of columns you need to delete .

Example 1:

Input :strs = ["cba","daf","ghi"]
Output :1
explain : The grid diagram is as follows :
  cba
  daf
  ghi
Column 0 And column 2 In ascending order , Danlie 1 No , So just delete the columns 1 .
Example 2:

Input :strs = ["a","b"]
Output :0
explain : The grid diagram is as follows :
  a
  b
Only Columns 0 This column , And have been arranged in ascending order , So you don't have to delete any columns .
Example 3:

Input :strs = ["zyx","wvu","tsr"]
Output :3
explain : The grid diagram is as follows :
  zyx
  wvu
  tsr
all 3 The columns are arranged in non ascending order , So you have to delete .
 

Tips :

n == strs.length
1 <= n <= 100
1 <= strs[i].length <= 1000
strs[i] It's made up of lowercase letters

source : Power button (LeetCode)
link :https://leetcode.cn/problems/delete-columns-to-make-sorted
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

Their thinking :

*  Their thinking :
*  Relatively simple , Just generate a two-dimensional array comparison 

Code :

public class Solution944 {

    public int minDeletionSize(String[] strs) {
        int num = 0;
        char[][] charss = new char[strs.length][strs[0].length()];
        for (int i = 0; i < strs.length; i++) {
            charss[i] = strs[i].toCharArray();
        }
        for (int i = 0; i < charss[0].length; i++) {
            int last = 0;
            for (char[] chars : charss) {
                char c = chars[i];
                if (c >= last) {
                    last = c;
                    continue;
                }
                num++;
                break;
            }
        }
        return num;
    }
}

原网站

版权声明
本文为[Lost summer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120202543488.html