diff --git "a/BOJ/20001-25000\353\262\210/HW_20181.java" "b/BOJ/20001-25000\353\262\210/HW_20181.java" new file mode 100644 index 00000000..149a8340 --- /dev/null +++ "b/BOJ/20001-25000\353\262\210/HW_20181.java" @@ -0,0 +1,36 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class HW_20181 { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); // 먹이 개수 + int K = Integer.parseInt(st.nextToken()); // 최소 만족도 + int[] arr = new int[N]; + long[] dp = new long[N+1]; // dp[i] : i번쨰 먹이까지의 최대 탈피 에너지 + + st = new StringTokenizer(br.readLine()); + for(int i=0; i words = new TreeMap<>(); + + for(int i=0; i entry : words.entrySet()) { + System.out.println(entry.getKey() + " " + entry.getValue()); + } + + } +} \ No newline at end of file diff --git "a/BOJ/20001-25000\353\262\210/HW_22944.java" "b/BOJ/20001-25000\353\262\210/HW_22944.java" new file mode 100644 index 00000000..24a980e8 --- /dev/null +++ "b/BOJ/20001-25000\353\262\210/HW_22944.java" @@ -0,0 +1,99 @@ +import java.io.*; +import java.util.*; + +public class HW_22944 { + static int N, H, D; + static char[][] board; + static int[][] check; + static int[] dx = {-1, 1, 0, 0}; + static int[] dy = {0, 0, -1, 1}; + + static class Node { + int x, y, h, d, m; + + public Node(int x, int y, int h, int d, int m) { + this.x = x; + this.y = y; + this.h = h; // 현재 체력 + this.d = d; // 우산 내구도 + this.m = m; // 이동 횟수 + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + H = Integer.parseInt(st.nextToken()); + D = Integer.parseInt(st.nextToken()); + + board = new char[N][N]; + check = new int[N][N]; + + int startX = 0, startY = 0; + for (int i = 0; i < N; i++) { + String line = br.readLine(); + for (int j = 0; j < N; j++) { + board[i][j] = line.charAt(j); + if (board[i][j] == 'S') { + startX = i; + startY = j; + } + } + } + + for (int i = 0; i < N; i++) { + Arrays.fill(check[i], -1); + } + + int result = bfs(startX, startY); + System.out.println(result); + } + + public static int bfs(int sx, int sy) { + Queue queue = new LinkedList<>(); + queue.add(new Node(sx, sy, H, 0, 0)); // 시작점 추가 + check[sx][sy] = H; // 시작점 방문 처리 + + while (!queue.isEmpty()) { + Node cur = queue.poll(); + + for (int i = 0; i < 4; i++) { + int newH = cur.h, newD = cur.d, newM = cur.m; + int nx = cur.x + dx[i]; + int ny = cur.y + dy[i]; + + + if (!isValid(nx, ny)) + continue; + + if (board[nx][ny] == 'E') { // 안전지대에 도달하면 이동 횟수 반환 + return newM + 1; + } + + if (board[nx][ny] == 'U') + newD = D; + + if (newD > 0) { + newD--; + } else { + newH--; + } + + if (newH == 0) + continue; + + if (check[nx][ny] < newH + newD) { + check[nx][ny] = newH + newD; + queue.add(new Node(nx, ny, newH, newD, newM + 1)); + } + } + } + return -1; // 안전지대에 도달하지 못한 경우 + } + + public static boolean isValid(int nx, int ny) { + return 0<= nx && nx < N && 0 <=ny && ny < N; + } +} diff --git "a/CodeTree/2021-2022\353\205\204/HW_\354\203\211\352\271\224_\355\217\255\355\203\204.java" "b/CodeTree/2021-2022\353\205\204/HW_\354\203\211\352\271\224_\355\217\255\355\203\204.java" new file mode 100644 index 00000000..18bda886 --- /dev/null +++ "b/CodeTree/2021-2022\353\205\204/HW_\354\203\211\352\271\224_\355\217\255\355\203\204.java" @@ -0,0 +1,179 @@ +import java.io.*; +import java.util.*; + +public class HW_색깔_폭탄 { + static int n, m; + static int[][] board; + static int score = 0; + static int[] dx = {-1,1,0,0}; + static int[] dy = {0,0,-1,1}; + + static class Bomb { + int size; + int redCount; + int stdX; + int stdY; + List blocks; // 폭탄 묶음 내 좌표 + + Bomb(int size, int redCount, int stdX, int stdY, List blocks) { + this.size = size; + this.redCount = redCount; + this.stdX = stdX; + this.stdY = stdY; + this.blocks = blocks; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + n = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + board = new int[n][n]; + + for (int i=0; i candidates = new ArrayList<>(); + + for (int i=0; i { + if (a.size != b.size) // 1. size 내림차순 + return b.size - a.size; + if (a.redCount != b.redCount) // 2. redCount 오름차순 + return a.redCount - b.redCount; + if (a.stdX != b.stdX) // 3. 기준 행 내림차순 + return b.stdX - a.stdX; + return a.stdY - b.stdY; // 4. 기준 열 오름차순 + }); + + return candidates.get(0); + } + + static Bomb bfs(int sx, int sy, int color) { + boolean[][] visited = new boolean[n][n]; + Queue q = new LinkedList<>(); + q.offer(new int[]{sx, sy}); + visited[sx][sy] = true; + + List group = new ArrayList<>(); + group.add(new int[]{sx, sy}); + int redCount = (board[sx][sy] == 0) ? 1 : 0; + + while(!q.isEmpty()){ + int[] cur = q.poll(); + int x=cur[0], y=cur[1]; + + for (int k=0; k<4; k++){ + int nx = x + dx[k]; + int ny = y + dy[k]; + if (!isValid(nx, ny)) { + continue; + } + if (board[nx][ny] == -1) { // 검은 돌 제외 + continue; + } + if (!visited[nx][ny]) { + if (board[nx][ny] == color || board[nx][ny] == 0) { + visited[nx][ny] = true; + q.offer(new int[]{nx,ny}); + group.add(new int[]{nx,ny}); + if (board[nx][ny] == 0) { + redCount++; + } + } + } + } + } + + if (group.size()<2) return null; + + + int targetX=-1; + int targetY=-1; + for (int i=0; i targetX || (gx==targetX && (targetY==-1 || gy < targetY))) { + targetX = gx; + targetY = gy; + } + } + } + return new Bomb(group.size(), redCount, targetX, targetY, group); + } + + static void remove(Bomb b) { + for (int i=0; i=0; x--){ + if (board[x][y] > -1) { + int nx = x; + while(true) { + int down = nx+1; + if (down>=n) break; + if (board[down][y]!=-2) break; + board[down][y]=board[nx][y]; + board[nx][y]=-2; + nx=down; + } + } + } + } + } + static int[][] rotate(int[][] arr) { + int[][] newBoard = new int[n][n]; + for (int x=0; x timePos; // 시간, 위치별 충돌 횟수 관리 + Map m; // 포인트 번호별 좌표 관리 (key 포인트 번호, value [r,c]) + int answer= 0; + + public int solution(int[][] points, int[][] routes) { + m = new HashMap<>(); + timePos = new HashMap<>(); + + for(int i = 0; i targetX) + startX--; + else if(startY < targetY) + startY++; + else if(startY > targetY) + startY--; + + time++; + last[0] = startX; + last[1] = startY; + } + } + String key = time+"-"+last[0]+"-"+last[1]; + timePos.put(key, timePos.getOrDefault(key, 0) + 1); + + if(timePos.get(key) == 2) { // 충돌 횟수++ + answer++; + } + } +} \ No newline at end of file diff --git a/Programmers/Level3/HW_118669.java b/Programmers/Level3/HW_118669.java new file mode 100644 index 00000000..ea410236 --- /dev/null +++ b/Programmers/Level3/HW_118669.java @@ -0,0 +1,79 @@ +import java.util.*; + +class HW_118669 { + public int[] solution(int n, int[][] paths, int[] gates, int[] summits) { + List[] graph = new ArrayList[n + 1]; + for (int i = 0; i <= n; i++) { + graph[i] = new ArrayList<>(); + } + for (int i = 0; i < paths.length; i++) { + int a = paths[i][0]; + int b = paths[i][1]; + int w = paths[i][2]; + graph[a].add(new int[]{b, w}); // 양방향 + graph[b].add(new int[]{a, w}); + } + + Set gateSet = new HashSet<>(); // 출입구 + Set summitSet = new HashSet<>(); // 산봉우리 + for (int i = 0; i < gates.length; i++) { + gateSet.add(gates[i]); + } + for (int i = 0; i < summits.length; i++) { + summitSet.add(summits[i]); + } + + int[] intensity = new int[n + 1]; + for (int i = 0; i <= n; i++) { + intensity[i] = Integer.MAX_VALUE; + } + + PriorityQueue pq = new PriorityQueue<>((a, b) -> a[1] - b[1]); + + for (int i = 0; i < gates.length; i++) { // 모든 출입구를 시작점으로 초기화 + pq.offer(new int[]{gates[i], 0}); + intensity[gates[i]] = 0; + } + + while (!pq.isEmpty()) { // 다익스트라 + int[] cur = pq.poll(); + int now = cur[0]; + int curIntensity = cur[1]; + + if (intensity[now] < curIntensity) { // 현재 intensity가 이미 최솟값보다 크면 무시 + continue; + } + + for (int i = 0; i < graph[now].size(); i++) { // 현재 지점에서 이동 가능한 모든 경로 탐색 + int[] next = graph[now].get(i); + int nextNode = next[0]; + int weight = next[1]; + + if (gateSet.contains(nextNode)) { // 산봉우리는 거쳐갈 수 없음 + continue; + } + + int newIntensity = Math.max(curIntensity, weight); // 현재 경로에서의 최대 intensity 갱신 + + if (newIntensity < intensity[nextNode]) { // 더 작은 intensity로 업데이트 + intensity[nextNode] = newIntensity; + pq.offer(new int[]{nextNode, newIntensity}); + } + } + } + + int minIntensity = Integer.MAX_VALUE; + int bestSummit = -1; + + Arrays.sort(summits); // 산봉우리 번호가 낮은 것을 우선 처리하기 위함 + for (int i = 0; i < summits.length; i++) { + int summit = summits[i]; + if (intensity[summit] < minIntensity) { + minIntensity = intensity[summit]; + bestSummit = summit; + } + } + + return new int[]{bestSummit, minIntensity}; + } +} \ No newline at end of file diff --git "a/SQL/14\354\243\274\354\260\250/HW_Odd and Even Transactions.sql" "b/SQL/14\354\243\274\354\260\250/HW_Odd and Even Transactions.sql" new file mode 100644 index 00000000..3cc3ad05 --- /dev/null +++ "b/SQL/14\354\243\274\354\260\250/HW_Odd and Even Transactions.sql" @@ -0,0 +1,7 @@ +SELECT + transaction_date, + SUM(CASE WHEN amount % 2 = 1 THEN amount ELSE 0 END) AS odd_sum, + SUM(CASE WHEN amount % 2 = 0 THEN amount ELSE 0 END) AS even_sum +FROM transactions +GROUP BY transaction_date +ORDER BY transaction_date; \ No newline at end of file diff --git "a/SQL/14\354\243\274\354\260\250/HW_Students and Examinations.sql" "b/SQL/14\354\243\274\354\260\250/HW_Students and Examinations.sql" new file mode 100644 index 00000000..d1e680d3 --- /dev/null +++ "b/SQL/14\354\243\274\354\260\250/HW_Students and Examinations.sql" @@ -0,0 +1,8 @@ +SELECT s.student_id, s.student_name, sb.subject_name, COUNT(e.subject_name) AS attended_exams +FROM Students s + CROSS JOIN Subjects sb + LEFT JOIN Examinations e + ON s.student_id = e.student_id + AND sb.subject_name = e.subject_name +GROUP BY s.student_id, s.student_name, sb.subject_name +ORDER BY s.student_id, s.student_name, sb.subject_name; \ No newline at end of file