diff --git "a/Java/\354\235\264\354\240\225\353\263\265/[Week11]Heap, UnionFind/[BOJ] \354\265\234\353\214\200 \355\236\231_11279_\354\213\244\353\262\2042.java" "b/Java/\354\235\264\354\240\225\353\263\265/[Week11]Heap, UnionFind/[BOJ] \354\265\234\353\214\200 \355\236\231_11279_\354\213\244\353\262\2042.java" new file mode 100644 index 0000000..aa1e51d --- /dev/null +++ "b/Java/\354\235\264\354\240\225\353\263\265/[Week11]Heap, UnionFind/[BOJ] \354\265\234\353\214\200 \355\236\231_11279_\354\213\244\353\262\2042.java" @@ -0,0 +1,32 @@ +package week11; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Collections; +import java.util.PriorityQueue; + +public class Maxheap { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int count = Integer.parseInt(br.readLine()); + int op = 0; + + // 최소힙 문제와 완전히 동일하지만, Collections.reverseOrder()를 줘서 최대힙으로 사용 + PriorityQueue heap = new PriorityQueue<>(Collections.reverseOrder()); + + for (int i=0; i heap = new PriorityQueue<>(); + + for (int i=0; i minHeap = new PriorityQueue<>(); + + public int solution(int[] scoville, int K) { + int answer = 0; + + for (int i : scoville) { + minHeap.add(i); + } + + // minHeap의 최소값이 K보다 작은 경우 계속 반복 + while(minHeap.peek() < K){ + try { + mix(scoville); + } catch (NullPointerException e) { + /* + NullPointerException이 발생했다면 + 1. while문에서 k값을 넘지 않는 값이 있다는 것을 확인했고 + 2. Null이 반환되었다면 힙에 남은 항목이 1이었다는 것 + -> 따라서 k보다 매운 음식을 만들 수 없는 상황. -1을 출력 + */ + return -1; + } + answer++; + } + + return answer; + } + + private int mix(int[] scoville) throws NullPointerException{ + Integer min = minHeap.poll(); + Integer min2 = minHeap.poll(); + + // 여기서 result가 Null이라면 NullPointerException 발생 (큐가 비어있었다면) + int result = min + min2*2; + minHeap.add(result); + + return result; + } +} \ No newline at end of file diff --git "a/Java/\354\235\264\354\240\225\353\263\265/[Week14]dfs, bfs/[BOJ] \354\210\250\353\260\224\352\274\255\354\247\210_1679_\354\213\244\353\262\2041.java" "b/Java/\354\235\264\354\240\225\353\263\265/[Week14]dfs, bfs/[BOJ] \354\210\250\353\260\224\352\274\255\354\247\210_1679_\354\213\244\353\262\2041.java" new file mode 100644 index 0000000..cf56e3e --- /dev/null +++ "b/Java/\354\235\264\354\240\225\353\263\265/[Week14]dfs, bfs/[BOJ] \354\210\250\353\260\224\352\274\255\354\247\210_1679_\354\213\244\353\262\2041.java" @@ -0,0 +1,64 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +/* + 의문점 + 해당 로직은 최소 깊이를 찾는 것이 아닌, 가장 먼저 만난 상황에서의 깊이를 찾는 로직 + 채점을 했을 때는 정답으로 나오기는 하는데, 이유가 무엇일지 고민해봐도 모르겠습니다. + */ + +public class Main { + private static Queue queue = new LinkedList<>(); + private static boolean[] visited = new boolean[100001]; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int soobin = Integer.parseInt(st.nextToken()); // 수빈이 출발하는 위치 + int sister = Integer.parseInt(st.nextToken()); // 동생의 위치 + + // 수빈이 출발하는 지점을 깊이 0으로 세팅 + queue.add(new int[]{soobin, 0}); + visited[soobin] = true; + System.out.println(bfs(sister)); + } + + // 현재 점의 정수를 큐에 넣으며 bfs 탐색을 진행한다. + private static int bfs(int sister) { + int curr, temp, depth=0; + + while (!queue.isEmpty()) { + // 큐에서 현재 지점과 깊이를 가져옴 + int[] data = queue.poll(); + curr = data[0]; + depth = data[1]; + + // 동생을 만났다면 깊이 리턴 + if(sister == curr) + return depth; + + // 가능한 연산들을 큐에 추가 + temp = curr * 2; + if(temp <= 100000 && visited[temp] == false) { + queue.add(new int[]{temp, depth + 1}); + visited[temp] = true; + } + + temp = curr - 1; + if(temp >= 0 && visited[temp] == false){ + queue.add(new int[]{temp, depth + 1}); + visited[temp] = true; + } + + temp = curr +1; + if(temp <= 100000 && visited[temp] == false){ + queue.add(new int[]{temp, depth + 1}); + visited[temp] = true; + } + } + // 탐색실패, 문제 조건 상 도달할 수는 없음 + return -1; + } +} \ No newline at end of file diff --git "a/Java/\354\235\264\354\240\225\353\263\265/[Week14]dfs, bfs/[PGS] \352\262\214\354\236\204 \353\247\265 \354\265\234\353\213\250\352\261\260\353\246\254_Level2.java" "b/Java/\354\235\264\354\240\225\353\263\265/[Week14]dfs, bfs/[PGS] \352\262\214\354\236\204 \353\247\265 \354\265\234\353\213\250\352\261\260\353\246\254_Level2.java" new file mode 100644 index 0000000..b5f90ae --- /dev/null +++ "b/Java/\354\235\264\354\240\225\353\263\265/[Week14]dfs, bfs/[PGS] \352\262\214\354\236\204 \353\247\265 \354\265\234\353\213\250\352\261\260\353\246\254_Level2.java" @@ -0,0 +1,54 @@ +import java.util.ArrayList; +import java.util.Queue; +import java.util.LinkedList; + +class Solution { + public int solution(int[][] maps) { + // 배열의 길이 설정 + int rowLength = maps.length; + int colLength = maps[0].length; + + // 2차원 배열을 벽으로 한 줄씩 패딩 > 탐색 시 코드 가독성 개선 (조건문 최소화) + int[][] padded = new int[rowLength+2][colLength+2]; // 패딩된 maps 배열 + boolean[][] visited = new boolean[rowLength+2][colLength+2]; // visited + Queue queue = new LinkedList<>(); // 탐색에 사용할 큐 + + for (int i = 0; i < rowLength; i++) { + for (int j = 0; j < colLength; j++) { + padded[i + 1][j + 1] = maps[i][j]; + } + } + + // 초기 위치 설정 + visited[1][1] = true; + queue.add(new int[]{1, 1, 0}); // {x, y, depth} + + int[] dirX = {0, 0, +1, -1}; + int[] dirY = {+1, -1, 0, 0}; + + while(!queue.isEmpty()) { + int[] data = queue.poll(); + int x = data[0]; + int y = data[1]; + int depth = data[2] + 1; + + // 도착 위치라면 깊이를 리턴 + if (x == rowLength && y == colLength){ + return depth; + } else { + for(int i=0; i<4; i++) { + int newX = x + dirX[i]; + int newY = y + dirY[i]; + + if(padded[newX][newY] == 1 && visited[newX][newY] == false){ + queue.add(new int[]{newX, newY, depth}); + visited[newX][newY] = true; + } + } + } + } + + // 탐색실패 + return -1; + } +} \ No newline at end of file diff --git "a/Java/\354\235\264\354\240\225\353\263\265/[Week14]dfs, bfs/[PGS] \353\204\244\355\212\270\354\233\214\355\201\254_Level3.java" "b/Java/\354\235\264\354\240\225\353\263\265/[Week14]dfs, bfs/[PGS] \353\204\244\355\212\270\354\233\214\355\201\254_Level3.java" new file mode 100644 index 0000000..6731c27 --- /dev/null +++ "b/Java/\354\235\264\354\240\225\353\263\265/[Week14]dfs, bfs/[PGS] \353\204\244\355\212\270\354\233\214\355\201\254_Level3.java" @@ -0,0 +1,63 @@ +import java.util.HashSet; + +class Solution { + private static int n; + private int[][] computers; + + // 접근 : UNION-FIND? + // 알고리즘 정석대로 풀지는 않고,(Union을 만들어주지 않고) + // 연결된 네트워크에서 가장 큰 값을 갖는 노드를 대표값으로 설정하고 + // 완성된 대표값의 종류를 세는 방법으로 해결했습니다. + // GPT한테 물어보니깐 이 로직은 실제 Union-Find처럼 방향 그래프까지 커버하지는 못한다고 하네요 + public int solution(int n, int[][] computers) { + this.n = n; + this.computers = computers; + int[] represent = new int[n]; + + // 대표값을 자기 자신으로 초기화 + for (int i = 0; i < n; i++) { + represent[i] = i; + System.out.printf("%d ", represent[i]); + } + System.out.println(); + + // 각 노드마다 DFS 돌려서 '해당 노드가 속한 것의 최대 인덱스'를 대표값으로 저장 + for (int i = 0; i < n; i++) { + int[] visited = new int[n]; // i마다 방문 기록 새로 + int max = findMaxDFS(visited, i); + represent[i] = max; // i의 대표값은 max라고 가정 + System.out.printf("%d -> %d%n", i, max); + } + + // represent 배열 안에 있는 값들의 고유 개수 세기 + HashSet set = new HashSet<>(); + for (int i = 0; i < n; i++) { + set.add(represent[i]); + } + + int answer = set.size(); + System.out.println("distinct reps = " + answer); + + return answer; + } + + // 각 노드에 대해 탐색할 때마다 visited를 초기화해야함 + int findMaxDFS(int[] visited, int curr) { + // 지금 노드를 방문처리 + visited[curr] = 1; + int max = curr; + + // curr에서 갈 수 있는 모든 노드 탐색 + for (int i = 0; i < n; i++) { + // 연결되어 있고 아직 방문 안 했으면 DFS 진행 + if (computers[curr][i] == 1 && visited[i] == 0) { + int childMax = findMaxDFS(visited, i); + if (max < childMax) { + max = childMax; + } + } + } + + return max; + } +} diff --git a/README.md b/README.md index b3aac48..2eb2c2c 100644 --- a/README.md +++ b/README.md @@ -153,10 +153,4 @@ git commit -m "[PGS] 이종혁 / Hello World / 브론즈5 / 10 " | Week8 | 25.09.03 | Greedy2(그리디) | 지붕잃고 외양간 고치기
[강의실 배정](https://www.acmicpc.net/problem/11000)
[섬 연결하기](https://school.programmers.co.kr/learn/courses/30/lessons/42861) | -

Lv.3 | 조원희 | | Week9 | 25.09.10 | Dynamic Programming1(동적 계획법) | [다리 놓기](https://www.acmicpc.net/problem/1010)
[1로 만들기](https://www.acmicpc.net/problem/1463)|
| 조원희 | | Week10 | 25.09.17 | Dynamic Programming2(동적 계획법) | [가장 긴 증가하는 부분 수열](https://www.acmicpc.net/problem/11053)
[가장 긴 바이토닉 부분 수열](https://www.acmicpc.net/problem/11054)
[정수 삼각형](https://school.programmers.co.kr/learn/courses/30/lessons/43105)
[0/1 Knapsack](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWBJAVpqrzQDFAWr&)|

Lv.3
D3 | 조원희 | -| - | 25.09.24 | [백엔드 기술 세미나] | - | - | - | -| Week11 | 25.10.01 | Data Structure(자료구조)
   - Heap(힙)
   - Priority Queue(우선순위 큐)

Graph Theory(그래프 이론)|[최대 힙](https://www.acmicpc.net/problem/11279)
[최소 힙](https://www.acmicpc.net/problem/1927)
[더 맵게](https://school.programmers.co.kr/learn/courses/30/lessons/42626)

[연결 요소의 개수](https://www.acmicpc.net/problem/11724)|

Lv.2


| 조원희 | -| - | 25.10.08 | [추석] | - | - | - | -| Week12 | 25.10.15 | Two Pointers(투 포인터)
Sliding Window(슬라이딩 윈도우)
Range Sum(구간합)
Prefix Sum(누적합)|[두 수의 합](https://www.acmicpc.net/problem/3273)
[연속된 부분 수열의 합](https://school.programmers.co.kr/learn/courses/30/lessons/178870)
[꿀 아르바이트](https://www.acmicpc.net/problem/12847)
[수열](https://www.acmicpc.net/problem/2559)
[부분합](https://www.acmicpc.net/problem/1806)
[나머지 합](https://www.acmicpc.net/problem/10986)|
Lv.2



| 조원희 | -| Week13 | 25.10.22 | Prefix Sum(누적합) | [피아노 체조](https://www.acmicpc.net/problem/21318)
[파괴되지 않은 건물](https://school.programmers.co.kr/learn/courses/30/lessons/92344) |
Lv.3 | 조원희 | -| Week14 | 25.10.29 | DFS BFS1(깊이/넓이 우선 탐색) | [DFS와 BFS](https://www.acmicpc.net/problem/1260)
[숨바꼭질](https://school.programmers.co.kr/learn/courses/30/lessons/1697)
[게임 맵 최단거리](https://school.programmers.co.kr/learn/courses/30/lessons/1844)
[네트워크](https://school.programmers.co.kr/learn/courses/30/lessons/43162)
|

Lv.2
Lv.3 | 조원희 | -| Week15 | 25.11.05 | DFS BFS2(깊이/넓이 우선 탐색) | [미로2](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14wL9KAGkCFAYD)
[보급로](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15QRX6APsCFAYD)
[파핑파핑 지뢰찾기](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5LwsHaD1MDFAXc&)
|D4
D4
D4 | 조원희 | +