Skip to content

Commit 5423a29

Browse files
authored
Merge pull request #211 from AlgorithmStudy-Allumbus/minjeong3
Minjeong / 5์›” 3์ฃผ์ฐจ / 4๋ฌธ์ œ
2 parents fed9918 + 88c9f7c commit 5423a29

4 files changed

+179
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
# 1. ์ž…๋ ฅ ์ฒ˜๋ฆฌ
6+
M, N = map(int, input().split()) # ๊ฐ€๋กœ ์นธ ์ˆ˜, ์„ธ๋กœ ์นธ ์ˆ˜
7+
box = [list(map(int, input().split())) for _ in range(N)] # ํ† ๋งˆํ† 
8+
9+
# 2. ์ดˆ๊ธฐ ์„ค์ •
10+
queue = deque([]) # ํ
11+
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)] # ๋ฐฉํ–ฅ๋ฒกํ„ฐ
12+
day = 0 # ์ •๋‹ต์ด ๋‹ด๊ธธ ๋ณ€์ˆ˜
13+
14+
# 3. ํ์— ์ดˆ๊ธฐ ์ต์€ ํ† ๋งˆํ†  ์œ„์น˜ ์ €์žฅ
15+
for i in range(N):
16+
for j in range(M):
17+
if box[i][j] == 1:
18+
queue.append((i, j))
19+
20+
# 4. BFS ํƒ์ƒ‰
21+
while queue:
22+
# ์ฒ˜์Œ ํ† ๋งˆํ†  ๊บผ๋‚ด๊ธฐ
23+
x, y = queue.popleft()
24+
25+
# ์ฒ˜์Œ ํ† ๋งˆํ† ์˜ ์ธ์ ‘ํ•œ ํ† ๋งˆํ†  ์ฐพ๊ธฐ
26+
for dx, dy in directions:
27+
nx, ny = x + dx, y + dy
28+
29+
# ๋ฒ”์œ„ ๋‚ด์— ์žˆ๊ณ , ํ† ๋งˆํ† ๊ฐ€ ์ต์ง€ ์•Š์€ ๊ฒฝ์šฐ
30+
if (0 <= nx < N and 0 <= ny < M) and (box[nx][ny] == 0):
31+
# ์ตํžˆ๊ณ  1 ๋”ํ•ด์ฃผ๋ฉฐ ํšŸ์ˆ˜ ์„ธ๊ธฐ
32+
# ์—ฌ๊ธฐ์„œ ๋‚˜์˜จ ์ œ์ผ ํฐ ๊ฐ’์ด ์ •๋‹ต์ด ๋œ๋‹ค.
33+
box[nx][ny] += box[x][y] + 1 # ์ผ์ˆ˜ ๋ˆ„์ 
34+
queue.append((nx, ny))
35+
36+
37+
# 5. ์ •๋‹ต ๊ตฌํ•˜๊ธฐ
38+
for row in box:
39+
for tomato in row:
40+
# ๋ชจ๋‘ ํƒ์ƒ‰ํ–ˆ์ง€๋งŒ ํ† ๋งˆํ† ๊ฐ€ ๋ชจ๋‘ ์ต์ง€ ์•Š์•˜๋‹ค๋ฉด -1 ์ถœ๋ ฅ
41+
if tomato == 0:
42+
print(-1)
43+
exit()
44+
45+
# ๋‹ค ์ตํ˜”๋‹ค๋ฉด ์ตœ๋Œ“๊ฐ’์ด ์ •๋‹ต
46+
day = max(day, max(row))
47+
48+
# 6. ์ •๋‹ต ์ถœ๋ ฅ
49+
print(day - 1) # ์ฒ˜์Œ์— 1๋กœ ์ต์€ ํ† ๋งˆํ† ๋ฅผ ํ‘œํ˜„ํ–ˆ์œผ๋‹ˆ 1์„ ๋นผ์ค€๋‹ค.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
# 1. ์ž…๋ ฅ ์ฒ˜๋ฆฌ
6+
M, N, H = map(int, input().split()) # ๊ฐ€๋กœ, ์„ธ๋กœ, ๋†’์ด
7+
box = [[list(map(int, input().split())) for _ in range(N)] for _ in range(H)]
8+
9+
# 2. ์ดˆ๊ธฐ ๋ณ€์ˆ˜ ์„ค์ •
10+
queue = deque([])
11+
directions = [(-1, 0, 0), (0, 1, 0), (1, 0, 0), (0, -1, 0),
12+
(0, 0, 1), (0, 0, -1)] # ์œ„-์˜ค๋ฅธ์ชฝ-์•„๋ž˜-์™ผ์ชฝ-์•ž-๋’ค
13+
day = 0 # ์ •๋‹ต์œผ๋กœ ๋ฐ˜ํ™˜ํ•  ๋ณ€์ˆ˜
14+
15+
# 3. ์ดˆ๊ธฐ ์ต์€ ํ† ๋งˆํ† ๋ฅผ ํ์— ์ถ”๊ฐ€ํ•˜๊ธฐ
16+
for i in range(H):
17+
for j in range(N):
18+
for k in range(M):
19+
if box[i][j][k] == 1:
20+
queue.append((i, j, k))
21+
22+
# 4. BFS ํƒ์ƒ‰
23+
while queue:
24+
z, y, x = queue.popleft()
25+
26+
for dx, dy, dz in directions:
27+
nx, ny, nz = x + dx, y + dy, z + dz
28+
# ๋ฒ”์œ„ ๋‚ด์— ์žˆ๊ณ  ์•„์ง ์•ˆ ์ต์€ ํ† ๋งˆํ† ๋ผ๋ฉด
29+
if (0 <= nx < M and 0 <= ny < N and 0 <= nz < H) and (box[nz][ny][nx] == 0):
30+
box[nz][ny][nx] += box[z][y][x] + 1 # ์ต์€ ๋‚ ์งœ ๋ˆ„์  ๊ฐฑ์‹ 
31+
queue.append((nz, ny, nx))
32+
33+
# 5. ์ •๋‹ต ๊ตฌํ•˜๊ธฐ
34+
for height in box:
35+
for row in height:
36+
for tomato in row:
37+
# ์•ˆ ์ต์€ ํ† ๋งˆํ† ๊ฐ€ ๋‚จ์•„์žˆ๋Š”์ง€ ์—ฌ๋ถ€ ํ™•์ธ
38+
if tomato == 0:
39+
print(-1)
40+
exit()
41+
# ์ต๋Š”๋ฐ ๊ฑธ๋ฆฐ ์ตœ๋Œ€ ์ผ์ˆ˜ ์ถ”์ 
42+
day = max(day, max(row))
43+
44+
# 6. ์ •๋‹ต ์ถœ๋ ฅ
45+
print(day - 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int solution(int bridge_length, int weight, int[] truck_weights) {
5+
6+
Queue<Integer> bridge = new LinkedList<>();
7+
for (int i = 0; i < bridge_length; i++) {
8+
bridge.offer(0); // ์ฒ˜์Œ์— ๋นˆ ์นธ์œผ๋กœ ์ฑ„์›Œ๋‘”๋‹ค.
9+
}
10+
11+
int time = 0; // ์ „์ฒด ๊ฒฝ๊ณผ ์‹œ๊ฐ„
12+
int current = 0; // ๋‹ค๋ฆฌ ์œ„ ํ˜„์žฌ ๋ฌด๊ฒŒ ํ•ฉ
13+
int idx = 0; // ๋‹ค์Œ์— ๋ณด๋‚ผ ํŠธ๋Ÿญ ์ธ๋ฑ์Šค
14+
15+
while (idx < truck_weights.length || current > 0) {
16+
time++;
17+
// 1. ๋‹ค๋ฆฌ์—์„œ ํ•œ ์นธ ๋น ์ ธ๋‚˜์˜ด
18+
int left = bridge.poll();
19+
current -= left; // ๋น ์ ธ๋‚˜๊ฐ„ ๊ฒŒ 0์ผ์ˆ˜๋„ ์žˆ๊ณ , ํŠธ๋Ÿญ์ผ ์ˆ˜๋„ ์žˆ์Œ.
20+
21+
// 2. ๋‹ค์Œ ํŠธ๋Ÿญ์„ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์œผ๋ฉด ์˜ฌ๋ฆฌ๊ธฐ, ์•„๋‹ˆ๋ฉด ๋นˆ ์นธ(0) ์˜ฌ๋ฆฌ๊ธฐ
22+
if (idx < truck_weights.length && current + truck_weights[idx] <= weight) {
23+
bridge.offer(truck_weights[idx]);
24+
current += truck_weights[idx];
25+
idx++;
26+
}
27+
else {
28+
bridge.offer(0);
29+
}
30+
}
31+
return time;
32+
}
33+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* ๋‚ด ํ’€์ด */
2+
import java.util.*;
3+
4+
5+
class Solution {
6+
public int[] solution(int[] prices) {
7+
int[] answer = new int[prices.length];
8+
9+
for (int i = 0; i < answer.length; i++) {
10+
for (int j = i+1; j < answer.length; j++) {
11+
answer[i] += 1;
12+
if (prices[i] > prices[j] ) {
13+
break;
14+
}
15+
}
16+
}
17+
return answer;
18+
}
19+
}
20+
21+
/* ์Šคํƒ ํ’€์ด */
22+
import java.util.Stack;
23+
24+
public class Solution {
25+
public int[] solution(int[] prices) {
26+
int n = prices.length;
27+
int[] answer = new int[n];
28+
// ์•„์ง ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์€ ์‹œ์ ๋“ค์˜ ์ธ๋ฑ์Šค๋ฅผ ๋‹ด์„ ์Šคํƒ
29+
Stack<Integer> st = new Stack<>();
30+
31+
for (int i = 0; i < n; i++) {
32+
// ์ง€๊ธˆ ๊ฐ€๊ฒฉ(prices[i])์ด ์Šคํƒ ์œ„ ์‹œ์ ์˜ ๊ฐ€๊ฒฉ๋ณด๋‹ค ๋‚ฎ๋‹ค๋ฉด,
33+
// ๊ทธ ์‹œ์ ์˜ ๊ฐ€๊ฒฉ์€ i ์‹œ์ ์— ๋–จ์–ด์ง„ ๊ฒƒ์ด๋ฏ€๋กœ
34+
// answer[idx] = i - idx ๋กœ ๊ตฌ๊ฐ„ ๊ธธ์ด๋ฅผ ํ™•์ •์ง“๊ณ  pop
35+
while (!st.isEmpty() && prices[st.peek()] > prices[i]) {
36+
int idx = st.pop();
37+
answer[idx] = i - idx;
38+
}
39+
// ํ˜„์žฌ ์‹œ์  i๋ฅผ ์Šคํƒ์— ์ถ”๊ฐ€
40+
st.push(i);
41+
}
42+
43+
// ๋งˆ์ง€๋ง‰๊นŒ์ง€ ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์€ ์ธ๋ฑ์Šค๋“ค์€
44+
// ๋๊นŒ์ง€(๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค n-1) ๋ฒ„ํ‹ด ์‹œ๊ฐ„์ด (n-1) - idx
45+
while (!st.isEmpty()) {
46+
int idx = st.pop();
47+
answer[idx] = (n - 1) - idx;
48+
}
49+
50+
return answer;
51+
}
52+
}

0 commit comments

Comments
ย (0)