Posts programmers_Level1_javascript - 카카오 크레인 인형뽑기
Post
Cancel

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

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

1. board에서 선택된 인형들을 result 배열에 담기

count = 0 이면 board의 첫번째 열의 값들을 b[m - 1]로 찾을 수 있다.

b[m - 1] = 0,0,0,4,3

result에 0이 아닌 값을 가진 첫번째 인형 [4] 가 담기면
b[m - 1] = 0로 board에서 선택된 인형의 값 [4]를 [0]으로 만들어준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  [
  [ 0, 0, 0, 0, 0 ],
  [ 0, 0, 1, 0, 3 ],
  [ 0, 2, 5, 0, 1 ],
  [ 4, 2, 4, 4, 2 ],
  [ 3, 5, 1, 3, 1 ]
]

// b[m - 1] = 0 를 하면
[
  [ 0, 0, 0, 0, 0 ],
  [ 0, 0, 1, 0, 3 ],
  [ 0, 2, 5, 0, 1 ],
  [ 0, 2, 4, 4, 2 ],
  [ 3, 5, 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
function solution(board, moves) {
    const result = [];
    let count = 0;
    let point = 0;
    moves.map((m, j) => {
        board.map((b, i) => {
            if (b[m - 1] !== 0 && count === j) {
                count++;
                result.push(b[m - 1]);              
                 b[m - 1] = 0;
                  // console.log(result)
               // [ 4, 3, 1, 1, 3, 2 ]
            }
           // console.log(board)          
         
        })
    })
   }

solution([[0, 0, 0, 0, 0], [0, 0, 1, 0, 3], [0, 2, 5, 0, 1], [4, 2, 4, 4, 2], [3, 5, 1, 3, 1]], [1, 5, 3, 5, 1, 2, 1, 4]);


2. 선택된 인형들 중 연속해서 같은 값이 담기면 result에서 제거하기

전에 선택된 인형 result[result.length - 1] 와 현재 선택한 인형의 값 b[m - 1] 을 비교해서 같으면 result 배열에서 지우고 point를 2씩 올려준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function solution(board, moves) {
    const result = [];
    let count = 0;
    let point = 0;
    moves.map((m, j) => {
        board.map((b, i) => {

            if (b[m - 1] !== 0 && count === j) {
                count++;
                if (result[result.length - 1] === b[m - 1]) {
                    result.pop();
                    point += 2;
                } else result.push(b[m - 1]);
                b[m - 1] = 0;               
            }           
        })
    })
    return point;

}

solution([[0, 0, 0, 0, 0], [0, 0, 1, 0, 3], [0, 2, 5, 0, 1], [4, 2, 4, 4, 2], [3, 5, 1, 3, 1]], [1, 5, 3, 5, 1, 2, 1, 4]);

참고:
알고리즘 카카오 크레인 인형뽑기-JavaScript

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

javascript - 객체의 복사(copy object)

programmers_Level1_javascript - 카카오 실패율

Comments powered by Disqus.