Delete Columns to Make Sorted

ㅋㅋ·2023년 1월 3일
0

알고리즘-leetcode

목록 보기
83/135

소문자 알파벳으로 이루어진 같은 길이의 문자열이 담긴 벡터를 받는다.

0번째 인덱스의 문자열을 시작으로 아래쪽 방향으로 세로로 줄세웠을 때

문자열들의 열들 중 알파벳 순으로 정렬되지 않은 열이 몇 개인지 판단하는 문제

For example, strs = ["abc", "bce", "cae"] can be arranged as:
abc
bce
cae
column 1 ('b', 'c', 'a') is not sorted
class Solution {
public:
    int minDeletionSize(vector<string>& strs) {
        
        int strLength = strs[0].size();

        int result{0};
        for (int i = 0; i < strLength; i++)
        {
            for (int j = 1; j < strs.size(); j++)
            {
                if (strs[j][i] < strs[j - 1][i])
                {
                    result++;
                    break;
                }
            }
        }

        return result;
    }
};

0개의 댓글