๐ ํ๋ก๊ทธ๋๋จธ์ค / ์คํ&ํ / Level2 / ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ
๐ ๋ฌธ์
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๐ซ ์ ํ ์ฌํญ
- bridge_length๋ 1 ์ด์ 10,000 ์ดํ์ ๋๋ค.
- weight๋ 1 ์ด์ 10,000 ์ดํ์ ๋๋ค.
- truck_weights์ ๊ธธ์ด๋ 1 ์ด์ 10,000 ์ดํ์ ๋๋ค.
- ๋ชจ๋ ํธ๋ญ์ ๋ฌด๊ฒ๋ 1 ์ด์ weight ์ดํ์ ๋๋ค.
๐ ์ ์ถ๋ ฅ ์
| bridge_length | weight | truck_weights | return |
| 2 | 10 | [7,4,5,6] | 8 |
| 100 | 100 | [10] | 101 |
| 100 | 100 | [10,10,10,10,10,10,10,10,10,10] | 110 |
๐ค ํ์ด ๋ฐฉ๋ฒ



๐ป ํ์ด ์ฝ๋
import java.util.Queue;
import java.util.LinkedList;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int time = 0; // ํ์ฌ ์๊ฐ
int tw = 0; // ๋ค๋ฆฌ ์์ ์ด ๋ฌด๊ฒ
int index = 0; // ์ง๋๊ฐ์ผํ๋ ๋ค์ index
Queue<Integer> bridge = new LinkedList<>(); // ๋ค๋ฆฌ
Queue<Integer> stop = new LinkedList<>(); // ๋ถ๊ธฐ
while (index < truck_weights.length) {// ๋ค ๋ฃ์ ๋ ๊น์ง
if (!stop.isEmpty() && time == stop.peek()) {
// ๋ฌด๊ฒ ๋นผ๊ณ & ํ์์ ๋นผ๊ณ ๋์
tw -= bridge.poll();
stop.remove();
}
// (1) ๋ ๋ฃ์ ์ ์๋ ์ํ๋ฉด
// ๋ฃ๊ณ , ๋ฌด๊ฒ์ถ๊ฐ, ์ข
๋ฃ์๊ฐ ์ถ๊ฐ
if (bridge.size() < bridge_length && tw + truck_weights[index] <= weight) {
tw += truck_weights[index]; // ๋ฌด๊ฒ ์ถ๊ฐ
bridge.add(truck_weights[index]); // ๋ค๋ฆฌ์ ๋ฃ๊ธฐ
stop.add(time + bridge_length); // ์๊ฐ ๋ถ๊ธฐ์ ์ถ๊ฐ
time++; // ์๊ฐ ํ๋ฆ
index++; // ๋ค์ ๋ฐฐ์ด
continue;
}
// (2) ๋ ๋ฃ์ ์ ์๋ ์ํ๋ฉด ๊ทธ๋๋ก ์๊ฐ์ถ๊ฐ +1
time ++;
}
while (!stop.isEmpty()) {
time = stop.poll();
}
return time+1;
}
}
[level 2] Title: ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ, Time: 0.21 ms, Memory: 76.8 MB -BaekjoonHub · Young998904/Practice_Algorithm_Auto@
Show file tree Showing 2 changed files with 190 additions and 0 deletions.
github.com