[프로그래머스 레벨투] 기능개발 ⚙️

9rganizedChaos·2021년 10월 2일
0
post-thumbnail

🔽 문제 링크

https://programmers.co.kr/learn/courses/30/lessons/42586

✍🏼 나의 수도 코드

1) progress의 각 요소를 100에서 빼준, 새로운 배열 todoArr를 만든다.
2) 해당 todoArr 배열은 진도까지 남은 퍼센티지를 나타내는 배열이다.
3) todoArr 배열의 각 요소를 speeds로 나누어준다. 
4) 각 요소들은 각 기능들을 개발하기까지 며칠이 남았는지를 나타내게 된다.
5) todoArr를 포문을 돌며 배포해준다.

👨🏻‍💻 나의 문제 풀이

function solution(progresses, speeds) {
    let howManyDays = progresses.map(item => 100 - item).map((item, index) => Math.ceil(item / speeds[index]));
    let result = [];
    let count = 1;
    let standard = howManyDays[0];
    for(let i = 1; i < howManyDays.length + 1; i++){
        if(howManyDays[i] > standard || !howManyDays[i]){
            result.push(count);
            standard = howManyDays[i];
            count = 1;
        } else {
            count++;
        }
    }
    return result;
}

👩🏻‍💻 다른 사람의 코드

// 나의 풀이와 거의 유사하다. 
// 다만 반복문 밖에 count라는 변수를 두는 것이 아니라, j라는 변수를 하나 더 두면서,
// answer 배열의 몇 번째에 ++를 해줄 건지 마크하고 있다.
function solution(progresses, speeds) {
    let answer = [0];
    let days = progresses.map((progress, index) => Math.ceil((100 - progress) / speeds[index]));
    let maxDay = days[0];

    for(let i = 0, j = 0; i< days.length; i++){
        if(days[i] <= maxDay) {
            answer[j] += 1;
        } else {
            maxDay = days[i];
            answer[++j] = 1;
        }
    }

    return answer;
}

🍯 알게 된 것들

  • for문에서 변수 선언할 때, 당연한 것이지만, ","를 이용해 여러 개의 변수를 선언할 수 있다.
    (당연한 거지만, 딱히 이중반복문을 사용할 때가 아니라면, 반복문 변수를 두 개 이상 선언해서 사용해본 경험이 없다.)
profile
부정확한 정보나 잘못된 정보는 댓글로 알려주시면 빠르게 수정토록 하겠습니다, 감사합니다!

0개의 댓글