알고리즘 풀이 방법입니다.
문제(Problem) -> 생각(Think) -> 해결책(Solution) -> 리뷰(Review) 를 통해서 정리해서 작성합니다.
Problem📄

https://school.programmers.co.kr/learn/courses/30/lessons/161989

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


Think🤔
import java.util.ArrayList;

class Solution {
    public int solution(int n, int m, int[] section) { // 8 4 236
        int answer = 0;
        int start = section[0]; // 2
        int end = section[section.length-1]; // 6
        
        for(int i=0; i<end; i++){
            if(section[i] >= start && section[i] <= end){
                start = section[i];
                end = end - m + 1;
                answer++;
            }
        }
        
        
        return answer;
    }
}

첫번째 작성코드 62점

마지막 값에서 타일 바른갯수를 삭제하고 카운트를 해줬다.

 

이 코드가 안되는 이유는 end는 자릿수를 검사해야 되는데 반복문을 깎아버려서 애초에 맞지 않는 코드


Solution✍
import java.util.ArrayList;

class Solution {
    public int solution(int n, int m, int[] section) {
        int answer = 0;
        int start = section[0];
        answer++; // section의 길이 최소 1 한번은 무조건 칠함
        
        for(int i=0; i<section.length; i++){
            if(start + m > section[i]){ // 칠해져있으면 section의 값은 패쓰
                continue;
            }
            answer++;
            start = section[i];
        }
        
        
        return answer;
    }
}

Review🤩

 

+ Recent posts