Posts programmers_Level1_javascript - 카카오 실패율
Post
Cancel

programmers_Level1_javascript - 카카오 실패율

프로그래머스에서 문제 확인하기

1. filter를 이용해 각 단계의 실패율을 배열 arr에 담아준다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function solution(N, stages) {
    let totalNum = stages.length;
    let arr = [];

    for (let i = 1; i <= N; i++) {
        let stageNum = stages.filter(e => e === i).length;
        let failRatio = 0;
        if (stageNum === 0) {
            failRatio = 0
        } else {
            failRatio = stageNum / totalNum;
        }
        totalNum -= stageNum;
        arr.push({ id: i, ratio: failRatio });

    }
    // console.log(arr);
    // [
    //     { id: 1, ratio: 0.125 },
    //     { id: 2, ratio: 0.42857142857142855 },
    //     { id: 3, ratio: 0.5 },
    //     { id: 4, ratio: 0.5 },
    //     { id: 5, ratio: 0 }
    //   ]
  

}
console.log(solution(5, [2, 1, 2, 6, 2, 4, 3, 3]));

2. sort를 이용해 실패율을 내림차순으로 정렬한다

sort 할 때 ‘-1’이면 순서가 유지되고, ‘1’이면 순서가 서로 바뀐다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  const sortNum1 = [1, 2].sort((a, b) => {
        return a - b
        //a-b < 0 이므로 [1,2]로 순서가 유지된다. 

    });
    const sortNum2 = [2, 1].sort((a, b) => {
        return a - b
        //a-b > 0 이므로 [1,2]로 순서가 바뀐다.

    });

const sortNum3 = [1, 3, 2].sort((a, b) => {
    if (a - b > 0) {//[3, 2] 일 때 1이면 순서가 바뀌므로 [2, 3]가 될 것이다
        return -1;//오름차순으로 바꿔준다 [ 3, 2 ]
    }else if (a - b < 0) {//[1, 3] 일 때 -1이면 순서가 유지되므로 [1, 3]가 될 것이다
            return 1;//오름차순으로 바꿔준다 [ 3, 1 ]
        }
});


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function solution(N, stages) {
    let totalNum = stages.length;
    let arr = [];

    for (let i = 1; i <= N; i++) {
        let stageNum = stages.filter(e => e === i).length;
        let failRatio = 0;
        if (stageNum === 0) {
            failRatio = 0
        } else {
            failRatio = stageNum / totalNum;
        }
        totalNum -= stageNum;
        arr.push({ id: i, ratio: failRatio });

    }
  
    arr.sort((p, c) => {
        if (p.ratio > c.ratio) {
            return -1;//유지
        } else if (p.ratio < c.ratio) {
            return 1;//바꾸기
        } else {
            if (p.id > c.id) {
                return 1;//바꾸기
            } else {
                return -1;//유지
            }
        }
    })

}
console.log(solution(5, [2, 1, 2, 6, 2, 4, 3, 3]));

참고:
[프로그래머스] 실패율 (JavaScript)

This post is licensed under CC BY 4.0 by the author.

programmers_Level1_javascript - 카카오 크레인 인형뽑기

antd-icon 용량 줄이기

Comments powered by Disqus.