From 3b51abc0c965be78dc827a2a7d499f4edbefd0dd Mon Sep 17 00:00:00 2001 From: zoeyvarnax Date: Tue, 5 Sep 2023 09:06:30 +0900 Subject: [PATCH] =?UTF-8?q?solve(=EC=95=95=EC=B6=95,=20=ED=94=84=EB=A0=8C?= =?UTF-8?q?=EC=A6=884=EB=B8=94=EB=A1=9D):=20rayeon=5F230905?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "rayeon/week27/\354\225\225\354\266\225.java" | 40 +++++++ ...354\246\2104\353\270\224\353\241\235.java" | 104 ++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 "rayeon/week27/\354\225\225\354\266\225.java" create mode 100644 "rayeon/week27/\355\224\204\353\240\214\354\246\2104\353\270\224\353\241\235.java" diff --git "a/rayeon/week27/\354\225\225\354\266\225.java" "b/rayeon/week27/\354\225\225\354\266\225.java" new file mode 100644 index 0000000..92da2fc --- /dev/null +++ "b/rayeon/week27/\354\225\225\354\266\225.java" @@ -0,0 +1,40 @@ +package week27; + +import java.util.*; + +class 압축 { + public int[] solution(String msg) { + Map dictionary = new HashMap<>(); + for (int i = 0; i < 27; i++) { + dictionary.put(String.valueOf((char)('A' + i)), (i+1)); + } + + List list = new ArrayList<>(); + int index = 0; + int sum = 0; + StringBuilder sb = new StringBuilder(); + while (index < msg.length()) { + sb.append(msg.charAt(index)); + + if (dictionary.containsKey(sb.toString())) { + sum = dictionary.get(sb.toString()); + } else { + list.add(sum); + dictionary.put(sb.toString(), dictionary.size()); + + sum = dictionary.get(String.valueOf(msg.charAt(index))); + sb = new StringBuilder(String.valueOf(msg.charAt(index))); + } + + index++; + } + list.add(sum); + + int[] answer = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + answer[i] = list.get(i); + } + + return answer; + } +} \ No newline at end of file diff --git "a/rayeon/week27/\355\224\204\353\240\214\354\246\2104\353\270\224\353\241\235.java" "b/rayeon/week27/\355\224\204\353\240\214\354\246\2104\353\270\224\353\241\235.java" new file mode 100644 index 0000000..1928c06 --- /dev/null +++ "b/rayeon/week27/\355\224\204\353\240\214\354\246\2104\353\270\224\353\241\235.java" @@ -0,0 +1,104 @@ +package week27; + +import java.util.*; + +class 프렌즈4블록 { + static int M,N; + static char[][] board; + static boolean[][] bombBoard; + + static int[][] direction = {{1, 0}, {0, 1}, {1, 1}}; + + static void checkBomb(int row, int col) { + char target = board[row][col]; + + for (int[] d : direction) { + if (board[row + d[0]][col + d[1]] != target) { + return; + } + } + + bombBoard[row][col] = true; + for (int[] d : direction) { + bombBoard[row + d[0]][col + d[1]] = true; + } + } + + static int bomb() { + int count = 0; + + for (int row = 0; row < M; row++) { + for (int col = 0; col < N; col++) { + if (bombBoard[row][col]) { + count++; + board[row][col] = ' '; + bombBoard[row][col] = false; + } + } + } + + return count; + } + + static void check() { + for (int row = 0; row < M - 1; row++) { + for (int col = 0; col < N - 1; col++) { + if (board[row][col] == ' ') { + continue; + } + + checkBomb(row, col); + } + } + } + + static void down() { + for (int col = 0; col < N; col++) { + Queue emptyNodes = new LinkedList<>(); + + for (int row = M - 1; row >= 0; row--) { + if (board[row][col] == ' ') { + emptyNodes.offer(new int[]{row, col}); + + continue; + } + + if (emptyNodes.isEmpty()) { + continue; + } + + int[] node = emptyNodes.poll(); + board[node[0]][node[1]] = board[row][col]; + board[row][col] = ' '; + emptyNodes.offer(new int[]{row, col}); + } + } + } + + public int solution(int m, int n, String[] input) { + int answer = 0; + + M = m; + N = n; + board = new char[M][N]; + bombBoard = new boolean[M][N]; + + for (int row = 0; row < M; row++) { + board[row] = input[row].toCharArray(); + } + + while (true) { + check(); + + int count = bomb(); + if (count == 0) { + break; + } + + answer += count; + down(); + } + + return answer; + } +}