[Algorithm] 7 week(2.21 ~ 27) 3/3

Dev_min·2022년 2월 25일
0

algorithm

목록 보기
21/157

944. Delete Columns to Make Sorted

var minDeletionSize = function(strs) {
    const countStrs = strs.length;
    const splitStr = strs[0].length;
    const columns = [];
    let count = 0;
    
    for(let i = 0; i < splitStr; i++){
        for(let j = 0; j < countStrs; j++){
            if(columns[i]){
                columns[i] += strs[j][i];    
            } else {
                columns[i] = strs[j][i];    
            }
            
        }
    }
    
    [...columns].forEach((item, index) => {
        const sortedStr = item.split('').sort().join('');
        console.log('sortedStr', sortedStr);
        if(item !== sortedStr){
            count += 1;
        }
    })
    
    return count;
    
};

runtime 76ms / faster than 96.03% code

var minDeletionSize = function(strs) {
    let idx = new Set();
    for (let i = 0; i < strs.length-1; i++) {
        for (let j = 0; j < strs[i].length; j++) {
            if(strs[i][j] > strs[i+1][j] && !idx.has(j)){
                idx.add(j);
            }
        }
    }
    return idx.size;
};
profile
TIL record

0개의 댓글