๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์ž๋ฃŒ๊ตฌ์กฐ & ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ฒŒ์ž„ (Java)

by 1mj 2021. 10. 29.

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ฒŒ์ž„

[[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] 4

programmers.co.kr

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‹ญ "ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ฒŒ์ž„" ๋ฌธ์ œ ํ’€์ด์ด๋‹ค.

 

์„ค๋ช…์— ๋‚˜์™€์žˆ๋Š” ๋Œ€๋กœ ๋”ฐ๋ผํ•˜๋ฉด ์–ด๋ ต์ง€๋Š” ์•Š์€๋ฐ, ์ธํ˜•์„ ๋ฝ‘๋Š” ๋ฐ˜๋ณต๋ฌธ๊ณผ ๋ฝ‘ํžŒ ๋ชฉ๋ก ๋‚ด ๊ฐ™์€ ๊ฐ’์ด ๋‘ ๋ฒˆ ๋‚˜์˜ค๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ๋‘ ๋ฒˆ์— ๋‚˜๋ˆ ์„œ ํ• ์ง€ ํ•œ ๋ฒˆ ๊ณ ๋ฏผํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

๋˜ํ•œ, ๊ทธ๋ฆผ ์ƒ์— ๋ฝ‘์€ ์ธํ˜•์ด ์Œ“์ด๋Š” ๋ถ€๋ถ„์ด ์Šคํƒ์ฒ˜๋Ÿผ ์ƒ๊ฒจ์„œ ์Šคํƒ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋‚˜ ์ž ๊น ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ ์ด์ „ ๊ฐ’๊ณผ ๋น„๊ตํ•˜๋Š” ๊ฑด ๋ฆฌ์ŠคํŠธ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ต์ˆ™ํ•œ ๊ฒƒ์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ์Šคํƒ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๊ณต๋ถ€ํ•ด๋ด์•ผ๊ฒ ๋‹ค.

 

import java.util.*;

class Solution {
    public int solution(int[][] board, int[] moves) {
        int count = 0;
        List<Integer> pickedList = new ArrayList<>();
        for (int move : moves) {
            for (int i = 0; i < board.length; i++) {
                if (board[i][move - 1] != 0) {
                    pickedList.add(board[i][move - 1]);
                    
                    int size = pickedList.size();
                    if (size != 1) {
                        if (pickedList.get(size - 2) == board[i][move - 1]) {
                            count++;
                            pickedList.remove(size - 1);
                            pickedList.remove(pickedList.size() - 1);
                        }
                    }
                    board[i][move - 1] = 0;
                    break;
                }
            }
        }
        return count * 2;
    }
}

 

 

๋Œ“๊ธ€