Skip to content

Commit e0ec549

Browse files
feat: 프로그래머스 알고리즘 문제 복습
1 parent 7b469b1 commit e0ec549

File tree

5 files changed

+322
-0
lines changed

5 files changed

+322
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.algorithm2025.backjoon3.day005;
2+
3+
public class Example20250630_Q_L697 { // lettcode 번외 697번 Degree of an Array
4+
5+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.algorithm2025.backjoon3.day006;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
public class Example20250716_Q68937 { // 코딩테스트 연습 월간 코드 챌린지 시즌1 트리 트리오 중간값
9+
10+
static ArrayList<Integer>[] board;
11+
12+
public static int[] bfs(int idx, int n) {
13+
int[] visited = new int[n+1];
14+
Arrays.fill(visited, -1);
15+
Queue<Integer> q = new LinkedList<Integer>();
16+
q.offer(idx);
17+
visited[idx] = 0;
18+
19+
int[] max = {0, 0, 0};
20+
21+
while (!q.isEmpty()) {
22+
int old = q.poll();
23+
for (int newIdx : board[old]) {
24+
if (visited[newIdx] == -1) {
25+
visited[newIdx] = visited[old] + 1;
26+
q.offer(newIdx);
27+
28+
if (max[0] < visited[newIdx]) {
29+
max[0] = visited[newIdx];
30+
max[1] = newIdx;
31+
max[2] = 1;
32+
} else if (max[0] == visited[newIdx]) {
33+
max[2]++;
34+
max[1] = newIdx;
35+
}
36+
}
37+
}
38+
}
39+
40+
return max;
41+
}
42+
43+
public static int solution(int n, int[][] edges) {
44+
board = new ArrayList[n+1];
45+
for (int i = 1; i <= n; i++) {
46+
board[i] = new ArrayList<Integer>();
47+
}
48+
49+
int answer = 0;
50+
int max_value = -1;
51+
52+
for (int[] edge : edges) {
53+
int i = edge[0];
54+
int j = edge[1];
55+
board[i].add(j);
56+
board[j].add(i);
57+
}
58+
int[] temp = bfs(1, n);
59+
int[] temp2 = bfs(temp[1], n);
60+
61+
if (temp2[2] >= 2) {
62+
return temp2[0];
63+
}
64+
65+
else {
66+
int[] temp3 = bfs(temp2[1], n);
67+
68+
if (temp3[2] >= 2) {
69+
return temp3[0];
70+
}
71+
72+
else {
73+
return temp3[0]-1;
74+
}
75+
}
76+
}
77+
78+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package com.algorithm2025.backjoon3.day006;
2+
3+
import java.util.*;
4+
5+
public class Example20250716_Q84021 { //코딩테스트 연습 깊이/너비 우선 탐색(DFS/BFS) 퍼즐 조각 채우기
6+
7+
List<List<Point>> t = new ArrayList<>();
8+
List<List<Point>> g = new ArrayList<>();
9+
int[] dx = {0, 0, 1, -1};
10+
int[] dy = {1, -1, 0, 0};
11+
12+
public int Example20250716_Q84021(int[][] game_board, int[][] table) {
13+
int answer = 0;
14+
15+
int len = game_board.length;
16+
17+
for (int i = 0; i < len; i++) {
18+
for (int j = 0; j < len; j++) {
19+
game_board[i][j] = game_board[i][j] == 0 ? 1 : 0;
20+
}
21+
}
22+
23+
boolean[][] visited_t = new boolean[len][len];
24+
boolean[][] visited_g = new boolean[len][len];
25+
26+
for (int i = 0; i < len; i++) {
27+
for (int j = 0; j < len; j++) {
28+
if (table[i][j] == 1 && !visited_t[i][j])
29+
bfs(i, j, table, visited_t, t);
30+
31+
if (game_board[i][j] == 1 && !visited_g[i][j])
32+
bfs(i, j, game_board, visited_g, g);
33+
}
34+
}
35+
36+
answer = compareBlock(t, g, answer);
37+
38+
return answer;
39+
}
40+
41+
public int compareBlock(List<List<Point>> table, List<List<Point>> board, int answer) {
42+
int table_size = table.size();
43+
int board_size = board.size();
44+
45+
boolean[] visited = new boolean[board_size];
46+
47+
for (int i = 0; i < table_size; i++) {
48+
for (int j = 0; j < board_size; j++) {
49+
// 일치하면
50+
if (visited[j] || table.get(i).size() != board.get(j).size())
51+
continue;
52+
if (isRotate(table.get(i), board.get(j))) {
53+
visited[j] = true; // 블록으로 채워짐
54+
answer += board.get(j).size();
55+
break;
56+
}
57+
}
58+
}
59+
60+
return answer;
61+
}
62+
63+
public boolean isRotate(List<Point> table, List<Point> board) {
64+
65+
Collections.sort(board);
66+
67+
for (int i = 0; i < 4; i++) {
68+
69+
Collections.sort(table);
70+
71+
int curr_x = table.get(0).x;
72+
int curr_y = table.get(0).y;
73+
74+
for (int j = 0; j < table.size(); j++) {
75+
table.get(j).x -= curr_x;
76+
table.get(j).y -= curr_y;
77+
}
78+
79+
boolean check = true;
80+
81+
for (int j = 0; j < board.size(); j++) {
82+
if (board.get(j).x != table.get(j).x || board.get(j).y != table.get(j).y) {
83+
check = false;
84+
break;
85+
}
86+
}
87+
88+
if (check) {
89+
return true;
90+
} else {
91+
92+
for (int j = 0; j < table.size(); j++) {
93+
int temp = table.get(j).x;
94+
table.get(j).x = table.get(j).y;
95+
table.get(j).y = -temp;
96+
}
97+
}
98+
}
99+
100+
return false;
101+
}
102+
103+
public void bfs(int x, int y, int[][] board, boolean[][] visited, List<List<Point>> list) {
104+
visited[x][y] = true;
105+
106+
Queue<Point> q = new LinkedList<>();
107+
q.add(new Point(x, y));
108+
109+
List<Point> sub_list = new ArrayList<>();
110+
sub_list.add(new Point(0, 0));
111+
112+
while (!q.isEmpty()) {
113+
Point p = q.poll();
114+
115+
for (int i = 0; i < 4; i++) {
116+
int nx = p.x + dx[i];
117+
int ny = p.y + dy[i];
118+
119+
if (nx < 0 || ny < 0 || nx >= board.length || ny >= board.length)
120+
continue;
121+
122+
if (!visited[nx][ny] && board[nx][ny] == 1) {
123+
visited[nx][ny] = true;
124+
q.add(new Point(nx, ny));
125+
sub_list.add(new Point(nx - x, ny - y));
126+
}
127+
}
128+
}
129+
130+
list.add(sub_list);
131+
}
132+
133+
static class Point implements Comparable<Point> {
134+
int x, y;
135+
136+
Point(int x, int y) {
137+
this.x = x;
138+
this.y = y;
139+
}
140+
141+
public int compareTo(Point o) {
142+
int res = Integer.compare(this.x, o.x);
143+
if (res == 0) {
144+
res = Integer.compare(this.y, o.y);
145+
}
146+
return res;
147+
}
148+
}
149+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.algorithm2025.backjoon3.day006;
2+
3+
public class Example20250716_Q86053 {
4+
5+
public long Example20250716_Q86053(int a, int b, int[] g, int[] s, int[] w, int[] t) {
6+
long answer = Long.MAX_VALUE;
7+
long start = 0;
8+
long end = 4 * (long)Math.pow(10, 14);
9+
10+
while (start <= end) {
11+
long mid = (start + end) / 2;
12+
long gold = 0;
13+
long silver = 0;
14+
long add = 0;
15+
16+
for (int i = 0; i < t.length; i++) {
17+
long nowG = g[i];
18+
long nowS = s[i];
19+
long nowW = w[i];
20+
long nowT = t[i];
21+
22+
long moveCount = mid / (nowT * 2);
23+
if (mid % (nowT * 2) >= nowT) {
24+
moveCount += 1;
25+
}
26+
27+
gold += Math.min(nowG, moveCount * nowW);
28+
silver += Math.min(nowS, moveCount * nowW);
29+
add += Math.min(nowG + nowS, moveCount * nowW);
30+
}
31+
32+
if (gold >= a && silver >= b && add >= (a + b)) {
33+
answer = Math.min(mid, answer);
34+
end = mid - 1;
35+
} else {
36+
start = mid + 1;
37+
}
38+
}
39+
40+
return answer;
41+
}
42+
43+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.algorithm2025.backjoon3.day006;
2+
3+
public class Example20250716_Q92345 { //코딩테스트 연습 2022 KAKAO BLIND RECRUITMENT 사라지는 발판
4+
5+
6+
int[] dx = {0, 0, -1, 1};
7+
int[] dy = {-1, 1, 0, 0};
8+
int n, m;
9+
10+
boolean OOB(int x, int y) {
11+
return x < 0 || x >= n || y < 0 || y >= m;
12+
}
13+
14+
int[][] vis = new int[5][5];
15+
int[][] block = new int[5][5];
16+
17+
int solve(int curx, int cury, int opx, int opy) {
18+
if (vis[curx][cury] == 1) return 0;
19+
int ret = 0;
20+
for (int dir = 0; dir < 4; dir++) {
21+
int nx = curx + dx[dir];
22+
int ny = cury + dy[dir];
23+
if (OOB(nx, ny) || vis[nx][ny] == 1 || block[nx][ny] == 0) continue;
24+
vis[curx][cury] = 1;
25+
26+
int val = solve(opx, opy, nx, ny) + 1;
27+
28+
vis[curx][cury] = 0;
29+
30+
if (ret % 2 == 0 && val % 2 == 1) ret = val;
31+
else if (ret % 2 == 0 && val % 2 == 0) ret = Math.max(ret, val);
32+
else if (ret % 2 == 1 && val % 2 == 1) ret = Math.min(ret, val);
33+
}
34+
return ret;
35+
}
36+
37+
public int Example20250716_Q92345(int[][] board, int[] aloc, int[] bloc) {
38+
n = board.length;
39+
m = board[0].length;
40+
for (int i = 0; i < n; i++) {
41+
for (int j = 0; j < m; j++) {
42+
block[i][j] = board[i][j];
43+
}
44+
}
45+
return solve(aloc[0], aloc[1], bloc[0], bloc[1]);
46+
}
47+
}

0 commit comments

Comments
 (0)