diff --git a/README.md b/README.md index fe3bd72..455d96a 100644 --- a/README.md +++ b/README.md @@ -119,34 +119,138 @@ algorithm-study #### DP -[[실버2] 부분수열의 합](https://www.acmicpc.net/problem/1182)
-[[실버3] 1로 만들기](https://www.acmicpc.net/problem/1463)
-[[실버3] 2xn 타일링](https://www.acmicpc.net/problem/11726)
-[[실버3] 2xn 타일링(2)](https://www.acmicpc.net/problem/11727)
-[[실버3] 1,2,3 더하기](https://www.acmicpc.net/problem/9095)
-[[실버1] 카드 구매하기](https://www.acmicpc.net/problem/11052)
-[[실버1] 카드 구매하기2](https://www.acmicpc.net/problem/16194)
-[[실버2] 1,2,3 더하기 5](https://www.acmicpc.net/problem/15990)
-[[실버1] 쉬운 계단 수](https://www.acmicpc.net/problem/10844)
-[[실버2] 가장 긴 증가하는 부분 수열](https://www.acmicpc.net/problem/11053)
+[24. [실버2] 부분수열의 합](https://www.acmicpc.net/problem/1182)
+[25. [실버3] 1로 만들기](https://www.acmicpc.net/problem/1463)
+[26. [실버3] 2xn 타일링](https://www.acmicpc.net/problem/11726)
+[27. [실버3] 2xn 타일링(2)](https://www.acmicpc.net/problem/11727)
+[28. [실버3] 1,2,3 더하기](https://www.acmicpc.net/problem/9095)
+[29. [실버1] 카드 구매하기](https://www.acmicpc.net/problem/11052)
+[30. [실버1] 카드 구매하기2](https://www.acmicpc.net/problem/16194)
+[31. [실버2] 1,2,3 더하기 5](https://www.acmicpc.net/problem/15990)
+[32. [실버1] 쉬운 계단 수](https://www.acmicpc.net/problem/10844)
+[33. [실버2] 가장 긴 증가하는 부분 수열](https://www.acmicpc.net/problem/11053)
### 5주차 #### DP -[[실버2] 연속 합](https://www.acmicpc.net/problem/1912)
-[[실버2] 제곱수의 합](https://www.acmicpc.net/problem/1699)
-[[실버2] 1,2,3 더하기 3](https://www.acmicpc.net/problem/15988)
-[[실버1] RGB거리](https://www.acmicpc.net/problem/1149)
-[[실버1] 동물원](https://www.acmicpc.net/problem/1309)
-[[실버1] 오르막 수](https://www.acmicpc.net/problem/11057)
+[34. [실버2] 연속 합](https://www.acmicpc.net/problem/1912)
+[35. [실버2] 제곱수의 합](https://www.acmicpc.net/problem/1699)
+[36. [실버2] 1,2,3 더하기 3](https://www.acmicpc.net/problem/15988)
+[37. [실버1] RGB거리](https://www.acmicpc.net/problem/1149)
+[38. [실버1] 동물원](https://www.acmicpc.net/problem/1309)
+[39. [실버1] 오르막 수](https://www.acmicpc.net/problem/11057)
#### 큐와 그래프 -[[실버4] 큐](https://www.acmicpc.net/problem/10845)
-[[실버4] 덱](https://www.acmicpc.net/problem/10866)
-[[실버2] DFS와 BFS](https://www.acmicpc.net/problem/1260)
-[[실버2] 연결 요소의 개수](https://www.acmicpc.net/problem/11724)
+[40. [실버4] 큐](https://www.acmicpc.net/problem/10845)
+[41. [실버4] 덱](https://www.acmicpc.net/problem/10866)
+[42. [실버2] DFS와 BFS](https://www.acmicpc.net/problem/1260)
+[43. [실버2] 연결 요소의 개수](https://www.acmicpc.net/problem/11724)
+ +### 6주차 + +#### 큐와 그래프 + +[44. [실버1] 단지번호붙이기](https://www.acmicpc.net/problem/2667)
+[45. [실버1] 미로 탐색](https://www.acmicpc.net/problem/2178)
+[46. [실버1] 나이트의 이동](https://www.acmicpc.net/problem/7562)
+ +#### BFS + +[47. [실버1] 숨바꼭질](https://www.acmicpc.net/problem/1697)
+[48. [골드4] 숨바꼭질 4](https://www.acmicpc.net/problem/13913)
+[49. [골드4] 이모티콘](https://www.acmicpc.net/problem/14226)
+[50. [골드5] 숨바꼭질3](https://www.acmicpc.net/problem/13549)
+[51. [골드4] 알고스팟](https://www.acmicpc.net/problem/1261)
+ +#### 시뮬레이션과 구현 + +[52. [골드5] 배열 돌리기 3](https://www.acmicpc.net/problem/16935)
+[53. [실버1] 배열 돌리기 1](https://www.acmicpc.net/problem/16926)
+ +### 7주차 + +#### 시뮬레이션과 구현 + +[54. [골드5] 배열 돌리기 2](https://www.acmicpc.net/problem/16927)
+[55. [골드4] 주사위 굴리기](https://www.acmicpc.net/problem/14499)
+[56. [골드3] 경사로](https://www.acmicpc.net/problem/14890)
+ +### 8주차 + +#### 시뮬레이션과 구현 + +[57. [골드5] 톱니바퀴(2)](https://www.acmicpc.net/problem/15662)
+[58. [골드5] 로봇청소기](https://www.acmicpc.net/problem/14503)
+[59. [실버2] LCD Test](https://www.acmicpc.net/problem/2290)
+[60. [실버2] 겉넓이 구하기](https://www.acmicpc.net/problem/16931)
+[61. [실버3] 배열 복원하기](https://www.acmicpc.net/problem/16967)
+[62. [골드5] 컨베이어 벨트 위의 로봇](https://www.acmicpc.net/problem/20055)
+ +#### 해시 + +[63. [레벨1] 폰켓몬](https://school.programmers.co.kr/learn/courses/30/lessons/1845)
+[64. [레벨1] 완주하지 못한 선수](https://school.programmers.co.kr/learn/courses/30/lessons/42576)
+ +### 9주차 + +#### 해시 + +[66. [레벨2] 전화번호 목록](https://school.programmers.co.kr/learn/courses/30/lessons/42577)
+[66. [레벨2] 의상](https://school.programmers.co.kr/learn/courses/30/lessons/42578)
+[67. [레벨3] 베스트앨범](https://school.programmers.co.kr/learn/courses/30/lessons/42579)
+ +#### 스택/큐 + +[68. [레벨1] 같은 숫자는 싫어](https://school.programmers.co.kr/learn/courses/30/lessons/12906)
+[69. [레벨2] 기능개발](https://school.programmers.co.kr/learn/courses/30/lessons/42586)
+[70. [레벨2] 올바른 괄호](https://school.programmers.co.kr/learn/courses/30/lessons/12909)
+[71. [레벨2] 다리를 지나는 트럭](https://school.programmers.co.kr/learn/courses/30/lessons/42583)
+[72. [레벨2] 주식가격](https://school.programmers.co.kr/learn/courses/30/lessons/42584)
+ +### 10주차 + +#### 힙 + +[73. [레벨2] 더 맵게](https://school.programmers.co.kr/learn/courses/30/lessons/42626)
+[74. [레벨3] 디스크 컨트롤러](https://school.programmers.co.kr/learn/courses/30/lessons/42627)
+[75. [레벨3] 이중우선순위큐](https://school.programmers.co.kr/learn/courses/30/lessons/42628)
+ +#### 정렬 + +[76. [레벨1] K번째수](https://school.programmers.co.kr/learn/courses/30/lessons/42748)
+[77. [레벨2] 가장 큰 수](https://school.programmers.co.kr/learn/courses/30/lessons/42746)
+[78. [레벨2] H-Index](https://school.programmers.co.kr/learn/courses/30/lessons/42747)
+ +#### 완전탐색 + +[79. [레벨2] 소수 찾기](https://school.programmers.co.kr/learn/courses/30/lessons/42839)
+[80. [레벨2] 카펫](https://school.programmers.co.kr/learn/courses/30/lessons/42842)
+[81. [레벨2] 피로도](https://school.programmers.co.kr/learn/courses/30/lessons/87946)
+[82. [레벨2] 전력망을 둘로 나누기](https://school.programmers.co.kr/learn/courses/30/lessons/86971)
+ +### 11주차 + +#### 완전탐색 + +[[레벨2] 모음사전](https://school.programmers.co.kr/learn/courses/30/lessons/84512)
+ +#### 탐욕법 + +[[레벨1] 체육복](https://school.programmers.co.kr/learn/courses/30/lessons/42862)
+[[레벨2] 조이스틱](https://school.programmers.co.kr/learn/courses/30/lessons/42860)
+[[레벨2] 큰 수 만들기](https://school.programmers.co.kr/learn/courses/30/lessons/42883)
+[[레벨2] 구명보트](https://school.programmers.co.kr/learn/courses/30/lessons/42885)
+[[레벨3] 섬 연결하기](https://school.programmers.co.kr/learn/courses/30/lessons/42861)
+ +#### 동적계획법 + +[[레벨3] N으로 표현](https://school.programmers.co.kr/learn/courses/30/lessons/42895) + +#### DFS/BFS + +[[레벨3] 타겟 넘버](https://school.programmers.co.kr/learn/courses/30/lessons/43165) ## 참고 diff --git a/src/main/java/changhyeon/BOJ_1261.java b/src/main/java/changhyeon/BOJ_1261.java new file mode 100644 index 0000000..f092a2a --- /dev/null +++ b/src/main/java/changhyeon/BOJ_1261.java @@ -0,0 +1,88 @@ +package changhyeon; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_1261 { + + private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + private static int[][] map; + private static boolean[][] visited; + private static int[] mx = {0, 0, -1, 1}; + private static int[] my = {-1, 1, 0, 0}; + private static int M, N; + + public static void main(String[] args) throws IOException { + Scanner sc = new Scanner(System.in); + M = sc.nextInt(); + N = sc.nextInt(); + sc.nextLine(); + + visited = new boolean[N + 1][M + 1]; + map = new int[N + 1][M + 1]; + + for (int i = 1; i <= N; i++) { + String line = sc.nextLine(); + for (int j = 1; j <= M; j++) { + map[i][j] = line.charAt(j - 1) - '0'; + } + } + bfs(1, 1); + } + + private static void bfs(final int x, final int y) { + Queue priorityQueue = new PriorityQueue<>(); + priorityQueue.offer(new Node(x, y, 0)); + visited[x][y] = true; + + while (!priorityQueue.isEmpty()) { + Node node = priorityQueue.poll(); + + if (node.x == N && node.y == M) { + System.out.println(node.count); + return; + } + + for (int i = 0; i < 4; i++) { + int dx = mx[i] + node.x; + int dy = my[i] + node.y; + + if (dx < 1 || dy < 1 || dx > N || dy > M) { + continue; + } + + if (!visited[dx][dy] && map[dx][dy] == 1) { + visited[dx][dy] = true; + priorityQueue.offer(new Node(dx, dy, node.count + 1)); + } + + if (!visited[dx][dy] && map[dx][dy] == 0) { + visited[dx][dy] = true; + priorityQueue.offer(new Node(dx, dy, node.count)); + } + } + } + } + + static class Node implements Comparable { + + int x; + int y; + int count; + + public Node(int x, int y, int count) { + this.x = x; + this.y = y; + this.count = count; + } + + @Override + public int compareTo(Node node) { + return count - node.count; + } + } +} diff --git a/src/main/java/changhyeon/BOJ_13549.java b/src/main/java/changhyeon/BOJ_13549.java new file mode 100644 index 0000000..43736f8 --- /dev/null +++ b/src/main/java/changhyeon/BOJ_13549.java @@ -0,0 +1,70 @@ +package changhyeon; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Deque; + +public class BOJ_13549 { + + private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + private static int N, K, MAX = 100_001, MIN = 0, count; + private static boolean[] visited; + private static int[] time; + + public static void main(String[] args) throws IOException { + String[] line = bufferedReader.readLine().split(" "); + N = Integer.parseInt(line[0]); + K = Integer.parseInt(line[1]); + + visited = new boolean[MAX]; + time = new int[MAX]; + + if (N == K) { + System.out.println(0); + return; + } + + bfs(N); + } + + private static void bfs(final int N) { + Deque deque = new ArrayDeque<>(); + deque.add(N); + visited[N] = true; + int size; + + while (true) { + size = deque.size(); + + for (int i = 0; i < size; i++) { + int number = deque.poll(); + + if (number == K) { + System.out.println(time[number]); + return; + } + + if (number * 2 >= MIN && number * 2 < MAX && !visited[number * 2]) { + deque.add(number * 2); + visited[number * 2] = true; + time[number * 2] = time[number]; + } + + if (number - 1 >= MIN && number - 1 < MAX && !visited[number - 1]) { + deque.add(number - 1); + visited[number - 1] = true; + time[number - 1] = time[number] + 1; + } + + if (number + 1 >= MIN && number + 1 < MAX && !visited[number + 1]) { + deque.add(number + 1); + visited[number + 1] = true; + time[number + 1] = time[number] + 1; + } + } + count++; + } + } +} diff --git a/src/main/java/changhyeon/BOJ_13913.java b/src/main/java/changhyeon/BOJ_13913.java new file mode 100644 index 0000000..94278b9 --- /dev/null +++ b/src/main/java/changhyeon/BOJ_13913.java @@ -0,0 +1,84 @@ +package changhyeon; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Deque; + +public class BOJ_13913 { + + private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + private static int N, K, MAX = 100_001, MIN = 0, count; + private static int[] before; + private static boolean[] visited; + + public static void main(String[] args) throws IOException { + String[] line = bufferedReader.readLine().split(" "); + N = Integer.parseInt(line[0]); + K = Integer.parseInt(line[1]); + + visited = new boolean[MAX]; + before = new int[MAX]; + + if (N == K) { + System.out.println(0); + System.out.println(N); + return; + } + + bfs(N); + + Deque deque = new ArrayDeque<>(); + + while (K != N) { + deque.add(K); + K = before[K]; + } + + deque.add(K); + + while (!deque.isEmpty()) { + System.out.print(deque.pollLast() + " "); + } + } + + private static void bfs(final int N) { + Deque deque = new ArrayDeque<>(); + deque.add(N); + visited[N] = true; + int size; + + while (true) { + size = deque.size(); + + for (int i = 0; i < size; i++) { + int number = deque.poll(); + + if (number == K) { + System.out.println(count); + return; + } + + if (number * 2 >= MIN && number * 2 < MAX && !visited[number * 2]) { + deque.add(number * 2); + visited[number * 2] = true; + before[number * 2] = number; + } + + if (number - 1 >= MIN && number - 1 < MAX && !visited[number - 1]) { + deque.add(number - 1); + visited[number - 1] = true; + before[number - 1] = number; + } + + if (number + 1 >= MIN && number + 1 < MAX && !visited[number + 1]) { + deque.add(number + 1); + visited[number + 1] = true; + before[number + 1] = number; + } + } + count++; + } + } +} diff --git a/src/main/java/changhyeon/BOJ_14053.java b/src/main/java/changhyeon/BOJ_14053.java new file mode 100644 index 0000000..53e72d3 --- /dev/null +++ b/src/main/java/changhyeon/BOJ_14053.java @@ -0,0 +1,81 @@ +package changhyeon; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class BOJ_14053 { + + private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + private static boolean[][] visited; + private static int[][] map; + private static int x, y; + private static int count; + private static final int[] mx = {-1, 0, 1, 0}; + private static final int[] my = {0, 1, 0, -1}; + + public static void main(String[] args) throws IOException { + StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine(), " "); + + x = Integer.parseInt(stringTokenizer.nextToken()); + y = Integer.parseInt(stringTokenizer.nextToken()); + + stringTokenizer = new StringTokenizer(bufferedReader.readLine(), " "); + + int startX = Integer.parseInt(stringTokenizer.nextToken()); + int startY = Integer.parseInt(stringTokenizer.nextToken()); + int startDirection = Integer.parseInt(stringTokenizer.nextToken()); + + map = new int[x][y]; + visited = new boolean[x][y]; + + for (int i = 0; i < x; i++) { + stringTokenizer = new StringTokenizer(bufferedReader.readLine(), " "); + for (int j = 0; j < y; j++) { + map[i][j] = Integer.parseInt(stringTokenizer.nextToken()); + } + } + + count++; + visited[startX][startY] = true; + + dfs(startX, startY, startDirection); + + System.out.println(count); + } + + public static void dfs(int curX, int curY, int nowDirection) { + int i = 1; + for (; i <= 4; i++) { + int newDirection = (nowDirection - i < 0) ? nowDirection - i + 4 : nowDirection - i; + int nextX = curX + mx[newDirection]; + int nextY = curY + my[newDirection]; + + if ((nextX > 0 && nextX < x - 1) + && (nextY > 0 && nextY < y - 1) + && map[nextX][nextY] == 0 + && !visited[nextX][nextY] + ) { + count++; + visited[nextX][nextY] = true; + dfs(nextX, nextY, newDirection); + + break; + } + } + + if (i == 5) { + int oppoDirection = (nowDirection + 2 > 3) ? nowDirection - 2 : nowDirection + 2; + int nextX = curX + mx[oppoDirection]; + int nextY = curY + my[oppoDirection]; + + if ((nextX > 0 && nextX < x - 1) + && (nextY > 0 && nextY < y - 1) + && map[nextX][nextY] == 0 + ) { + dfs(nextX, nextY, nowDirection); + } + } + } +} diff --git a/src/main/java/changhyeon/BOJ_15662.java b/src/main/java/changhyeon/BOJ_15662.java new file mode 100644 index 0000000..eee3c34 --- /dev/null +++ b/src/main/java/changhyeon/BOJ_15662.java @@ -0,0 +1,99 @@ +package changhyeon; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; + +public class BOJ_15662 { + + private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + private static int count, GEAR_MAX_NUMBER = 8, LEFT_GEAR = 6, RIGHT_GEAR = 2, LEFT_DIRECTION = -1, RIGHT_DIRECTION = 1; + private static int[][] gears, method; + private static int rotationCount; + private static boolean[] visited; + + public static void main(String[] args) throws IOException { + count = Integer.parseInt(bufferedReader.readLine()); + gears = new int[count + 1][GEAR_MAX_NUMBER]; + + for (int i = 1; i <= count; i++) { + gears[i] = Arrays.stream(bufferedReader.readLine().split("")) + .mapToInt(Integer::parseInt) + .toArray(); + } + + rotationCount = Integer.parseInt(bufferedReader.readLine()); + + method = new int[rotationCount][2]; + + for (int i = 0; i < rotationCount; i++) { + method[i] = Arrays.stream(bufferedReader.readLine().split(" ")) + .mapToInt(Integer::parseInt) + .toArray(); + } + + for (int i = 0; i < rotationCount; i++) { + int gearNumber = method[i][0]; // 톱니바퀴 번호 + int direction = method[i][1]; // 방향 + + visited = new boolean[count + 1]; + process(gearNumber, direction); + } + + int cnt = 0; + for (int i = 1; i <= count; i++) { + if (gears[i][0] == 1) { + cnt++; + } + } + + System.out.println(cnt); + } + + private static void process(final int gearNumber, final int direction) { + visited[gearNumber] = true; + + changeDirection(gearNumber, direction); + + goLeftGear(gearNumber, direction); + goRightGear(gearNumber, direction); + } + + private static void goLeftGear(final int gearNumber, final int direction) { + if (gearNumber - 1 >= 1 && !visited[gearNumber - 1] + && gears[gearNumber - 1][RIGHT_GEAR] != gears[gearNumber][LEFT_GEAR + direction]) { + process(gearNumber - 1, direction * -1); + } + } + + private static void goRightGear(final int gearNumber, final int direction) { + if (gearNumber + 1 <= count && !visited[gearNumber + 1] + && gears[gearNumber + 1][LEFT_GEAR] != gears[gearNumber][RIGHT_GEAR + direction]) { + process(gearNumber + 1, direction * -1); + } + } + + private static void changeDirection(final int gearNumber, final int direction) { + if (direction == LEFT_DIRECTION) { + int zeroTemp = gears[gearNumber][0]; + + for (int i = 0; i < GEAR_MAX_NUMBER - 1; i++) { + gears[gearNumber][i] = gears[gearNumber][i + 1]; + } + + gears[gearNumber][7] = zeroTemp; + return; + } + + if (direction == RIGHT_DIRECTION) { + int lastTemp = gears[gearNumber][GEAR_MAX_NUMBER - 1]; + + for (int i = GEAR_MAX_NUMBER - 1; i > 0; i--) { + gears[gearNumber][i] = gears[gearNumber][i - 1]; + } + + gears[gearNumber][0] = lastTemp; + } + } +} diff --git a/src/main/java/changhyeon/BOJ_16931.java b/src/main/java/changhyeon/BOJ_16931.java new file mode 100644 index 0000000..d274d9e --- /dev/null +++ b/src/main/java/changhyeon/BOJ_16931.java @@ -0,0 +1,53 @@ +package changhyeon; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class BOJ_16931 { + + private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + private static final int[] mx = {-1, 0, 1, 0}; + private static final int[] my = {0, 1, 0, -1}; + private static int result = 0; + + public static void main(String[] args) throws IOException { + StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine()); + + int N = Integer.parseInt(stringTokenizer.nextToken()); + int M = Integer.parseInt(stringTokenizer.nextToken()); + + int[][] map = new int[N][M]; + + for (int i = 0; i < N; i++) { + map[i] = Arrays.stream(bufferedReader.readLine().split(" ")) + .mapToInt(Integer::parseInt) + .toArray(); + } + + result += 2 * N * M; + + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + + for (int k = 0; k < 4; k++) { + int dx = i + mx[k]; + int dy = j + my[k]; + + if (dx < 0 || dy < 0 || dx > N - 1 || dy > M - 1) { + result += map[i][j]; + continue; + } + + if (map[dx][dy] < map[i][j]) { + result += map[i][j] - map[dx][dy]; + } + } + } + } + + System.out.println(result); + } +} diff --git a/src/main/java/changhyeon/BOJ_16967.java b/src/main/java/changhyeon/BOJ_16967.java new file mode 100644 index 0000000..594e4cf --- /dev/null +++ b/src/main/java/changhyeon/BOJ_16967.java @@ -0,0 +1,63 @@ +package changhyeon; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; + +public class BOJ_16967 { + + private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + private static int H, W, X, Y; + + public static void main(String[] args) throws IOException { + + String[] line = bufferedReader.readLine().split(" "); + + H = Integer.parseInt(line[0]); + W = Integer.parseInt(line[1]); + X = Integer.parseInt(line[2]); + Y = Integer.parseInt(line[3]); + + int[][] arrA = new int[H][W]; + int[][] arrB = new int[H + X][W + Y]; + + for (int i = 0; i < arrB.length; i++) { + arrB[i] = Arrays.stream(bufferedReader.readLine().split(" ")) + .mapToInt(Integer::parseInt) + .toArray(); + } + + noOverlap(arrA, arrB); + overlap(arrA, arrB); + + for (int i = 0; i < H; i++) { + for (int j = 0; j < W; j++) { + System.out.print(arrA[i][j] + " "); + } + System.out.println(); + } + } + + private static void noOverlap(final int[][] arrA, final int[][] arrB) { + for (int i = 0; i < X; i++) { + for (int j = 0; j < W; j++) { + arrA[i][j] = arrB[i][j]; + } + } + + for (int i = X; i < H; i++) { + for (int j = 0; j < Y; j++) { + arrA[i][j] = arrB[i][j]; + } + } + } + + private static void overlap(final int[][] arrA, final int[][] arrB) { + for (int i = X; i < H; i++) { + for (int j = Y; j < W; j++) { + arrA[i][j] = arrB[i][j] - arrA[i - X][j - Y]; + } + } + } +} diff --git a/src/main/java/changhyeon/BOJ_1697.java b/src/main/java/changhyeon/BOJ_1697.java new file mode 100644 index 0000000..73486a5 --- /dev/null +++ b/src/main/java/changhyeon/BOJ_1697.java @@ -0,0 +1,63 @@ +package changhyeon; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Deque; + +public class BOJ_1697 { + + private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + private static int MIN = 0, MAX = 100_001, count = 0; + private static boolean[] visited = new boolean[MAX]; + + public static void main(String[] args) throws IOException { + String[] line = bufferedReader.readLine().split(" "); + + int N = Integer.parseInt(line[0]); + int K = Integer.parseInt(line[1]); + + if (N == K) { + System.out.println(0); + return; + } + + bfs(N, K); + } + + private static void bfs(final int N, final int K) { + Deque deque = new ArrayDeque<>(); + deque.add(N); + visited[N] = true; + int size; + + while (true) { + count++; + size = deque.size(); + for (int i = 0; i < size; i++) { + int current = deque.poll(); + + if (current - 1 == K || current + 1 == K || current * 2 == K) { + System.out.println(count); + return; + } + + if (current - 1 >= MIN && current - 1 < MAX && !visited[current - 1]) { + deque.add(current - 1); + visited[current - 1] = true; + } + + if (current + 1 >= MIN && current + 1 < MAX && !visited[current + 1]) { + deque.add(current + 1); + visited[current + 1] = true; + } + + if (current * 2 >= MIN && current * 2 < MAX && !visited[current * 2]) { + deque.add(current * 2); + visited[current * 2] = true; + } + } + } + } +} diff --git a/src/main/java/changhyeon/BOJ_20055.java b/src/main/java/changhyeon/BOJ_20055.java new file mode 100644 index 0000000..a37162a --- /dev/null +++ b/src/main/java/changhyeon/BOJ_20055.java @@ -0,0 +1,104 @@ +package changhyeon; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class BOJ_20055 { + + private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + private static int N, K; + private static int[][] belt; + private static int[] durability; + private static boolean[][] robot; + + public static void main(String[] args) throws IOException { + String[] line = bufferedReader.readLine().split(" "); + + N = Integer.parseInt(line[0]); + K = Integer.parseInt(line[1]); + + belt = new int[2][N + 1]; + robot = new boolean[2][N + 1]; + durability = new int[2 * N + 1]; + + line = bufferedReader.readLine().split(" "); + for (int i = 1; i <= 2 * N; i++) { + durability[i] = Integer.parseInt(line[i - 1]); + } + + int count = 1; + + for (int i = 1; i <= N; i++) + belt[0][i] = count++; + + for (int i = N; i >= 1; i--) + belt[1][i] = count++; + + System.out.println(goWork()); + } + + private static int goWork() { + int count = 0; + + while (checkDurability()) { + moveBelt(); + moveRobot(); + raiseRobot(); + count++; + } + + return count; + } + + private static boolean checkDurability() { + int count = 0; + + for (int i = 1; i <= 2 * N; i++) { + if (durability[i] == 0) { + count++; + } + } + + return count < K; + } + + private static void moveBelt() { + int tempBelt = belt[1][1]; + + for (int i = 1; i < N; i++) { + belt[1][i] = belt[1][i + 1]; + } + + belt[1][N] = belt[0][N]; + + for (int i = N; i >= 2; i--) { + belt[0][i] = belt[0][i - 1]; + robot[0][i] = robot[0][i - 1]; + } + + belt[0][1] = tempBelt; + + robot[0][1] = false; + robot[0][N] = false; + } + + private static void moveRobot() { + for (int i = N - 1; i >= 1; i--) { + if (robot[0][i] && !robot[0][i + 1] && durability[belt[0][i + 1]] > 0) { + robot[0][i] = false; + robot[0][i + 1] = true; + durability[belt[0][i + 1]]--; + } + } + + robot[0][N] = false; + } + + private static void raiseRobot() { + if (durability[belt[0][1]] > 0) { + robot[0][1] = true; + durability[belt[0][1]]--; + } + } +} diff --git a/src/main/java/changhyeon/BOJ_2290.java b/src/main/java/changhyeon/BOJ_2290.java new file mode 100644 index 0000000..fe8f9b4 --- /dev/null +++ b/src/main/java/changhyeon/BOJ_2290.java @@ -0,0 +1,89 @@ +package changhyeon; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class BOJ_2290 { + + private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + private static int S, N; + private static int left; + private static String[][] board; + private static int[][] map = { + {0, 1, 1, 1, 0, 1, 1, 1}, + {0, 0, 0, 1, 0, 0, 1, 0}, + {0, 1, 0, 1, 1, 1, 0, 1}, + {0, 1, 0, 1, 1, 0, 1, 1}, + {0, 0, 1, 1, 1, 0, 1, 0}, + {0, 1, 1, 0, 1, 0, 1, 1}, + {0, 1, 1, 0, 1, 1, 1, 1}, + {0, 1, 0, 1, 0, 0, 1, 0}, + {0, 1, 1, 1, 1, 1, 1, 1}, + {0, 1, 1, 1, 1, 0, 1, 1} + }; + + public static void main(String[] args) throws IOException { + StringTokenizer st = new StringTokenizer(bufferedReader.readLine()); + S = Integer.parseInt(st.nextToken()); + + String[] num = st.nextToken().split(""); + N = num.length; + + board = new String[2 * S + 4][N * (S + 4)]; + + fillEmptyBoard(); + + for (int i = 0; i < N; i++) { + makeNumber(num[i]); + } + + result(); + bufferedReader.close(); + } + + private static void makeNumber(final String num) { + int n = Integer.parseInt(num); + + if (map[n][1] == 1) + fill(0, 0, left + 1, left + S, "-"); + if (map[n][4] == 1) + fill(S + 1, S + 1, left + 1, left + S, "-"); + if (map[n][7] == 1) + fill(2 * (S + 1), 2 * (S + 1), left + 1, left + S, "-"); + if (map[n][2] == 1) + fill(1, S, left, left, "|"); + if (map[n][5] == 1) + fill(1 + (S + 1), S + (S + 1), left, left, "|"); + if (map[n][3] == 1) + fill(1, S, left + S + 1, left + S + 1, "|"); + if (map[n][6] == 1) + fill(1 + (S + 1), S + (S + 1), left + S + 1, left + S + 1, "|"); + + left += S + 3; + } + + private static void fillEmptyBoard() { + for (int i = 0; i < 2 * S + 3; i++) { + for (int j = 0; j < N * (S + 4); j++) { + board[i][j] = " "; + } + } + } + + public static void result() { + for (int i = 0; i < 2 * S + 3; i++) { + for (int j = 0; j < left; j++) { + System.out.print(board[i][j]); + } + System.out.println(); + } + } + + public static void fill(int sx, int ex, int sy, int ey, String str) { + for (int x = sx; x <= ex; x++) + for (int y = sy; y <= ey; y++) + board[x][y] = str; + } +} diff --git a/src/main/java/changhyeon/PRO_HIndex.java b/src/main/java/changhyeon/PRO_HIndex.java new file mode 100644 index 0000000..9fc7e13 --- /dev/null +++ b/src/main/java/changhyeon/PRO_HIndex.java @@ -0,0 +1,24 @@ +package changhyeon; + +import java.util.Arrays; + +public class PRO_HIndex { + + public int solution(int[] citations) { + int answer = 0; + Arrays.sort(citations); + + for (int i = 0; i < citations.length; i++) { + int smaller = Math.min(citations[i], citations.length - i); + + if (smaller >= answer) { + answer = Math.max(answer, smaller); + continue; + } + + break; + } + + return answer; + } +} diff --git "a/src/main/java/changhyeon/PRO_K\353\262\210\354\247\270\354\210\230.java" "b/src/main/java/changhyeon/PRO_K\353\262\210\354\247\270\354\210\230.java" new file mode 100644 index 0000000..8bb2355 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_K\353\262\210\354\247\270\354\210\230.java" @@ -0,0 +1,39 @@ +package changhyeon; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class PRO_K번째수 { + + public int[] solution(int[] array, int[][] commands) { + int[] answer = new int[commands.length]; + + for (int i = 0; i < commands.length; i++) { + answer[i] = doProcess(array, commands[i]); + } + + return answer; + } + + private int doProcess(int[] array, int[] command) { + int start = command[0]; + int end = command[1]; + int select = command[2]; + List slicedArray = slice(array, start, end); + + Collections.sort(slicedArray); + + return slicedArray.get(select - 1); + } + + private List slice(int[] array, int start, int end) { + List slicedArray = new ArrayList<>(); + + for (int i = start - 1; i <= end - 1; i++) { + slicedArray.add(array[i]); + } + + return slicedArray; + } +} diff --git "a/src/main/java/changhyeon/PRO_\352\260\231\354\235\200\354\210\253\354\236\220\353\212\224\354\213\253\354\226\264.java" "b/src/main/java/changhyeon/PRO_\352\260\231\354\235\200\354\210\253\354\236\220\353\212\224\354\213\253\354\226\264.java" new file mode 100644 index 0000000..30685a8 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\352\260\231\354\235\200\354\210\253\354\236\220\353\212\224\354\213\253\354\226\264.java" @@ -0,0 +1,30 @@ +package changhyeon; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class PRO_같은숫자는싫어 { + + public int[] solution(int[] arr) { + Deque deque = new ArrayDeque<>(); + + for (int number : arr) { + if (deque.isEmpty()) { + deque.add(number); + continue; + } + + if (deque.peekLast() != number) { + deque.add(number); + } + } + + int[] answer = new int[deque.size()]; + + for (int i = 0; i < answer.length; i++) { + answer[i] = deque.poll(); + } + + return answer; + } +} diff --git "a/src/main/java/changhyeon/PRO_\352\262\214\354\236\204\353\247\265\354\265\234\353\213\250\352\261\260\353\246\254.java" "b/src/main/java/changhyeon/PRO_\352\262\214\354\236\204\353\247\265\354\265\234\353\213\250\352\261\260\353\246\254.java" new file mode 100644 index 0000000..bc92ced --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\352\262\214\354\236\204\353\247\265\354\265\234\353\213\250\352\261\260\353\246\254.java" @@ -0,0 +1,47 @@ +package changhyeon; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class PRO_게임맵최단거리 { + + private static int[] mx = {0, 0, -1, 1}; + private static int[] my = {-1, 1, 0, 0}; + private static boolean[][] visited; + private static int WIDTH = 5, HEIGHT = 5; + + public int solution(int[][] maps) { + visited = new boolean[maps.length][maps[0].length]; + WIDTH = maps[0].length; + HEIGHT = maps.length; + + bfs(0, 0, maps); + + if (maps[HEIGHT - 1][WIDTH - 1] == 1) { + return -1; + } + + return maps[HEIGHT - 1][WIDTH - 1]; + } + + private void bfs(int x, int y, int[][] maps) { + Deque deque = new ArrayDeque<>(); + deque.add(new int[] {x, y}); + visited[y][x] = true; + + while (!deque.isEmpty()) { + int[] cur = deque.poll(); + + for (int i = 0; i < 4; i++) { + int dx = mx[i] + cur[0]; + int dy = my[i] + cur[1]; + + if (dx >= 0 && dy >= 0 && dx < WIDTH && dy < HEIGHT && maps[dy][dx] != 0 && !visited[dy][dx]) { + visited[dy][dx] = true; + maps[dy][dx] = maps[cur[1]][cur[0]] + 1; + deque.add(new int[] {dx, dy}); + } + } + } + } +} diff --git "a/src/main/java/changhyeon/PRO_\352\265\254\353\252\205\353\263\264\355\212\270.java" "b/src/main/java/changhyeon/PRO_\352\265\254\353\252\205\353\263\264\355\212\270.java" new file mode 100644 index 0000000..c2b3023 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\352\265\254\353\252\205\353\263\264\355\212\270.java" @@ -0,0 +1,40 @@ +package changhyeon; + +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Deque; + +public class PRO_구명보트 { + + public int solution(int[] people, int limit) { + int count = 0; + Arrays.sort(people); + Deque deque = new ArrayDeque<>(); + + for (int i = people.length - 1; i >= 0; i--) { + deque.add(people[i]); + } + + while (deque.size() >= 2) { + int first = deque.peekFirst(); + int last = deque.peekLast(); + + if (first + last <= limit) { + deque.pollFirst(); + deque.pollLast(); + count++; + continue; + } + + deque.poll(); + count++; + } + + if (!deque.isEmpty()) { + deque.poll(); + count++; + } + + return count; + } +} diff --git "a/src/main/java/changhyeon/PRO_\352\270\260\353\212\245\352\260\234\353\260\234.java" "b/src/main/java/changhyeon/PRO_\352\270\260\353\212\245\352\260\234\353\260\234.java" new file mode 100644 index 0000000..5d30870 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\352\270\260\353\212\245\352\260\234\353\260\234.java" @@ -0,0 +1,48 @@ +package changhyeon; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.List; + +public class PRO_기능개발 { + + public int[] solution(int[] progresses, int[] speeds) { + List answer = new ArrayList<>(); + Deque deque = new ArrayDeque<>(); + + for (int i = 0; i < progresses.length; i++) { + int progress = progresses[i]; + int count = 0; + + while (progress < 100) { + count++; + progress += speeds[i]; + } + + deque.add(count); + } + + int count = 0; + int number = 0; + while (!deque.isEmpty()) { + if (number >= deque.peek()) { + deque.poll(); + } else { + number = deque.poll(); + } + + count++; + + if (deque.isEmpty()) { + answer.add(count); + } else if (deque.peek() > number) { + answer.add(count); + count = 0; + number = 0; + } + } + + return answer.stream().mapToInt(i -> i).toArray(); + } +} diff --git "a/src/main/java/changhyeon/PRO_\353\204\244\355\212\270\354\233\214\355\201\254.java" "b/src/main/java/changhyeon/PRO_\353\204\244\355\212\270\354\233\214\355\201\254.java" new file mode 100644 index 0000000..48000ea --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\353\204\244\355\212\270\354\233\214\355\201\254.java" @@ -0,0 +1,27 @@ +package changhyeon; + +public class PRO_네트워크 { + + public int solution(int n, int[][] computers) { + int count = 0; + boolean[] visited = new boolean[n]; + + for (int i = 0; i < n; i++) { + if (!visited[i]) { + count++; + dfs(i, computers, visited); + } + } + + return count; + } + + private void dfs(int index, int[][] computers, boolean[] visited) { + visited[index] = true; + for (int i = 0; i < computers[index].length; i++) { + if (computers[index][i] == 1 && !visited[i]) { + dfs(i, computers, visited); + } + } + } +} diff --git "a/src/main/java/changhyeon/PRO_\353\213\244\353\246\254\353\245\274\354\247\200\353\202\230\353\212\224\355\212\270\353\237\255.java" "b/src/main/java/changhyeon/PRO_\353\213\244\353\246\254\353\245\274\354\247\200\353\202\230\353\212\224\355\212\270\353\237\255.java" new file mode 100644 index 0000000..ca6902f --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\353\213\244\353\246\254\353\245\274\354\247\200\353\202\230\353\212\224\355\212\270\353\237\255.java" @@ -0,0 +1,75 @@ +package changhyeon; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class PRO_다리를지나는트럭 { + + public int solution(int bridge_length, int weight, int[] truck_weights) { + int sum = 0; + int count = 0; + + Deque wait = new ArrayDeque<>(); + Deque during = new ArrayDeque<>(); + + for (int truckWeight : truck_weights) { + wait.add(truckWeight); + } + + while (true) { + if (wait.isEmpty() && during.isEmpty()) { + break; + } + + count++; + Truck duringTruck = during.peek(); + + if (duringTruck != null) { + if (duringTruck.getIndex() == bridge_length) { + during.poll(); + sum -= duringTruck.getWeight(); + } + + for (Truck truck : during) { + truck.updateIndex(); + } + } + + Integer truckWeight = wait.peek(); + if (truckWeight != null) { + if (weight < (truckWeight + sum) || during.size() >= bridge_length) { + continue; + } + + sum += truckWeight; + truckWeight = wait.poll(); + during.add(new Truck(1, truckWeight)); + } + } + + return count; + } + + static class Truck { + + private int index; + private int weight; + + public Truck(int index, int weight) { + this.index = index; + this.weight = weight; + } + + public void updateIndex() { + index++; + } + + public int getIndex() { + return index; + } + + public int getWeight() { + return weight; + } + } +} diff --git "a/src/main/java/changhyeon/PRO_\353\215\224\353\247\265\352\262\214.java" "b/src/main/java/changhyeon/PRO_\353\215\224\353\247\265\352\262\214.java" new file mode 100644 index 0000000..2426cc6 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\353\215\224\353\247\265\352\262\214.java" @@ -0,0 +1,35 @@ +package changhyeon; + +import java.util.PriorityQueue; + +public class PRO_더맵게 { + + public int solution(int[] scoville, int K) { + int answer = 0; + PriorityQueue priorityQueue = new PriorityQueue<>(); + + for (int number : scoville) { + priorityQueue.add(number); + } + + while (true) { + if (priorityQueue.size() < 2 && priorityQueue.peek() < K) { + return -1; + } + + Integer num1 = priorityQueue.peek(); + + if (num1 >= K) { + break; + } + + priorityQueue.poll(); + int num2 = priorityQueue.poll(); + int newNumber = num1 + (num2 * 2); + priorityQueue.add(newNumber); + answer++; + } + + return answer; + } +} diff --git "a/src/main/java/changhyeon/PRO_\353\224\224\354\212\244\355\201\254\354\273\250\355\212\270\353\241\244\353\237\254.java" "b/src/main/java/changhyeon/PRO_\353\224\224\354\212\244\355\201\254\354\273\250\355\212\270\353\241\244\353\237\254.java" new file mode 100644 index 0000000..6f0d1f8 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\353\224\224\354\212\244\355\201\254\354\273\250\355\212\270\353\241\244\353\237\254.java" @@ -0,0 +1,37 @@ +package changhyeon; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.PriorityQueue; + +public class PRO_디스크컨트롤러 { + + public int solution(int[][] jobs) { + Arrays.sort(jobs, Comparator.comparingInt(job -> job[0])); + + PriorityQueue priorityQueue = new PriorityQueue<>((job1, job2) -> job1[1] - job2[1]); + + int index = 0; + int count = 0; + int total = 0; + int end = 0; + + while (count < jobs.length) { + + while (index < jobs.length && jobs[index][0] <= end) { + priorityQueue.add(jobs[index++]); + } + + if (priorityQueue.isEmpty()) { + end = jobs[index][0]; + } else { + int[] cur = priorityQueue.poll(); + total += cur[1] + end - cur[0]; + end += cur[1]; + count++; + } + } + + return total / jobs.length; + } +} diff --git "a/src/main/java/changhyeon/PRO_\353\252\250\354\235\214\354\202\254\354\240\204.java" "b/src/main/java/changhyeon/PRO_\353\252\250\354\235\214\354\202\254\354\240\204.java" new file mode 100644 index 0000000..48c7f09 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\353\252\250\354\235\214\354\202\254\354\240\204.java" @@ -0,0 +1,31 @@ +package changhyeon; + +public class PRO_모음사전 { + + private final static String[] stringArray = {"A", "E", "I", "O", "U"}; + private final static int MAX_NUMBER = 5; + private static boolean flag = false; + private static int sum = 0; + + public int solution(String word) { + recursive(0, "", word); + + return sum - 1; + } + + private void recursive(int count, String currentWord, String word) { + if (count > MAX_NUMBER || flag) { + return; + } + + sum++; + + if (currentWord.equals(word)) { + flag = true; + } + + for (int i = 0; i < stringArray.length; i++) { + recursive(count + 1, currentWord + stringArray[i], word); + } + } +} diff --git "a/src/main/java/changhyeon/PRO_\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.java" "b/src/main/java/changhyeon/PRO_\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.java" new file mode 100644 index 0000000..331a3e2 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.java" @@ -0,0 +1,65 @@ +package changhyeon; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class PRO_베스트앨범 { + + public int[] solution(String[] genres, int[] plays) { + List answer = new ArrayList<>(); + + Map map = new HashMap<>(); + ArrayList sortedGenres = new ArrayList<>(); + + for (int i = 0; i < genres.length; i++) { + map.put(genres[i], map.getOrDefault(genres[i], 0) + plays[i]); + } + + for (String genre : map.keySet()) { + sortedGenres.add(genre); + } + + sortedGenres.sort((g1, g2) -> map.get(g2) - map.get(g1)); + + for (String genre : sortedGenres) { + List musics = new ArrayList<>(); + for (int i = 0; i < genres.length; i++) { + if (genre.equals(genres[i])) { + musics.add(new music(i, plays[i])); + } + } + + musics.sort(new Comparator() { + public int compare(music m1, music m2) { + if (m1.play == m2.play) { + return m1.index - m2.index; + } + return m2.play - m1.play; + } + }); + + answer.add(musics.get(0).index); + if (musics.size() != 1) { + answer.add(musics.get(1).index); + } + } + + System.out.println(answer.toString()); + + return answer.stream().mapToInt(i -> i).toArray(); + } + + public static class music { + + int index; + int play; + + public music(int index, int play) { + this.index = index; + this.play = play; + } + } +} diff --git "a/src/main/java/changhyeon/PRO_\354\206\214\354\210\230\354\260\276\352\270\260.java" "b/src/main/java/changhyeon/PRO_\354\206\214\354\210\230\354\260\276\352\270\260.java" new file mode 100644 index 0000000..0864cc1 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\354\206\214\354\210\230\354\260\276\352\270\260.java" @@ -0,0 +1,58 @@ +package changhyeon; + +import java.util.HashSet; +import java.util.Set; + +public class PRO_소수찾기 { + + private boolean[] visited; + private Set set = new HashSet<>(); + + public int solution(String numbers) { + int answer = 0; + + for (int i = 0; i < numbers.length(); i++) { + if (numbers.charAt(i) == '0') + continue; + visited = new boolean[numbers.length()]; + visited[i] = true; + recursive(numbers, 0, String.valueOf(numbers.charAt(i))); + } + + return set.size(); + } + + private void recursive(String numbers, int count, String str) { + if (count == numbers.length()) { + return; + } + + if (isPrime(str)) { + set.add(str); + } + + for (int i = 0; i < numbers.length(); i++) { + if (!visited[i]) { + visited[i] = true; + recursive(numbers, count + 1, str + String.valueOf(numbers.charAt(i))); + visited[i] = false; + } + } + } + + private boolean isPrime(String str) { + int number = Integer.parseInt(str); + int sum = 0; + + if (number == 0 || number == 1) + return false; + + for (int i = 2; i <= number / 2; i++) { + if (number % i == 0) { + return false; + } + } + + return true; + } +} diff --git "a/src/main/java/changhyeon/PRO_\354\230\254\353\260\224\353\245\270\352\264\204\355\230\270.java" "b/src/main/java/changhyeon/PRO_\354\230\254\353\260\224\353\245\270\352\264\204\355\230\270.java" new file mode 100644 index 0000000..3345093 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\354\230\254\353\260\224\353\245\270\352\264\204\355\230\270.java" @@ -0,0 +1,24 @@ +package changhyeon; + +public class PRO_올바른괄호 { + + boolean solution(String s) { + boolean answer = true; + int count = 0; + String[] arr = s.split(""); + + for (int i = 0; i < arr.length; i++) { + if (count == 0 && arr[i].equals(")")) { + return false; + } + + if (arr[i].equals("(")) { + count++; + } else { + count--; + } + } + + return count == 0 ? true : false; + } +} diff --git "a/src/main/java/changhyeon/PRO_\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.java" "b/src/main/java/changhyeon/PRO_\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.java" new file mode 100644 index 0000000..161d684 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.java" @@ -0,0 +1,34 @@ +package changhyeon; + +import java.util.HashMap; +import java.util.Map; + +public class PRO_완주하지못한선수 { + + public String solution(String[] participant, String[] completion) { + String answer = ""; + + Map map = new HashMap<>(); + + for (int i = 0; i < participant.length; i++) { + if (!map.containsKey(participant[i])) { + map.put(participant[i], 1); + continue; + } + + map.put(participant[i], map.get(participant[i]) + 1); + } + + for (int i = 0; i < completion.length; i++) { + map.put(completion[i], map.get(completion[i]) - 1); + } + + for (Map.Entry entrySet : map.entrySet()) { + if (entrySet.getValue() >= 1) { + return entrySet.getKey(); + } + } + + return answer; + } +} diff --git "a/src/main/java/changhyeon/PRO_\354\235\230\354\203\201.java" "b/src/main/java/changhyeon/PRO_\354\235\230\354\203\201.java" new file mode 100644 index 0000000..c8699ae --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\354\235\230\354\203\201.java" @@ -0,0 +1,24 @@ +package changhyeon; + +import java.util.HashMap; +import java.util.Map; + +public class PRO_의상 { + + public int solution(String[][] clothes) { + int sum = 1; + + Map map = new HashMap<>(); + + for (int i = 0; i < clothes.length; i++) { + String category = clothes[i][1]; + map.put(category, map.getOrDefault(category, 0) + 1); + } + + for (int num : map.values()) { + sum *= (num + 1); // 안 입었을 때의 경우 1 추가 + } + + return sum - 1; + } +} diff --git "a/src/main/java/changhyeon/PRO_\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.java" "b/src/main/java/changhyeon/PRO_\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.java" new file mode 100644 index 0000000..bfab4a4 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.java" @@ -0,0 +1,58 @@ +package changhyeon; + +import java.util.PriorityQueue; + +public class PRO_이중우선순위큐 { + + PriorityQueue queue = new PriorityQueue<>(); + + public int[] solution(String[] operations) { + for (String operation : operations) { + String[] line = operation.split(" "); + String order = line[0]; + int number = Integer.parseInt(line[1]); + + if (order.equals("I")) { + addNumber(number); + continue; + } + + deleteNumber(number); + } + + if (queue.isEmpty()) { // 큐가 비었을 때 + return new int[] {0, 0}; + } + + int max = -10000000; + for (int num : queue) { // 최대 값 + if (num > max) { + max = num; + } + } + + return new int[] {max, queue.poll()}; + } + + private void addNumber(int number) { + queue.add(number); + } + + private void deleteNumber(int number) { + if (!queue.isEmpty()) { // 큐가 비지 않았을 때 + if (number == 1) { // 1이면 최대 값 삭제 + int max = -10000000; + for (int num : queue) { + if (num > max) { + max = num; + } + } + + queue.remove(max); + return; + } + + queue.poll(); // -1이면 최소 값 삭제 + } + } +} diff --git "a/src/main/java/changhyeon/PRO_\354\240\204\353\240\245\353\247\235\353\221\230\353\241\234\353\202\230\353\210\204\352\270\260.java" "b/src/main/java/changhyeon/PRO_\354\240\204\353\240\245\353\247\235\353\221\230\353\241\234\353\202\230\353\210\204\352\270\260.java" new file mode 100644 index 0000000..12e2503 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\354\240\204\353\240\245\353\247\235\353\221\230\353\241\234\353\202\230\353\210\204\352\270\260.java" @@ -0,0 +1,58 @@ +package changhyeon; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class PRO_전력망둘로나누기 { + + private static int[][] map; + + public int solution(int n, int[][] wires) { + int answer = n; + + map = new int[n + 1][n + 1]; + + for (int i = 0; i < wires.length; i++) { + int from = wires[i][0]; + int to = wires[i][1]; + + map[from][to] = 1; + map[to][from] = 1; + } + + for (int i = 0; i < wires.length; i++) { + int from = wires[i][0]; + int to = wires[i][1]; + + map[from][to] = 0; + map[to][from] = 0; + answer = Math.min(answer, bfs(n, from)); + map[from][to] = 1; + map[to][from] = 1; + } + + return answer; + } + + private int bfs(int n, int from) { + boolean[] visited = new boolean[n + 1]; + Deque deque = new ArrayDeque<>(); + int count = 1; + deque.add(from); + visited[from] = true; + + while (!deque.isEmpty()) { + int temp = deque.poll(); + + for (int i = 1; i <= n; i++) { + if (!visited[i] && map[temp][i] == 1) { + visited[i] = true; + deque.add(i); + count++; + } + } + } + + return (int)Math.abs(count - (n - count)); + } +} diff --git "a/src/main/java/changhyeon/PRO_\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.java" "b/src/main/java/changhyeon/PRO_\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.java" new file mode 100644 index 0000000..f0bfe30 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.java" @@ -0,0 +1,21 @@ +package changhyeon; + +import java.util.Arrays; + +public class PRO_전화번호목록 { + + public boolean solution(String[] phone_book) { + + Arrays.sort(phone_book); + + System.out.println(Arrays.toString(phone_book)); + + for (int i = 0; i < phone_book.length - 1; i++) { + if (phone_book[i + 1].startsWith(phone_book[i])) { + return false; + } + } + + return true; + } +} diff --git "a/src/main/java/changhyeon/PRO_\354\241\260\354\235\264\354\212\244\355\213\261.java" "b/src/main/java/changhyeon/PRO_\354\241\260\354\235\264\354\212\244\355\213\261.java" new file mode 100644 index 0000000..f2b077c --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\354\241\260\354\235\264\354\212\244\355\213\261.java" @@ -0,0 +1,22 @@ +package changhyeon; + +public class PRO_조이스틱 { + + public int solution(String name) { + int result = 0; + int idx; + int move = name.length() - 1; + + for (int i = 0; i < name.length(); i++) { + result += Math.min(name.charAt(i) - 'A', 'Z' - name.charAt(i) + 1); + + idx = i + 1; + while (idx < name.length() && name.charAt(idx) == 'A') { + idx++; + } + move = Math.min(move, (i * 2) + name.length() - idx); // 순서대로, 반대로 중 적은 횟수 값 + move = Math.min(move, (name.length() - idx) * 2 + i); // 뒷부분 먼저 + } + return result + move; + } +} diff --git "a/src/main/java/changhyeon/PRO_\354\243\274\354\213\235\352\260\200\352\262\251.java" "b/src/main/java/changhyeon/PRO_\354\243\274\354\213\235\352\260\200\352\262\251.java" new file mode 100644 index 0000000..055bc34 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\354\243\274\354\213\235\352\260\200\352\262\251.java" @@ -0,0 +1,54 @@ +package changhyeon; + +import java.util.ArrayDeque; +import java.util.Deque; + +public class PRO_주식가격 { + + public int[] solution(int[] prices) { + int[] answer = new int[prices.length]; + Deque stocks = new ArrayDeque<>(); + + for (int price : prices) { + stocks.add(new Stock(price)); + } + + for (int i = 0; i < prices.length; i++) { + Stock stock = stocks.poll(); + + for (int j = i + 1; j < prices.length; j++) { + stock.increaseSecond(); + if (stock.getPrice() > prices[j]) { + break; + } + } + + answer[i] = stock.getSecond(); + } + + return answer; + } + + static class Stock { + + private int second; + private int price; + + public Stock(int price) { + this.second = 0; + this.price = price; + } + + public int getSecond() { + return second; + } + + public int getPrice() { + return price; + } + + public void increaseSecond() { + second++; + } + } +} diff --git "a/src/main/java/changhyeon/PRO_\354\262\264\354\234\241\353\263\265.java" "b/src/main/java/changhyeon/PRO_\354\262\264\354\234\241\353\263\265.java" new file mode 100644 index 0000000..f32c910 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\354\262\264\354\234\241\353\263\265.java" @@ -0,0 +1,47 @@ +package changhyeon; + +import java.util.ArrayList; +import java.util.Arrays; + +public class PRO_체육복 { + + public int solution(int n, int[] lost, int[] reserve) { + ArrayList reserveList = new ArrayList<>(); + ArrayList lostList = new ArrayList<>(); + int count = 0; + Arrays.sort(lost); + Arrays.sort(reserve); + + for (int i : reserve) { + reserveList.add(i); + } + + for (int i : lost) { + lostList.add(i); + } + + for (int num : lost) { + if (lostList.size() != 0 && reserveList.size() != 0 && reserveList.contains(num)) { + lostList.remove(Integer.valueOf(num)); + reserveList.remove(Integer.valueOf(num)); + count++; + } + } + + for (int num : lostList) { + if (reserveList.size() != 0 && reserveList.contains(num - 1)) { + reserveList.remove(Integer.valueOf(num - 1)); + count++; + continue; + } + if (reserveList.size() != 0 && reserveList.contains(num + 1)) { + reserveList.remove(Integer.valueOf(num + 1)); + count++; + continue; + } + + } + + return n - lost.length + count; + } +} diff --git "a/src/main/java/changhyeon/PRO_\354\271\264\355\216\253.java" "b/src/main/java/changhyeon/PRO_\354\271\264\355\216\253.java" new file mode 100644 index 0000000..104edec --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\354\271\264\355\216\253.java" @@ -0,0 +1,24 @@ +package changhyeon; + +public class PRO_카펫 { + + public int[] solution(int brown, int yellow) { + int[] answer = new int[2]; + + for (int width = 1; width <= 5000; width++) { + for (int height = 1; height <= width; height++) { + + int brownTile = (width + height - 2) * 2; + int yellowTile = width * height - brownTile; + + if (brownTile == brown && yellowTile == yellow) { + answer[0] = width; + answer[1] = height; + break; + } + } + } + + return answer; + } +} diff --git "a/src/main/java/changhyeon/PRO_\355\201\260\354\210\230\353\247\214\353\223\244\352\270\260.java" "b/src/main/java/changhyeon/PRO_\355\201\260\354\210\230\353\247\214\353\223\244\352\270\260.java" new file mode 100644 index 0000000..36abf7e --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\355\201\260\354\210\230\353\247\214\353\223\244\352\270\260.java" @@ -0,0 +1,24 @@ +package changhyeon; + +public class PRO_큰수만들기 { + + private static StringBuilder sb = new StringBuilder(); + + public String solution(String number, int k) { + int index = 0; + int max; + + for (int i = 0; i < number.length() - k; i++) { + max = 0; + for (int j = index; j <= k + i; j++) { + if (max < number.charAt(j) - '0') { + max = number.charAt(j) - '0'; + index = j + 1; + } + } + + sb.append(max); + } + return sb.toString(); + } +} diff --git "a/src/main/java/changhyeon/PRO_\355\203\200\352\262\237\353\204\230\353\262\204.java" "b/src/main/java/changhyeon/PRO_\355\203\200\352\262\237\353\204\230\353\262\204.java" new file mode 100644 index 0000000..c9cc966 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\355\203\200\352\262\237\353\204\230\353\262\204.java" @@ -0,0 +1,23 @@ +package changhyeon; + +public class PRO_타겟넘버 { + + public int solution(int[] numbers, int target) { + return dfs(numbers, target, 0, 0); + } + + private int dfs(int[] numbers, int target, int depth, int sum) { + int matchCount = 0; + + if (depth == numbers.length) { + if (sum == target) { + return 1; + } + return 0; + } + + matchCount += dfs(numbers, target, depth + 1, sum + numbers[depth]); + matchCount += dfs(numbers, target, depth + 1, sum - numbers[depth]); + return matchCount; + } +} diff --git "a/src/main/java/changhyeon/PRO_\355\217\260\354\274\223\353\252\254.java" "b/src/main/java/changhyeon/PRO_\355\217\260\354\274\223\353\252\254.java" new file mode 100644 index 0000000..1b1dc54 --- /dev/null +++ "b/src/main/java/changhyeon/PRO_\355\217\260\354\274\223\353\252\254.java" @@ -0,0 +1,23 @@ +package changhyeon; + +import java.util.HashSet; +import java.util.Set; + +public class PRO_폰켓몬 { + + public int solution(int[] nums) { + int N = nums.length / 2; + + Set set = new HashSet<>(); + + for (int num : nums) { + set.add(num); + } + + if (set.size() >= N) { + return N; + } + + return set.size(); + } +} diff --git a/src/main/java/tommy/BOJ_1261.java b/src/main/java/tommy/BOJ_1261.java new file mode 100644 index 0000000..0347672 --- /dev/null +++ b/src/main/java/tommy/BOJ_1261.java @@ -0,0 +1,79 @@ +package tommy; + +import java.util.PriorityQueue; +import java.util.Scanner; + +public class BOJ_1261 { + static int m; + static int n; + static int[] dx = {-1, 1, 0, 0}; + static int[] dy = {0, 0, -1, 1}; + static int[][] map; + static boolean[][] visited; + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + m = sc.nextInt(); // 4 + n = sc.nextInt(); // 2 + sc.nextLine(); + map = new int[n + 1][m + 1]; + visited = new boolean[n + 1][m + 1]; + for (int i = 1; i <= n; i++) { + String line = sc.nextLine(); + for (int j = 1; j <= m; j++) { + map[i][j] = line.charAt(j - 1) - '0'; + } + } + bfs(1,1); + } + + private static void bfs(int x, int y) { + // priority queue의 경우 Node의 count가 작은 것 부터 우선 탐색을 하기 때문에 보다 빠른 경로를 찾을 수 있다. + PriorityQueue queue = new PriorityQueue<>(); + queue.offer(new Node(x, y, 0)); + visited[x][y] = true; + + while (!queue.isEmpty()) { + Node now = queue.poll(); + + if (now.x == n && now.y == m) { + System.out.println(now.count); + } + + for (int i = 0; i < 4; i++) { + int nx = now.x + dx[i]; + int ny = now.y + dy[i]; + + if (nx < 1 || ny < 1 || nx > n || ny > m){ + continue; + } + + if (!visited[nx][ny] && map[nx][ny] == 0) { + visited[nx][ny] = true; + queue.offer(new Node(nx, ny, now.count)); + } + + if (!visited[nx][ny] && map[nx][ny] == 1) { + visited[nx][ny] = true; + queue.offer(new Node(nx, ny, now.count + 1)); + } + } + } + } + + static class Node implements Comparable{ + int x; + int y; + int count; + + public Node(int x, int y, int count) { + this.x = x; + this.y = y; + this.count = count; + } + + @Override + public int compareTo(Node node) { + return count - node.count; + } + } +} diff --git a/src/main/java/tommy/BOJ_13549.java b/src/main/java/tommy/BOJ_13549.java new file mode 100644 index 0000000..993135e --- /dev/null +++ b/src/main/java/tommy/BOJ_13549.java @@ -0,0 +1,48 @@ +package tommy; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_13549 { + static int[] visited = new int[100001]; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int k = sc.nextInt(); + if (n == k) { + System.out.println(0); + } else { + bfs(n, k); + } + } + + private static void bfs(int n, int k) { + Queue queue = new LinkedList(); + queue.offer(n); + visited[n] = 1; + + while (!queue.isEmpty()) { + int now = queue.poll(); + if (now == k) { + System.out.println(visited[now] - 1); + return; + } + + if (now * 2 <= 100000 && visited[now * 2] == 0) { + queue.offer(now * 2); + visited[now * 2] = visited[now]; + } + if (now - 1 >= 0 && visited[now - 1] == 0) { + queue.offer(now - 1); + visited[now - 1] = visited[now] + 1; + } + if (now + 1 <= 100000 && visited[now + 1] == 0) { + queue.offer(now + 1); + visited[now + 1] = visited[now] + 1; + } + } + } + +} diff --git a/src/main/java/tommy/BOJ_13913.java b/src/main/java/tommy/BOJ_13913.java new file mode 100644 index 0000000..2615b69 --- /dev/null +++ b/src/main/java/tommy/BOJ_13913.java @@ -0,0 +1,69 @@ +package tommy; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; +import java.util.Stack; + +public class BOJ_13913 { + static int[] parent; + static int[] time; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int k = sc.nextInt(); + + if (n == k) { + System.out.println(0); + System.out.println(n); + } else { + bfs(n, k); + System.out.println(time[k] - 1); + + Stack stack = new Stack<>(); + stack.push(k); + int index = k; + while (index != n) { + stack.push(parent[index]); + index = parent[index]; + } + + StringBuilder sb = new StringBuilder(); + while (!stack.isEmpty()) { + sb.append(stack.pop()).append(" "); + } + System.out.println(sb); + } + } + + private static void bfs(int n, int k) { + Queue queue = new LinkedList<>(); + parent = new int[100001]; + time = new int[100001]; + queue.offer(n); + time[n] = 1; + + while (!queue.isEmpty()) { + Integer now = queue.poll(); + if (now == k) { + return; + } + if (now * 2 <= 100000 && time[now * 2] == 0) { + parent[now * 2] = now; + time[now * 2] = time[now] + 1; + queue.offer(now * 2); + } + if (now + 1 <= 100000 && time[now + 1] == 0) { + parent[now + 1] = now; + time[now + 1] = time[now] + 1; + queue.offer(now + 1); + } + if (now - 1 >= 0 && time[now - 1] == 0) { + parent[now - 1] = now; + time[now - 1] = time[now] + 1; + queue.offer(now - 1); + } + } + } +} diff --git a/src/main/java/tommy/BOJ_14226.java b/src/main/java/tommy/BOJ_14226.java new file mode 100644 index 0000000..f0874e7 --- /dev/null +++ b/src/main/java/tommy/BOJ_14226.java @@ -0,0 +1,59 @@ +package tommy; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_14226 { + + static boolean[][] visited = new boolean[1001][1001]; //[clipboard][total] + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int s = sc.nextInt(); + + bfs(s); + } + + private static void bfs(int s) { + Queue queue = new LinkedList<>(); + queue.offer(new Node(0, 1, 0)); + visited[0][1] = true; + + while (!queue.isEmpty()){ + Node current = queue.poll(); + + if (current.total == s){ + System.out.println(current.time); + return; + } + + // 1. 화면에 있는 이모티콘 클립 보드에 저장 + queue.offer(new Node(current.total, current.total, current.time + 1)); + + // 2. 클립보드에 있는 이모티콘 붙여넣기 + if (current.clipboard != 0 && current.total + current.clipboard <= s && !visited[current.clipboard][current.total + current.clipboard]) { + queue.offer(new Node(current.clipboard, current.total + current.clipboard, current.time + 1)); + visited[current.clipboard][current.total + current.clipboard] = true; + } + + // 3. 화면에 있는 이모티콘 중 하나 삭제 + if (current.total >= 1 && !visited[current.clipboard][current.total - 1]) { + queue.offer(new Node(current.clipboard, current.total - 1, current.time + 1)); + visited[current.clipboard][current.total + current.clipboard] = true; + } + } + } + + public static class Node{ + int clipboard; + int total; + int time; + + public Node(int clipboard, int total, int time) { + this.clipboard = clipboard; + this.total = total; + this.time = time; + } + } +} diff --git a/src/main/java/tommy/BOJ_14499.java b/src/main/java/tommy/BOJ_14499.java new file mode 100644 index 0000000..2740402 --- /dev/null +++ b/src/main/java/tommy/BOJ_14499.java @@ -0,0 +1,89 @@ +package tommy; + +import java.util.Scanner; + +public class BOJ_14499 { + static int N, M, x, y; + static int[][] map; + static int[] dice = new int[7]; + // 동 서 북 남 + static int[] dx = {0, 0, -1, 1}; + static int[] dy = {1, -1, 0, 0}; + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + N = sc.nextInt(); + M = sc.nextInt(); + x = sc.nextInt(); + y = sc.nextInt(); + int K = sc.nextInt(); + + map = new int[N][M]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + map[i][j] = sc.nextInt(); + } + } + + for (int k = 0; k < K; k++) { + int direction = sc.nextInt() - 1; + int nx = x + dx[direction]; + int ny = y + dy[direction]; + if (nx < 0 || ny < 0 || nx >= N || ny >= M) { + continue; + } else { + rotation(nx, ny, direction); + } + x = nx; + y = ny; + } + + System.out.println(sb); + } + + private static void rotation(int nx, int ny, int direction) { + int temp; + switch (direction) { + // 동 + case 0: + temp = dice[1]; + dice[1] = dice[4]; + dice[4] = dice[6]; + dice[6] = dice[3]; + dice[3] = temp; + break; + // 서 + case 1: + temp = dice[1]; + dice[1] = dice[3]; + dice[3] = dice[6]; + dice[6] = dice[4]; + dice[4] = temp; + break; + // 북 + case 2: + temp = dice[1]; + dice[1] = dice[5]; + dice[5] = dice[6]; + dice[6] = dice[2]; + dice[2] = temp; + break; + // 남 + case 3: + temp = dice[1]; + dice[1] = dice[2]; + dice[2] = dice[6]; + dice[6] = dice[5]; + dice[5] = temp; + break; + } + sb.append(dice[1]).append("\n"); + if (map[nx][ny] == 0) { + map[nx][ny] = dice[6]; + } else { + dice[6] = map[nx][ny]; + map[nx][ny] = 0; + } + } +} diff --git a/src/main/java/tommy/BOJ_14890.java b/src/main/java/tommy/BOJ_14890.java new file mode 100644 index 0000000..51666d1 --- /dev/null +++ b/src/main/java/tommy/BOJ_14890.java @@ -0,0 +1,77 @@ +package tommy; + +import java.util.Scanner; + +public class BOJ_14890 { + static int N; + static int L; + static int[][] map; + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + N = sc.nextInt(); + L = sc.nextInt(); + + map = new int[N][N]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + map[i][j] = sc.nextInt(); + } + } + + int answer = 0; + for (int i = 0; i < N; i++) { + if (checkRow(i)) answer++; + if (checkCol(i)) answer++; + } + + System.out.println(answer); + } + + private static boolean checkRow(int row) { + boolean[] isInstalled = new boolean[N]; + + for (int i = 0; i < N - 1; i++) { + int diff = map[row][i] - map[row][i + 1]; + if (diff < -1 || diff > 1) return false; + else if (diff == -1) { // 경사로가 높아지는 경우 + for (int j = 0; j < L; j++) { + // 범위를 벗어나거나 이미 설치한 경우 + if (i - j < 0 || isInstalled[i - j]) return false; + if (map[row][i] != map[row][i - j]) return false; + isInstalled[i - j] = true; + } + } else if (diff == 1) { // 경사로가 낮아지는 경우 + for (int j = 1; j <= L; j++) { + if (i + j >= N || isInstalled[i + j]) return false; + if (map[row][i] - 1 != map[row][i + j]) return false; + isInstalled[i + j] = true; + } + } + } + return true; + } + + private static boolean checkCol(int col) { + boolean[] isInstalled = new boolean[N]; + + for (int i = 0; i < N - 1; i++) { + int diff = map[i][col] - map[i + 1][col]; + + if (diff < -1 || diff > 1) return false; + else if (diff == -1) { // 경사로가 높아지는 경우 + for (int j = 0; j < L; j++) { + if (i - j < 0 || isInstalled[i - j]) return false; + if (map[i][col] != map[i - j][col]) return false; + isInstalled[i - j] = true; + } + } else if (diff == 1) { // 경사로가 낮아지는 경우 + for (int j = 1; j <= L; j++) { + if (i + j >= N || isInstalled[i+j]) return false; + if (map[i][col] -1 != map[i + j][col]) return false; + isInstalled[i + j] = true; + } + } + } + return true; + } +} diff --git a/src/main/java/tommy/BOJ_15662.java b/src/main/java/tommy/BOJ_15662.java new file mode 100644 index 0000000..bf6db24 --- /dev/null +++ b/src/main/java/tommy/BOJ_15662.java @@ -0,0 +1,67 @@ +package tommy; + +import java.util.Scanner; + +public class BOJ_15662 { + static int T; // 톱니 바퀴의 수 + static int[][] chain; + static boolean[] visited; + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + T = sc.nextInt(); + + // Initializing Chain + chain = new int[T][8]; // 날은 8개 + for (int i = 0; i < T; i++) { + String tmp = sc.next(); + for (int j = 0; j < tmp.length(); j++) { + chain[i][j] = tmp.charAt(j) - '0'; + } + } + + int K = sc.nextInt(); + for (int k = 0; k < K; k++) { + int chainNum = sc.nextInt() - 1; + int direction = sc.nextInt(); + visited = new boolean[T]; + compute(chainNum, direction); + } + + int cnt = 0; + for (int i = 0; i < T; i++) { + if (chain[i][0] == 1) { + cnt++; + } + } + System.out.println(cnt); + } + + private static void compute(int chainNum, int direction) { + visited[chainNum] = true; + rotate(chainNum, direction); + + if (chainNum - 1 >= 0 && !visited[chainNum - 1] && chain[chainNum - 1][2] != chain[chainNum][6 + direction]) { + compute(chainNum - 1, direction * -1); + } + if (chainNum + 1 < T && !visited[chainNum + 1] && chain[chainNum + 1][6] != chain[chainNum][2 + direction]) { + compute(chainNum + 1, direction * -1); + } + } + + private static void rotate(int chainNum, int direction) { + if (direction == 1) { + int tmp = chain[chainNum][7]; + for (int i = 7; i >= 1; i--) { + chain[chainNum][i] = chain[chainNum][i - 1]; + } + chain[chainNum][0] = tmp; + } else { + int tmp = chain[chainNum][0]; + for (int i = 0; i < 7; i++) { + chain[chainNum][i] = chain[chainNum][i + 1]; + } + chain[chainNum][7] = tmp; + } + } + +} diff --git a/src/main/java/tommy/BOJ_16926.java b/src/main/java/tommy/BOJ_16926.java new file mode 100644 index 0000000..a087635 --- /dev/null +++ b/src/main/java/tommy/BOJ_16926.java @@ -0,0 +1,58 @@ +package tommy; + +import java.util.Scanner; + +public class BOJ_16926 { + static int[][] arr; + static int min; // 돌려야 하는 기준 rep 정하기 + static int[] dx = {0, 1, 0, -1}; + static int[] dy = {1, 0, -1, 0}; + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + int M = sc.nextInt(); + int R = sc.nextInt(); + min = Math.min(N, M); + + arr = new int[N][M]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + arr[i][j] = sc.nextInt(); + } + } + + for (int r = 0; r < R; r++) { + rotate(N,M); + } + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + sb.append(arr[i][j]).append(" "); + } + sb.append("\n"); + } + System.out.println(sb); + } + + private static void rotate(int N, int M) { + for (int t = 0; t < min / 2; t++) { + int x = t; + int y = t; + int idx = 0; + int temp = arr[t][t]; + while (idx < 4) { + int nx = x + dx[idx]; + int ny = y + dy[idx]; + if (nx >= t && nx < N - t && ny >= t && ny < M - t) { + arr[x][y] = arr[nx][ny]; + x = nx; + y = ny; + } else { + idx++; + } + } + arr[t + 1][t] = temp; + } + } +} diff --git a/src/main/java/tommy/BOJ_16927.java b/src/main/java/tommy/BOJ_16927.java new file mode 100644 index 0000000..e405172 --- /dev/null +++ b/src/main/java/tommy/BOJ_16927.java @@ -0,0 +1,64 @@ +package tommy; + +import java.util.Scanner; + +public class BOJ_16927 { + static int N, M; + static int[][] arr; + static int[] dx = {0, 1, 0, -1}; + static int[] dy = {1, 0, -1, 0}; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + N = sc.nextInt(); + M = sc.nextInt(); + int R = sc.nextInt(); + int min = Math.min(M, N); + + arr = new int[N][M]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + arr[i][j] = sc.nextInt(); + } + } + + int nN = N; + int nM = M; + for (int i = 0; i < min / 2; i++) { + int nR = R % (2 * nN + 2 * nM - 4); + for (int r = 0; r < nR; r++) { + rotation(i); + } + nN -= 2; + nM -= 2; + } + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + sb.append(arr[i][j]).append(" "); + } + sb.append("\n"); + } + System.out.println(sb); + } + + private static void rotation(int start) { + int x = start; + int y = start; + int index = 0; + int temp = arr[x][y]; + while (index < 4) { + int nx = x + dx[index]; + int ny = y + dy[index]; + if (nx >= start && nx < N - start && ny >= start && ny < M - start) { + arr[x][y] = arr[nx][ny]; + x = nx; + y = ny; + } else { + index++; + } + } + arr[start + 1][start] = temp; + } +} diff --git a/src/main/java/tommy/BOJ_16931.java b/src/main/java/tommy/BOJ_16931.java new file mode 100644 index 0000000..b2b9dd3 --- /dev/null +++ b/src/main/java/tommy/BOJ_16931.java @@ -0,0 +1,74 @@ +package tommy; + +import java.util.Scanner; + +public class BOJ_16931 { + + static int N, M; + static int[][] map; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + N = sc.nextInt(); + M = sc.nextInt(); + + int area = 0; + + map = new int[N][M]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + map[i][j] = sc.nextInt(); + area++; // 위 + } + } + + area *= 2; // 아래 + + // East + for (int i = 0; i < N; i++) { + for (int j = M - 1; j >= 1; j--) { + int space = map[i][j - 1] - map[i][j]; + if (space >= 0) { + area += space; + } + } + area += map[i][M - 1]; + } + + // West + for (int i = 0; i < N; i++) { + for (int j = 1; j < M; j++) { + int space = map[i][j] - map[i][j - 1]; + if (space >= 0) { + area += space; + } + } + area += map[i][0]; + } + + // North + for (int j = 0; j < M; j++) { + for (int i = 0; i < N - 1; i++) { + int space = map[i + 1][j] - map[i][j]; + if (space >= 0) { + area += space; + } + } + area += map[0][j]; + } + + // South + for (int j = 0; j < M; j++) { + for (int i = N - 1; i >= 1; i--) { + int space = map[i - 1][j] - map[i][j]; + if (space >= 0) { + area += space; + } + } + area += map[N - 1][j]; + } + + System.out.println(area); + } +} diff --git a/src/main/java/tommy/BOJ_16935.java b/src/main/java/tommy/BOJ_16935.java new file mode 100644 index 0000000..cce3c80 --- /dev/null +++ b/src/main/java/tommy/BOJ_16935.java @@ -0,0 +1,171 @@ +package tommy; + +import java.util.Scanner; + +public class BOJ_16935 { + static int N; + static int M; + static int[][] arr; + static int[][] transArr; + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + N = sc.nextInt(); + M = sc.nextInt(); + int R = sc.nextInt(); + + arr = new int[N][M]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + arr[i][j] = sc.nextInt(); + } + } + + for (int r = 0; r < R; r++) { + int operator = sc.nextInt(); + switch (operator) { + case 1 : + solution1(); + break; + case 2: + solution2(); + break; + case 3 : + solution3(); + break; + case 4 : + solution4(); + break; + case 5 : + solution5(); + break; + case 6: + solution6(); + break; + } + } + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + sb.append(arr[i][j]).append(" "); + } + sb.append("\n"); + } + System.out.println(sb); + } + + // 상하 반전 + static void solution1() { + transArr = new int[N][M]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + transArr[N - 1 - i][j] = arr[i][j]; + } + } + arr = transArr; + } + // 좌우 반전 + static void solution2() { + transArr = new int[N][M]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + transArr[i][M - 1 - j] = arr[i][j]; + } + } + arr = transArr; + } + // 오른쪽 90도 회전 + static void solution3() { + transArr = new int[M][N]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + transArr[j][N - 1 - i] = arr[i][j]; + } + } + // 크기 변환 + int temp = N; + N = M; + M = temp; + arr = transArr; + } + + static void solution4() { + transArr = new int[M][N]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + transArr[M - 1 - j][i] = arr[i][j]; + } + } + // 크기 변환 + int temp = N; + N = M; + M = temp; + arr = transArr; + } + static void solution5() { + transArr = new int[N][M]; + int N_mean = N / 2; + int M_mean = M / 2; + + // 1 -> 2 + for (int i = 0; i < N_mean; i++) { + for (int j = 0; j < M_mean; j++) { + transArr[i][M_mean + j] = arr[i][j]; + } + } + // 2 -> 3 + for (int i = 0; i < N_mean; i++) { + for (int j = M_mean; j < M; j++) { + transArr[N_mean + i][j] = arr[i][j]; + } + } + // 3 -> 4 + for (int i = N_mean; i < N; i++) { + for (int j = M_mean; j < M; j++) { + transArr[i][j - M_mean] = arr[i][j]; + } + } + // 4 -> 1 + for (int i = N_mean; i < N; i++) { + for (int j = 0; j < M_mean; j++) { + transArr[i - N_mean][j] = arr[i][j]; + } + } + + arr = transArr; + } + + static void solution6() { + transArr = new int[N][M]; + int N_mean = N / 2; + int M_mean = M / 2; + + // 1 -> 4 + for (int i = 0; i < N_mean; i++) { + for (int j = 0; j < M_mean; j++) { + transArr[i + N_mean][j] = arr[i][j]; + } + } + // 4 -> 3 + for (int i = N_mean; i < N; i++) { + for (int j = 0; j < M_mean; j++) { + transArr[i][j + M_mean] = arr[i][j]; + } + } + // 3 -> 2 + for (int i = N_mean; i < N; i++) { + for (int j = M_mean; j < M; j++) { + transArr[i - N_mean][j] = arr[i][j]; + } + } + // 2 -> 1 + for (int i = 0; i < N_mean; i++) { + for (int j = M_mean; j < M; j++) { + transArr[i][j - M_mean] = arr[i][j]; + } + } + + arr = transArr; + } + +} diff --git a/src/main/java/tommy/BOJ_16967.java b/src/main/java/tommy/BOJ_16967.java new file mode 100644 index 0000000..895a8e7 --- /dev/null +++ b/src/main/java/tommy/BOJ_16967.java @@ -0,0 +1,53 @@ +package tommy; + +import java.util.Scanner; + +public class BOJ_16967 { + static int H, W, X, Y; + + static int[][] A; + static int[][] B; + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + H = sc.nextInt(); + W = sc.nextInt(); + X = sc.nextInt(); + Y = sc.nextInt(); + + A = new int[H][W]; + B = new int[H + X][W + Y]; + for (int i = 0; i < H + X; i++) { + for (int j = 0; j < W + Y; j++) { + B[i][j] = sc.nextInt(); + } + } + + for (int i = 0; i < X; i++) { + for (int j = 0; j < W; j++) { + A[i][j] = B[i][j]; + } + } + + for (int i = X; i < H; i++) { + for (int j = 0; j < Y; j++) { + A[i][j] = B[i][j]; + } + } + + for (int i = X; i < H; i++) { + for (int j = Y; j < W; j++) { + A[i][j] = B[i][j] - A[i - X][j - Y]; + } + } + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < H; i++) { + for (int j = 0; j < W; j++) { + sb.append(A[i][j]).append(" "); + } + sb.append("\n"); + } + + System.out.println(sb); + } +} diff --git a/src/main/java/tommy/BOJ_1697.java b/src/main/java/tommy/BOJ_1697.java new file mode 100644 index 0000000..1bbb05d --- /dev/null +++ b/src/main/java/tommy/BOJ_1697.java @@ -0,0 +1,54 @@ +package tommy; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_1697 { + static int N; + static int K; + static int[] visited = new int[100001]; + + /* + * Time Complexity : O(N) + * Space Complexity : O(N) + */ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + N = sc.nextInt(); + K = sc.nextInt(); + + int result = bfs(N); + System.out.println(result); + } + + private static int bfs(int node) { + Queue queue = new LinkedList<>(); + queue.offer(node); + + int index = node; + visited[index] = 1; + while (!queue.isEmpty()) { + int n = queue.poll(); + + if (n == K) { + return visited[n] - 1; + } + if (n - 1 >= 0 && visited[n - 1] == 0) { + queue.offer(n - 1); + visited[n - 1] = visited[n] + 1; + } + + if (n + 1 <= 100000 && visited[n - 1] == 0) { + queue.offer(n + 1); + visited[n + 1] = visited[n] + 1; + } + + if (2 * n <= 100000 && visited[2 * n] == 0) { + queue.offer(2 * n); + visited[2 * n] = visited[n] + 1; + } + } + return -1; + } +} diff --git a/src/main/java/tommy/BOJ_2290.java b/src/main/java/tommy/BOJ_2290.java new file mode 100644 index 0000000..e0788fe --- /dev/null +++ b/src/main/java/tommy/BOJ_2290.java @@ -0,0 +1,172 @@ +package tommy; + +import java.util.Scanner; + +public class BOJ_2290 { + + static int[][] map; + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int s = sc.nextInt(); + + String numbers = sc.next(); + int num = numbers.length(); + map = new int[(2 * s) + 3][(s + 2) * num]; + + // 1 : - , 2 : | + + for (int i = 0; i < num; i++) { + int number = numbers.charAt(i) - '0'; + int x = (s + 2) * i ; // base line + switch (number) { + case 1: + for (int j = 1; j < 1 + s; j++) { + map[j][x + s + 1] = 2; + } + for (int j = s + 2; j < (2 * s) + 2; j++) { + map[j][x + s + 1] = 2; + } + break; + case 2: + for (int j = x + 1; j < (x + s + 1); j++) { + map[0][j] = 1; + map[s + 1][j] = 1; + map[2 * s + 2][j] = 1; + } + for (int j = 1; j <= s; j++) { + map[j][x + s + 1] = 2; + } + for (int j = s + 2; j <= s * 2 + 1; j++) { + map[j][x] = 2; + } + break; + case 3: + for (int j = x + 1; j < (x + s + 1); j++) { + map[0][j] = 1; + map[s + 1][j] = 1; + map[2 * s + 2][j] = 1; + } + for (int j = 1; j <= s; j++) { + map[j][x + s + 1] = 2; + } + for (int j = s + 2; j <= s * 2 + 1; j++) { + map[j][x + s + 1] = 2; + } + break; + case 4: + for (int j = 1; j <= s; j++) { + map[j][x] = 2; + map[j][x + s + 1] = 2; + } + for (int j = s + 2; j <= 2 * s + 1; j++) { + map[j][x + s + 1] = 2; + } + for (int j = x + 1; j <= (x + s); j++) { + map[s + 1][j] = 1; + } + break; + case 5: + for (int j = x + 1; j < (x + s + 1); j++) { + map[0][j] = 1; + map[s + 1][j] = 1; + map[2 * s + 2][j] = 1; + } + for (int j = 1; j <= s; j++) { + map[j][x] = 2; + } + for (int j = s + 2; j <= 2 * s + 1; j++) { + map[j][x + s + 1] = 2; + } + break; + case 6: + for (int j = x + 1; j < (x + s + 1); j++) { + map[0][j] = 1; + map[s + 1][j] = 1; + map[2 * s + 2][j] = 1; + } + for (int j = 1; j <= s; j++) { + map[j][x] = 2; + } + for (int j = s + 2; j <= 2 * s + 1; j++) { + map[j][x] = 2; + map[j][x + s + 1] = 2; + } + break; + case 7: + for (int j = x + 1; j < (x + s + 1); j++) { + map[0][j] = 1; + } + for (int j = 1; j <= s; j++) { + map[j][x + s + 1] = 2; + } + for (int j = s + 2; j <= 2 * s + 1; j++) { + map[j][x + s + 1] = 2; + } + break; + case 8: + for (int j = x + 1; j < (x + s + 1); j++) { + map[0][j] = 1; + map[s + 1][j] = 1; + map[2 * s + 2][j] = 1; + } + for (int j = 1; j <= s; j++) { + map[j][x] = 2; + map[j][x + s + 1] = 2; + } + for (int j = s + 2; j <= 2 * s + 1; j++) { + map[j][x] = 2; + map[j][x + s + 1] = 2; + } + break; + case 9: + for (int j = x + 1; j < (x + s + 1); j++) { + map[0][j] = 1; + map[s + 1][j] = 1; + map[2 * s + 2][j] = 1; + } + for (int j = 1; j <= s; j++) { + map[j][x] = 2; + map[j][x + s + 1] = 2; + } + for (int j = s + 2; j <= 2 * s + 1; j++) { + map[j][x + s + 1] = 2; + } + break; + case 0: + for (int j = x + 1; j < (x + s + 1); j++) { + map[0][j] = 1; + map[2 * s + 2][j] = 1; + } + for (int j = 1; j <= s; j++) { + map[j][x] = 2; + map[j][x + s + 1] = 2; + } + for (int j = s + 2; j <= 2 * s + 1; j++) { + map[j][x] = 2; + map[j][x + s + 1] = 2; + } + break; + } + } + + StringBuilder sb = new StringBuilder(); + int index = 0; + for (int i = 0; i < map.length; i++) { + for (int j = 0; j < map[0].length; j++) { + if (map[i][j] == 2) { + sb.append("|"); + } else if (map[i][j] == 1) { + sb.append("-"); + } else { + sb.append(" "); + } + index++; + if (index % (s + 2) == 0) { + sb.append(" "); + } + } + sb.append("\n"); + } + System.out.println(sb); + } +} diff --git a/src/main/java/tommy/BOJ_7562.java b/src/main/java/tommy/BOJ_7562.java new file mode 100644 index 0000000..e376302 --- /dev/null +++ b/src/main/java/tommy/BOJ_7562.java @@ -0,0 +1,60 @@ +package tommy; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_7562 { + static int[] dx = {1, 2, 2, 1, -1, -2, -2, -1}; + static int[] dy = {2, 1, -1, -2, -2, -1, 1, 2}; + static int I; + static int curX, curY, objX, objY; + static int[][] arr; + static boolean[][] visited; + static StringBuilder sb = new StringBuilder(); + + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + for (int i = 0; i < N; i++) { + I = sc.nextInt(); + arr = new int[I][I]; + visited = new boolean[I][I]; + + curX = sc.nextInt(); + curY = sc.nextInt(); + objX = sc.nextInt(); + objY = sc.nextInt(); + + bfs(); + sb.append(arr[objX][objY]).append("\n"); + } + System.out.println(sb); + } + + private static void bfs() { + Queue queue = new LinkedList<>(); + queue.offer(new int[]{curX, curY}); + visited[curX][curY] = true; + + while (!queue.isEmpty()) { + int[] poll = queue.poll(); + int nx = poll[0]; + int ny = poll[1]; + + for (int i = 0; i < 8; i++) { + int newX = nx + dx[i]; + int newY = ny + dy[i]; + + if (newX >= 0 && newX < I && newY >= 0 && newY < I) { + if (!visited[newX][newY]) { + queue.offer(new int[]{newX, newY}); + arr[newX][newY] = arr[nx][ny] + 1; + visited[newX][newY] = true; + } + } + } + } + } +} diff --git a/src/main/java/tommy/PRO_HIndex.java b/src/main/java/tommy/PRO_HIndex.java new file mode 100644 index 0000000..4c1b666 --- /dev/null +++ b/src/main/java/tommy/PRO_HIndex.java @@ -0,0 +1,15 @@ +package tommy; + +import java.util.Arrays; + +public class PRO_HIndex { + public int solution(int[] citations) { + Arrays.sort(citations); // 오름 차순 정렬 + int answer = 0; + for(int i = 0; i < citations.length; i++){ + int small = Math.min(citations.length - i, citations[i]); + answer = Math.max(small, answer); + } + return answer; + } +} diff --git "a/src/main/java/tommy/PRO_K\353\262\210\354\247\270\354\210\230.java" "b/src/main/java/tommy/PRO_K\353\262\210\354\247\270\354\210\230.java" new file mode 100644 index 0000000..6f71f26 --- /dev/null +++ "b/src/main/java/tommy/PRO_K\353\262\210\354\247\270\354\210\230.java" @@ -0,0 +1,23 @@ +package tommy; + +import java.util.Arrays; + +public class PRO_K번째수 { + /* + * Time Complexity : O(N^2 logN) + * Space Complexity : O(N) + */ + public int[] solution(int[] array, int[][] commands) { + int[] answer = new int[commands.length]; + for(int i = 0; i < commands.length; i++){ + int[] ints = Arrays.stream(array) + .skip(commands[i][0] - 1) + .limit(commands[i][1] - commands[i][0] + 1) + .sorted() + .toArray(); + answer[i] = ints[commands[i][2] - 1]; + } + + return answer; + } +} diff --git "a/src/main/java/tommy/PRO_\352\260\200\354\236\245\355\201\260\354\210\230.java" "b/src/main/java/tommy/PRO_\352\260\200\354\236\245\355\201\260\354\210\230.java" new file mode 100644 index 0000000..bf0c9eb --- /dev/null +++ "b/src/main/java/tommy/PRO_\352\260\200\354\236\245\355\201\260\354\210\230.java" @@ -0,0 +1,25 @@ +package tommy; + +import java.util.Arrays; + +public class PRO_가장큰수 { + + /* + * Time Complexity : O(N) + * Space Complexity : O(N) + */ + public String solution(int[] numbers) { + String[] strings = new String[numbers.length]; + + for(int i = 0; i < numbers.length; i++){ + strings[i] = numbers[i] + ""; + } + + Arrays.sort(strings, (s1, s2) -> (s2 + s1).compareTo((s1 + s2))); + StringBuilder sb = new StringBuilder(); + for(String s : strings){ + sb.append(s); + } + return sb.toString().charAt(0) == '0' ? "0" : sb.toString(); + } +} diff --git "a/src/main/java/tommy/PRO_\352\260\231\354\235\200\354\210\253\354\236\220\353\212\224\354\213\253\354\226\264.java" "b/src/main/java/tommy/PRO_\352\260\231\354\235\200\354\210\253\354\236\220\353\212\224\354\213\253\354\226\264.java" new file mode 100644 index 0000000..9dd480d --- /dev/null +++ "b/src/main/java/tommy/PRO_\352\260\231\354\235\200\354\210\253\354\236\220\353\212\224\354\213\253\354\226\264.java" @@ -0,0 +1,40 @@ +package tommy; + +import java.util.*; + +public class PRO_같은숫자는싫어 { + public int[] solution(String[] genres, int[] plays) { + List answer = new ArrayList<>(); + + Map gMap = new HashMap<>(); + Map> pMap = new HashMap<>(); // index, play + + for(int i = 0; i < genres.length; i++){ + gMap.put(genres[i], gMap.getOrDefault(genres[i], 0) + plays[i]); + if(pMap.containsKey(genres[i])){ + pMap.get(genres[i]).put(i, plays[i]); + } else{ + Map temp = new HashMap<>(); + temp.put(i, plays[i]); + pMap.put(genres[i], temp); + } + } + + List keySet = new ArrayList<>(gMap.keySet()); + Collections.sort(keySet, (a, b) -> gMap.get(b) - gMap.get(a)); + + for (String key : keySet){ + Map map = pMap.get(key); + List genre_key = new ArrayList<>(map.keySet()); + + Collections.sort(genre_key, (a, b) -> map.get(b) - map.get(a)); + + answer.add(genre_key.get(0)); + if (genre_key.size() > 1){ + answer.add(genre_key.get(1)); + } + } + + return answer.stream().mapToInt(i -> i).toArray(); + } +} diff --git "a/src/main/java/tommy/PRO_\352\265\254\353\252\205\353\263\264\355\212\270.java" "b/src/main/java/tommy/PRO_\352\265\254\353\252\205\353\263\264\355\212\270.java" new file mode 100644 index 0000000..02febeb --- /dev/null +++ "b/src/main/java/tommy/PRO_\352\265\254\353\252\205\353\263\264\355\212\270.java" @@ -0,0 +1,21 @@ +package tommy; + +import java.util.Arrays; + +public class PRO_구명보트 { + public int solution(int[] people, int limit) { + Arrays.sort(people); + int answer = 0; + int pointer = 0; + + for(int i = people.length - 1; i >= pointer; i--){ + if (people[i] + people[pointer] <= limit){ + answer++; + pointer++; + } else{ + answer++; + } + } + return answer; + } +} diff --git "a/src/main/java/tommy/PRO_\352\270\260\353\212\245\352\260\234\353\260\234.java" "b/src/main/java/tommy/PRO_\352\270\260\353\212\245\352\260\234\353\260\234.java" new file mode 100644 index 0000000..8649750 --- /dev/null +++ "b/src/main/java/tommy/PRO_\352\270\260\353\212\245\352\260\234\353\260\234.java" @@ -0,0 +1,34 @@ +package tommy; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class PRO_기능개발 { + + /* + * Time Complexity : O(N) + * Space Complexity : O(N) + */ + public int[] solution(int[] progresses, int[] speeds) { + Queue queue = new LinkedList<>(); + for(int i = 0; i < progresses.length; i++){ + int due = (int) Math.ceil((100.0 - progresses[i]) / speeds[i]); + queue.add(due); + } + + List list = new ArrayList<>(); + while(!queue.isEmpty()){ + int current = queue.poll(); + int days = 1; + while(!queue.isEmpty() && queue.peek() <= current){ + queue.poll(); + days++; + } + list.add(days); + } + + return list.stream().mapToInt(i -> i).toArray(); + } +} diff --git "a/src/main/java/tommy/PRO_\353\204\244\355\212\270\354\233\214\355\201\254.java" "b/src/main/java/tommy/PRO_\353\204\244\355\212\270\354\233\214\355\201\254.java" new file mode 100644 index 0000000..97f2966 --- /dev/null +++ "b/src/main/java/tommy/PRO_\353\204\244\355\212\270\354\233\214\355\201\254.java" @@ -0,0 +1,27 @@ +package tommy; + +public class PRO_네트워크 { + static boolean[] visited; + + public int solution(int n, int[][] computers) { + visited = new boolean[n]; + int answer = 0; + + for(int i = 0; i < n; i++){ + if(!visited[i]){ + answer++; + recursion(computers, i); + } + } + return answer; + } + + void recursion(int[][] computers, int startIndex){ + visited[startIndex] = true; + for(int i = 0; i < computers.length; i++){ + if(computers[startIndex][i] == 1 && !visited[i]){ + recursion(computers, i); + } + } + } +} diff --git "a/src/main/java/tommy/PRO_\353\213\244\353\246\254\353\245\274\354\247\200\353\202\230\353\212\224\355\212\270\353\237\255.java" "b/src/main/java/tommy/PRO_\353\213\244\353\246\254\353\245\274\354\247\200\353\202\230\353\212\224\355\212\270\353\237\255.java" new file mode 100644 index 0000000..1cc8e08 --- /dev/null +++ "b/src/main/java/tommy/PRO_\353\213\244\353\246\254\353\245\274\354\247\200\353\202\230\353\212\224\355\212\270\353\237\255.java" @@ -0,0 +1,40 @@ +package tommy; + +import java.util.LinkedList; +import java.util.Queue; + +public class PRO_다리를지나는트럭 { + + public int solution(int bridge_length, int weight, int[] truck_weights) { + Queue queue = new LinkedList<>(); + int time = 0; + int sum = 0; + + for(int i = 0; i < truck_weights.length; i++){ + int current = truck_weights[i]; + + while(true){ + if(queue.isEmpty()){ + queue.add(current); + sum += current; + time++; + break; + } else if (queue.size() == bridge_length){ + sum -= queue.poll(); + } else { + if(sum + current <= weight){ + queue.add(current); + sum += current; + time++; + break; + } else{ + queue.add(0); + time++; + } + } + } + } + + return bridge_length + time; + } +} diff --git "a/src/main/java/tommy/PRO_\353\215\224\353\247\265\352\262\214.java" "b/src/main/java/tommy/PRO_\353\215\224\353\247\265\352\262\214.java" new file mode 100644 index 0000000..2687153 --- /dev/null +++ "b/src/main/java/tommy/PRO_\353\215\224\353\247\265\352\262\214.java" @@ -0,0 +1,25 @@ +package tommy; + +import java.util.PriorityQueue; + +public class PRO_더맵게 { + public int solution(int[] scoville, int K) { + PriorityQueue queue = new PriorityQueue<>(); + for(int s : scoville){ + queue.add(s); + } + + int answer = 0; + int min = queue.peek(); + while(min < K){ + if(queue.size() >= 2){ + queue.add(queue.poll() + (queue.poll() * 2)); + min = queue.peek(); + answer++; + } else { + return -1; + } + } + return answer; + } +} diff --git "a/src/main/java/tommy/PRO_\353\224\224\354\212\244\355\201\254\354\273\250\355\212\270\353\241\244\353\237\254.java" "b/src/main/java/tommy/PRO_\353\224\224\354\212\244\355\201\254\354\273\250\355\212\270\353\241\244\353\237\254.java" new file mode 100644 index 0000000..4b137a8 --- /dev/null +++ "b/src/main/java/tommy/PRO_\353\224\224\354\212\244\355\201\254\354\273\250\355\212\270\353\241\244\353\237\254.java" @@ -0,0 +1,55 @@ +package tommy; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.PriorityQueue; + +public class PRO_디스크컨트롤러 { + public int solution(int[][] jobs) { + + Arrays.sort(jobs, (j1, j2) -> j1[0] - j2[0]); + + // duration 적은 순서대로 정렬 + PriorityQueue queue = new PriorityQueue<>(Comparator.comparingInt(JOB::getDuration)); + + int time = 0; + int sum = 0; + int idx = 0; + int count = 0; + + while(count < jobs.length){ + while(idx < jobs.length && jobs[idx][0] <= time){ + queue.add(new JOB(jobs[idx][0], jobs[idx][1])); + idx++; + } + + if (queue.isEmpty()){ + time = jobs[idx][0]; + } else{ + JOB job = queue.poll(); + sum += time + job.getDuration() - job.getStart(); + time += job.getDuration(); + count++; + } + } + return sum / jobs.length; + } + + static class JOB{ + private int start; + private int duration; + + public JOB(int start, int duration){ + this.start = start; + this.duration = duration; + } + + public int getStart(){ + return start; + } + + public int getDuration(){ + return duration; + } + } +} diff --git "a/src/main/java/tommy/PRO_\353\252\250\354\235\214\354\202\254\354\240\204.java" "b/src/main/java/tommy/PRO_\353\252\250\354\235\214\354\202\254\354\240\204.java" new file mode 100644 index 0000000..1df7177 --- /dev/null +++ "b/src/main/java/tommy/PRO_\353\252\250\354\235\214\354\202\254\354\240\204.java" @@ -0,0 +1,32 @@ +package tommy; + +import java.util.List; +import java.util.ArrayList; + +public class PRO_모음사전 { + static String[] words = new String[]{"A", "E", "I", "O", "U"}; + static List list = new ArrayList<>(); + + public int solution(String word) { + recursion(0, ""); + + for(int i = 0; i < list.size(); i++){ + if (list.get(i).equals(word)){ + return i; + } + } + + return 0; + } + + void recursion(int depth, String word){ + list.add(word); + if(depth == 5){ + return; + } + + for(int i = 0; i < 5; i++){ + recursion(depth + 1, word + words[i]); + } + } +} diff --git "a/src/main/java/tommy/PRO_\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.java" "b/src/main/java/tommy/PRO_\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.java" new file mode 100644 index 0000000..ab8270f --- /dev/null +++ "b/src/main/java/tommy/PRO_\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.java" @@ -0,0 +1,40 @@ +package tommy; + +import java.util.*; + +public class PRO_베스트앨범 { + public int[] solution(String[] genres, int[] plays) { + List answer = new ArrayList<>(); + + Map gMap = new HashMap<>(); + Map> pMap = new HashMap<>(); // index, play + + for(int i = 0; i < genres.length; i++){ + gMap.put(genres[i], gMap.getOrDefault(genres[i], 0) + plays[i]); + if(pMap.containsKey(genres[i])){ + pMap.get(genres[i]).put(i, plays[i]); + } else{ + Map temp = new HashMap<>(); + temp.put(i, plays[i]); + pMap.put(genres[i], temp); + } + } + + List keySet = new ArrayList<>(gMap.keySet()); + Collections.sort(keySet, (a, b) -> gMap.get(b) - gMap.get(a)); + + for (String key : keySet){ + Map map = pMap.get(key); + List genre_key = new ArrayList<>(map.keySet()); + + Collections.sort(genre_key, (a, b) -> map.get(b) - map.get(a)); + + answer.add(genre_key.get(0)); + if (genre_key.size() > 1){ + answer.add(genre_key.get(1)); + } + } + + return answer.stream().mapToInt(i -> i).toArray(); + } +} diff --git "a/src/main/java/tommy/PRO_\354\206\214\354\210\230\354\260\276\352\270\260.java" "b/src/main/java/tommy/PRO_\354\206\214\354\210\230\354\260\276\352\270\260.java" new file mode 100644 index 0000000..33e9125 --- /dev/null +++ "b/src/main/java/tommy/PRO_\354\206\214\354\210\230\354\260\276\352\270\260.java" @@ -0,0 +1,57 @@ +package tommy; + +import java.util.HashSet; +import java.util.Set; + +public class PRO_소수찾기 { + static boolean[] visited = new boolean[7]; + static Set set = new HashSet<>(); + static String number = ""; + + /* + * Time Complexity : O(N ^ 2) + * Space Complexity : O(N) + */ + public int solution(String numbers) { + number = numbers; + // Initialization + dfs("", 0); + + int answer = 0; + for(int i : set){ + if(isPrime(i)){ + answer++; + } + } + + return answer; + } + + void dfs(String s, int depth){ + if(depth == number.length()){ + return; + } + + for(int i = 0; i < number.length(); i++){ + if (!visited[i]){ + visited[i] = true; + set.add(Integer.valueOf(s + number.charAt(i))); + dfs(s + number.charAt(i), depth + 1); + visited[i] = false; + } + } + } + + boolean isPrime(int n){ + if (n < 2){ + return false; + } + for(int i = 2; i <= n / 2; i++){ + if(n % i == 0){ + return false; + } + } + + return true; + } +} diff --git "a/src/main/java/tommy/PRO_\354\230\254\353\260\224\353\245\270\352\264\204\355\230\270.java" "b/src/main/java/tommy/PRO_\354\230\254\353\260\224\353\245\270\352\264\204\355\230\270.java" new file mode 100644 index 0000000..a80b736 --- /dev/null +++ "b/src/main/java/tommy/PRO_\354\230\254\353\260\224\353\245\270\352\264\204\355\230\270.java" @@ -0,0 +1,31 @@ +package tommy; + +import java.util.Stack; + +public class PRO_올바른괄호 { + + /* + * Time Complexity : O(N) + * Space Complexity : O(N) + */ + boolean solution(String s) { + Stack stack = new Stack<>(); + for(char c : s.toCharArray()){ + if (c == '('){ + stack.push(c); + } + else { + if (stack.isEmpty()){ + return false; + } + stack.pop(); + } + } + + if (!stack.isEmpty()){ + return false; + } + + return true; + } +} diff --git "a/src/main/java/tommy/PRO_\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.java" "b/src/main/java/tommy/PRO_\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.java" new file mode 100644 index 0000000..7e0e251 --- /dev/null +++ "b/src/main/java/tommy/PRO_\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.java" @@ -0,0 +1,27 @@ +package tommy; + +import java.util.HashMap; +import java.util.Map; + +public class PRO_완주하지못한선수 { + public String solution(String[] participant, String[] completion) { + Map map = new HashMap<>(); + for(String p : participant){ + map.put(p, map.getOrDefault(p,0) + 1); + } + + for(String c : completion){ + Integer res = map.get(c); + if(res == 1){ + map.remove(c); + } else { + map.put(c, map.get(c) - 1); + } + } + String answer = ""; + for(String key : map.keySet()){ + answer = key; + } + return answer; + } +} diff --git "a/src/main/java/tommy/PRO_\354\235\230\354\203\201.java" "b/src/main/java/tommy/PRO_\354\235\230\354\203\201.java" new file mode 100644 index 0000000..6cfd681 --- /dev/null +++ "b/src/main/java/tommy/PRO_\354\235\230\354\203\201.java" @@ -0,0 +1,30 @@ +package tommy; + +import java.util.HashMap; +import java.util.Map; + +public class PRO_의상 { + /* + * Time Complexity : O(N) + * Space Complexity : O(N) + */ + class Solution { + public int solution(String[][] clothes) { + int answer = 1; + Map map = new HashMap<>(); + for(String[] clothe : clothes){ + String category = clothe[1]; + map.put(category, map.getOrDefault(category, 0) + 1); + } + + for(int i : map.values()){ + answer *= (i + 1); + } + return answer - 1; + // Stream 을 활용한 방식 +// return map.values().stream() +// .map(i -> i + 1) +// .reduce(1, (a, b) -> a * b) - 1; + } + } +} diff --git "a/src/main/java/tommy/PRO_\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.java" "b/src/main/java/tommy/PRO_\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.java" new file mode 100644 index 0000000..664ffba --- /dev/null +++ "b/src/main/java/tommy/PRO_\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.java" @@ -0,0 +1,40 @@ +package tommy; + +import java.util.PriorityQueue; + +public class PRO_이중우선순위큐 { + /* + * 시간 복잡도 : O(N) + * 공간 복잡도 : O(2N) => queue 두개. + */ + public int[] solution(String[] operations) { + PriorityQueue queue1 = new PriorityQueue<>(); // 오름차순 + PriorityQueue queue2 = new PriorityQueue<>((n1, n2) -> n2 - n1); // 내림차순 + + for(String operation : operations){ + String operator = operation.split(" ")[0]; + String number = operation.split(" ")[1]; + if (operator.equals("I")){ + queue1.add(Integer.valueOf(number)); + queue2.add(Integer.valueOf(number)); + } else { + if(!queue1.isEmpty()){ + if (number.equals("1")){ // 최댓 값 삭제 + int max = queue2.peek(); + queue1.remove(max); + queue2.remove(max); + } else{ // 최소 값 삭제 + int min = queue1.peek(); + queue1.remove(min); + queue2.remove(min); + } + } + } + } + if (queue1.isEmpty()){ + return new int[]{0,0}; + } else{ + return new int[]{queue2.peek(), queue1.peek()}; + } + } +} diff --git "a/src/main/java/tommy/PRO_\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.java" "b/src/main/java/tommy/PRO_\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.java" new file mode 100644 index 0000000..d1dca9b --- /dev/null +++ "b/src/main/java/tommy/PRO_\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.java" @@ -0,0 +1,29 @@ +package tommy; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class PRO_전화번호목록 { + + /* + * Space Complexity : O(N) ==> map에 phone_book의 크기만큼 저장 될 수 있기 때문에 + * Time Complexity : O(N log N) ==> Arrays.sort() // for문이 중첩되긴 하지만 전화번호의 최대 길이는 20자이므로 N log N 이 우세 + */ + public boolean solution(String[] phone_book) { + boolean answer = true; + Map map = new HashMap<>(); + Arrays.sort(phone_book); + for(String phone : phone_book){ + // 검증 + for(int i = 0; i <= phone.length(); i++){ + String partition = phone.substring(0, i); + if(map.containsKey(partition)){ + answer = false; + } + } + map.put(phone, true); + } + return answer; + } +} diff --git "a/src/main/java/tommy/PRO_\354\241\260\354\235\264\354\212\244\355\213\261.java" "b/src/main/java/tommy/PRO_\354\241\260\354\235\264\354\212\244\355\213\261.java" new file mode 100644 index 0000000..35bab73 --- /dev/null +++ "b/src/main/java/tommy/PRO_\354\241\260\354\235\264\354\212\244\355\213\261.java" @@ -0,0 +1,21 @@ +package tommy; + +public class PRO_조이스틱 { + public int solution(String name) { + int answer = 0; + int move = name.length() - 1; // 단순하게 오른쪽으로만 움직일 경우의 move count + + for(int i = 0; i < name.length(); i++){ + answer += Math.min(name.charAt(i) - 'A', 26 - (name.charAt(i) - 'A')); // 조이스틱 위아래 move + if(i < name.length() - 1 && name.charAt(i + 1) == 'A'){ // 다음 글자가 A 라면 + int pointer = i + 1; + while(pointer < name.length() && name.charAt(pointer) == 'A'){ + pointer++; + } + move = Math.min(move, i * 2 + (name.length() - pointer)); // 오른쪽으로 갔다가 왼쪽으로 돌아가는 경우 + move = Math.min(move, i + 2 * (name.length() - pointer)); // 왼쪽으로 갔다가 오른쪽으로 돌아가는 경우 + } + } + return answer + move; + } +} diff --git "a/src/main/java/tommy/PRO_\354\243\274\354\213\235\352\260\200\352\262\251.java" "b/src/main/java/tommy/PRO_\354\243\274\354\213\235\352\260\200\352\262\251.java" new file mode 100644 index 0000000..726b788 --- /dev/null +++ "b/src/main/java/tommy/PRO_\354\243\274\354\213\235\352\260\200\352\262\251.java" @@ -0,0 +1,33 @@ +package tommy; + +import java.util.ArrayList; +import java.util.List; + +public class PRO_주식가격 { + public int[] solution(int[] prices) { + List answer = new ArrayList<>(); + + for(int i = 0; i < prices.length; i++){ + int current = prices[i]; + + int pointer = i + 1; + while(true){ + if(pointer == prices.length){ + answer.add(pointer - i - 1); + break; + } + + int temp = prices[pointer]; + + if(temp < current){ + answer.add(pointer - i); + break; + } else{ + pointer++; + } + } + } + + return answer.stream().mapToInt(i -> i).toArray(); + } +} diff --git "a/src/main/java/tommy/PRO_\354\271\264\355\216\253.java" "b/src/main/java/tommy/PRO_\354\271\264\355\216\253.java" new file mode 100644 index 0000000..a4d30b4 --- /dev/null +++ "b/src/main/java/tommy/PRO_\354\271\264\355\216\253.java" @@ -0,0 +1,20 @@ +package tommy; + +public class PRO_카펫 { + public int[] solution(int brown, int yellow) { + int sum = brown + yellow; + + for(int i = 3; i < sum; i++){ + int j = sum / i; + if(sum % i == 0 && j >= 3){ + int col = Math.min(i, j); + int row = Math.max(i, j); + int area = (col - 2) * (row - 2); + if(area == yellow){ + return new int[]{row, col}; + } + } + } + return new int[]{}; + } +} diff --git "a/src/main/java/tommy/PRO_\355\201\260\354\210\230\353\247\214\353\223\244\352\270\260.java" "b/src/main/java/tommy/PRO_\355\201\260\354\210\230\353\247\214\353\223\244\352\270\260.java" new file mode 100644 index 0000000..176490c --- /dev/null +++ "b/src/main/java/tommy/PRO_\355\201\260\354\210\230\353\247\214\353\223\244\352\270\260.java" @@ -0,0 +1,24 @@ +package tommy; + +import java.util.List; + +public class PRO_큰수만들기 { + public String solution(String number, int k) { + StringBuilder sb = new StringBuilder(); + int index = 0; + int max; + for(int i = 0; i < number.length() - k; i++){ + max = 0; + for(int j = index; j <= k + i; j++){ + if(max < number.charAt(j) - '0'){ + max = number.charAt(j) - '0'; + index = j + 1; + } + } + sb.append(max); + } + + return sb.toString(); + } + +} diff --git "a/src/main/java/tommy/PRO_\355\217\260\354\274\223\353\252\254.java" "b/src/main/java/tommy/PRO_\355\217\260\354\274\223\353\252\254.java" new file mode 100644 index 0000000..7a45d4f --- /dev/null +++ "b/src/main/java/tommy/PRO_\355\217\260\354\274\223\353\252\254.java" @@ -0,0 +1,20 @@ +package tommy; + +import java.util.HashSet; +import java.util.Set; + +public class PRO_폰켓몬 { + /* + 최대 반환 수 : N / 2 + 같은 숫자인 경우 종류는 1개로 취급 + */ + public int solution(int[] nums) { + int answer = 0; + Set set = new HashSet<>(); + for(int num : nums){ + set.add(num); + } + + return set.size() > nums.length / 2 ? nums.length / 2 : set.size(); + } +}