Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Integer> heap = new PriorityQueue<>(Collections.reverseOrder());

for (int i=0; i<count; i++){
op = Integer.parseInt(br.readLine());

if (op == 0) {
if (heap.isEmpty()){
System.out.println(0);
} else {
System.out.println(heap.poll());
}
} else {
heap.add(op);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package week11;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;

public class Minheap {
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;

// PriorityQueue의 Default 설정이 최소힙
PriorityQueue<Integer> heap = new PriorityQueue<>();

for (int i=0; i<count; i++){
op = Integer.parseInt(br.readLine());

// 0이라면 최소값(빈 힙일 때 0) 출력
if (op == 0) {
if (heap.isEmpty()){
System.out.println(0);
} else {
System.out.println(heap.poll());
}
} else {
// 0이 아니라면 힙에 추가
heap.add(op);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package week11;

import java.util.PriorityQueue;

class Solution {
// 최소힙 사용
PriorityQueue<Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<int[]> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<int[]> 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;
}
}
63 changes: 63 additions & 0 deletions Java/이정복/[Week14]dfs, bfs/[PGS] 네트워크_Level3.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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;
}
}
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,4 @@ git commit -m "[PGS] 이종혁 / Hello World / 브론즈5 / 10 "
| Week8 | 25.09.03 | Greedy2(그리디) | 지붕잃고 외양간 고치기<br/>[강의실 배정](https://www.acmicpc.net/problem/11000)<br/>[섬 연결하기](https://school.programmers.co.kr/learn/courses/30/lessons/42861) | -<br/><img height="20px" width="25px" src="https://static.solved.ac/tier_small/12.svg"/><br/>Lv.3 | 조원희 |
| Week9 | 25.09.10 | Dynamic Programming1(동적 계획법) | [다리 놓기](https://www.acmicpc.net/problem/1010)<br/>[1로 만들기](https://www.acmicpc.net/problem/1463)| <img height="20px" width="25px" src="https://static.solved.ac/tier_small/6.svg"/><br/><img height="20px" width="25px" src="https://static.solved.ac/tier_small/8.svg"/> | 조원희 |
| Week10 | 25.09.17 | Dynamic Programming2(동적 계획법) | [가장 긴 증가하는 부분 수열](https://www.acmicpc.net/problem/11053)<br/>[가장 긴 바이토닉 부분 수열](https://www.acmicpc.net/problem/11054)<br/>[정수 삼각형](https://school.programmers.co.kr/learn/courses/30/lessons/43105)<br/>[0/1 Knapsack](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWBJAVpqrzQDFAWr&)| <img height="20px" width="25px" src="https://static.solved.ac/tier_small/9.svg"/><br/><img height="20px" width="25px" src="https://static.solved.ac/tier_small/12.svg"/> </br> Lv.3</br>D3 | 조원희 |
| - | 25.09.24 | [백엔드 기술 세미나] | - | - | - |
| Week11 | 25.10.01 | Data Structure(자료구조)<br/>&nbsp;&nbsp;&nbsp;- Heap(힙)<br/>&nbsp;&nbsp;&nbsp;- Priority Queue(우선순위 큐)<br/><br/>Graph Theory(그래프 이론)|[최대 힙](https://www.acmicpc.net/problem/11279)<br/>[최소 힙](https://www.acmicpc.net/problem/1927)<br/>[더 맵게](https://school.programmers.co.kr/learn/courses/30/lessons/42626)<br/><br/>[연결 요소의 개수](https://www.acmicpc.net/problem/11724)| <img height="20px" width="25px" src="https://static.solved.ac/tier_small/9.svg"/><br/><img height="20px" width="25px" src="https://static.solved.ac/tier_small/9.svg"/><br/>Lv.2<br/><br/><img height="20px" width="25px" src="https://static.solved.ac/tier_small/9.svg"/><br/> | 조원희 |
| - | 25.10.08 | [추석] | - | - | - |
| Week12 | 25.10.15 | Two Pointers(투 포인터)<br/>Sliding Window(슬라이딩 윈도우)<br/>Range Sum(구간합)<br/>Prefix Sum(누적합)|[두 수의 합](https://www.acmicpc.net/problem/3273)<br/>[연속된 부분 수열의 합](https://school.programmers.co.kr/learn/courses/30/lessons/178870)<br/>[꿀 아르바이트](https://www.acmicpc.net/problem/12847)<br/>[수열](https://www.acmicpc.net/problem/2559)<br/>[부분합](https://www.acmicpc.net/problem/1806)<br/>[나머지 합](https://www.acmicpc.net/problem/10986)| <img height="20px" width="25px" src="https://static.solved.ac/tier_small/8.svg"/><br/>Lv.2<br/><img height="20px" width="25px" src="https://static.solved.ac/tier_small/8.svg"/><br/> <img height="20px" width="25px" src="https://static.solved.ac/tier_small/8.svg"/><br/><img height="20px" width="25px" src="https://static.solved.ac/tier_small/12.svg"/><br/><img height="20px" width="25px" src="https://static.solved.ac/tier_small/12.svg"/>| 조원희 |
| Week13 | 25.10.22 | Prefix Sum(누적합) | [피아노 체조](https://www.acmicpc.net/problem/21318)<br/>[파괴되지 않은 건물](https://school.programmers.co.kr/learn/courses/30/lessons/92344) | <img height="20px" width="25px" src="https://static.solved.ac/tier_small/10.svg"/><br/>Lv.3 | 조원희 |
| Week14 | 25.10.29 | DFS BFS1(깊이/넓이 우선 탐색) | [DFS와 BFS](https://www.acmicpc.net/problem/1260)<br/>[숨바꼭질](https://school.programmers.co.kr/learn/courses/30/lessons/1697)<br/>[게임 맵 최단거리](https://school.programmers.co.kr/learn/courses/30/lessons/1844)<br/>[네트워크](https://school.programmers.co.kr/learn/courses/30/lessons/43162)<br/> | <img height="20px" width="25px" src="https://static.solved.ac/tier_small/9.svg"/><br/><img height="20px" width="25px" src="https://static.solved.ac/tier_small/10.svg"/><br/>Lv.2<br/>Lv.3 | 조원희 |
| Week15 | 25.11.05 | DFS BFS2(깊이/넓이 우선 탐색) | [미로2](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14wL9KAGkCFAYD)<br/>[보급로](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15QRX6APsCFAYD)<br/>[파핑파핑 지뢰찾기](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5LwsHaD1MDFAXc&)<br/>|D4<br/>D4<br/>D4 | 조원희 |