๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค[Java] - (Level2)๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ

by ์ฃผ๋ฐœ2 2020. 3. 13.
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/42583

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

์ฝ”๋“œ

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class Truck{
	int weight;
	int index;

	public Truck(int weight, int index) {
		this.weight = weight;
		this.index = index;
	}
}

class Solution {

	public static int solution(int bridge_length, int weight, int[] truck_weights) {
		int time = 0;

		// ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๋Š” ํŠธ๋Ÿญ
		Queue<Integer> waitTruck = new LinkedList<Integer>();

		// ํ˜„์žฌ ๋‹ค๋ฆฌ ์œ„๋ฅผ ๊ฑด๋„ˆ๊ณ  ์žˆ๋Š” ํŠธ๋Ÿญ
		List<Truck> workingTruck = new ArrayList<Truck>();

		// ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ ๋„ฃ์–ด์คŒ
		for(int t : truck_weights)
			waitTruck.add(t);

		// ๋‹ค๋ฆฌ์œ„ ํŠธ๋Ÿญ์˜ ์ด ๋ฌด๊ฒŒ - ์ดˆ๊ธฐ๊ฐ’: ํ˜„์žฌ ๋‹ค๋ฆฌ์œ„๋กœ ์˜ฌ๋ผ๊ฐˆ ํŠธ๋Ÿญ(peek)
		int totalWeight = waitTruck.peek();

		// ์ฒซ๋ฒˆ์งธ ํŠธ๋Ÿญ์€ ๋‹ค๋ฆฌ์œ„๋กœ ๋ฐ”๋กœ ๊ฑด๋„ˆ๋ฏ€๋กœ ๋„ฃ์–ด์ฃผ๊ธฐ,
		workingTruck.add(new Truck(waitTruck.poll(), 0));

		// ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„๋•Œ๊นŒ์ง€
		while(!workingTruck.isEmpty()) {
			time ++;

			// โ˜… ํ˜„์žฌ ๋‹ค๋ฆฌ์œ„ ํŠธ๋Ÿญ์€ ๋ชจ๋‘ ํ•œ์นธ ์”ฉ ์ด๋™์‹œํ‚จ๋‹ค.
			for(int i=0; i<workingTruck.size(); i++) 
				workingTruck.get(i).index ++;

			// ๋‹ค๋ฆฌ๋ฅผ ๋ชจ๋‘ ๊ฑด๋„Œ ํŠธ๋Ÿญ์€ ๋นผ์ค€๋‹ค.
			// ๋๊นŒ์ง€ ๊ฐ”์œผ๋ฉด working์—์„œ ์ œ์™ธ
			if(workingTruck.get(0).index > bridge_length) {
				totalWeight -= workingTruck.get(0).weight;
				workingTruck.remove(0);
			}

			// ๋‹ค๋ฆฌ์œ„์— ํŠธ๋Ÿญ์„ ๋” ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๋Œ€๊ธฐ์—์„œ ๋นผ์ฃผ๊ณ , working์— ๋„ฃ๋Š”๋‹ค.
			if(!waitTruck.isEmpty() && totalWeight + waitTruck.peek() <= weight) {
				int nextTruck = waitTruck.poll();
				totalWeight += nextTruck;
				workingTruck.add(new Truck(nextTruck, 1));
			}
		}

		return time;
	}

ํ’€์ด

ํ + ๊ทธ๋ฆฌ๋”” + ๊ตฌํ˜„ (?) ์ •๋„์˜ ๋ฌธ์ œ ์ธ ๊ฒƒ ๊ฐ™๋‹ค.

๋ฌธ์ œ ํ’€๋•Œ ์ด๊ฒƒ์ €๊ฒƒ ์ƒ๊ฐํ•˜๋Š๋ผ ๋จธ๋ฆฌ๊ฐ€ ํ„ฐ์ง€๊ณ ,, ์ •๋‹ต ์ฝ”๋“œ ๋ณด๋ฉฐ ์ดํ•ดํ•˜๋Š”๋ฐ๋„ ๋จธ๋ฆฌ๊ฐ€ ํ„ฐ์ง„ ๋ฌธ์ œ๋‹ค ... 

์ฒ˜์Œ์—” ๋จผ์ € ๋“ค์–ด์˜จ ํŠธ๋Ÿญ์ด ๋จผ์ € ๋‚˜๊ฐ€๋Š” ์ˆœ์„œ์—ฌ์„œ ํ๋ฅผ ์ƒ๊ฐํ–ˆ๊ณ ,

ํŠธ๋Ÿญ์ด ๋“ค์–ด์˜ฌ ๋•Œ ๋งˆ๋‹ค ๋‹ค๋ฆฌ๊ฐ€ ๊ฒฌ๋”œ ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ์™€ ๋น„๊ตํ•ด์„œ, ๋‹ค์Œ ํŠธ๋Ÿญ๋„ ๋น„๊ตํ•ด๊ฐ€๋ฉฐ ๋ฌด๊ฒŒ์™€ ๊ฒฌ๋”œ์ˆ˜ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€

๋น„๊ตํ•ด๊ฐ€๋Š”์‹์œผ๋กœ ํŒ๋ณ„ํ•ด์„œ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ–ˆ์œผ๋‚˜ ๊ตฌํ˜„์€ ๋ชปํ•˜๊ณ  ํƒ€ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

https://taesan94.tistory.com/33

๋‹ค๋ฅธ ํ’€์ด๋“ค์„ ๋ด๋„ ์ดํ•ด๊ฐ€ ์•ˆ๋˜์„œ ๋‚˜์—๊ฒŒ ๊ทธ๋‚˜๋งˆ ์ดํ•ด๊ฐ€ ๊ฐ€๋Š” ์ฝ”๋“œ๋ฅผ ์ฐธ์กฐํ–ˆ๊ณ , ๋””๋ฒ„๊น…์„ ํ•ด๋ณด๋‹ˆ ์–ด๋Š์ •๋„ ์ดํ•ด๊ฐ€ ๊ฐ”๋‹ค.

๋‹ค์‹œ๋ด๋„ ์ ˆ๋Œ€ ๋ชป ํ’€ ๋ฌธ์ œ์ด๊ธฐ์— ์ฃผ๋ง์— ๋‹ค์‹œ ๋ณต์Šตํ•˜๋ฉฐ ํ’€์ด๋ฅผ ์˜ฌ๋ฆฌ๊ฒ ๋‹ค . . .

 

 

 

 

2020.03.15 22:00 ํ’€์ด

์ฃผ์–ด์ง„ ์ž…์ถœ๋ ฅ์—์„œ bridge_length = 2 , weight = 10 , truck_weights = [7, 4, 5, 6]์„ ๊ธฐ์ค€์œผ๋กœ ์ƒ๊ฐํ•œ๋‹ค.

์œ„ ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค ๊ฑด๋„๋•Œ๊นŒ์ง€ 1์ดˆ์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ, ๋ฌธ์ œ์˜ ์กฐ๊ฑด์— ๋งž๊ฒŒ ๊ตฌํ˜„ํ•œ๋‹ค.

ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ์ƒ์„ฑํ•ด์„œ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€๋•Œ๋Š” ๊ฑฐ์˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„์„œ ๋” ์–ด๋ ต๊ฒŒ ๋Š๊ผˆ๋‹ค.

 

Truck ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ, ์ด ํŠธ๋Ÿญ ๊ฐ์ฒด๋Š” ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ(๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ truck_weights)์™€

์ธ๋ฑ์Šค(ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ ์œ„์—์„œ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋ƒ„)๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ–๋Š” ํด๋ž˜์Šค๋‹ค.

๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๊ธฐ ์ „์˜ ํŠธ๋Ÿญ์„ ๋„ฃ์„ ํ waitTruck์™€ ํ˜„์žฌ ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๊ณ  ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ workingTruck

๋ฅผ ์„ ์–ธํ•œ๋‹ค.

waitTruck ์—๋Š” ํ˜„์žฌ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. ๊ทธ๋Ÿผ ํ์—๋Š” 7, 4, 5, 6 ์ด ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅ๋œ๋‹ค.

๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ์•ผ ํ•˜๋ฏ€๋กœ, ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ค€๋‹ค.

๋‹ค๋ฆฌ ํŠธ๋Ÿญ์˜ ์ด ๋ฌด๊ฒŒ๋„ ๊ฐ ํŠธ๋Ÿญ๋ณ„๋กœ ๋น„๊ตํ•ด์•ผํ•˜๋ฏ€๋กœ, ์ฒซ๋ฒˆ์งธ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ์ €์žฅํ•œ๋‹ค.

// ๋‹ค๋ฆฌ์œ„ ํŠธ๋Ÿญ์˜ ์ด ๋ฌด๊ฒŒ - ์ดˆ๊ธฐ๊ฐ’: ํ˜„์žฌ ๋‹ค๋ฆฌ์œ„๋กœ ์˜ฌ๋ผ๊ฐˆ ํŠธ๋Ÿญ(peek) int totalWeight = waitTruck.peek(); // ์ฒซ๋ฒˆ์งธ ํŠธ๋Ÿญ์€ ๋‹ค๋ฆฌ์œ„๋กœ ๋ฐ”๋กœ ๊ฑด๋„ˆ๋ฏ€๋กœ ๋„ฃ์–ด์ฃผ๊ธฐ, ์ธ๋ฑ์Šค = 0(์ฒซ๋ฒˆ์งธ) workingTruck.add(new Truck(waitTruck.poll(), 0));

 

 

๊ทธ ํ›„ ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„๋•Œ๊นŒ์ง€ - while(!workingTruck.isEmpty()) 1์ดˆ๋งˆ๋‹ค ํŠธ๋Ÿญ์„ ์ด๋™ํ•˜๊ณ , ๋ชจ๋‘ ๊ฑด๋„Œ ํŠธ๋Ÿญ์ด ์žˆ์œผ๋ฉด ์ œ์™ธํ•˜๊ณ , ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ(totalWeight)์™€ ๋‹ค๋ฆฌ๊ฐ€ ๊ฒฌ๋”œ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ(weight)๋ฅผ ๋น„๊ตํ•˜๋ฉฐ ๋‹ค๋ฆฌ์— ๋‹ค์Œ ํŠธ๋Ÿญ์„ ์˜ฌ๋ฆด์ง€, ์•„๋‹์ง€ ๋น„๊ตํ•œ๋‹ค.

 

ํŠธ๋Ÿญ์€ 1์ดˆ์— 1๋งŒํผ ์›€์ง์ด๋ฏ€๋กœ, ํ˜„์žฌ ๋‹ค๋ฆฌ์œ„์— ์žˆ๋Š” ํŠธ๋Ÿญ์„ ๋ชจ๋‘ ํ•œ ์นธ์”ฉ ์ด๋™์‹œํ‚จ๋‹ค.

			for(int i=0; i<workingTruck.size(); i++) 
				workingTruck.get(i).index ++;

 

 

 

์ด๋™์‹œํ‚จํ›„ ๋งŒ์•ฝ ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„Œ ํŠธ๋Ÿญ์ด ์žˆ์„๊ฒฝ์šฐ  ๊ฑด๋„Œ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋นผ๊ณ , ํ˜„์žฌ ๋ฆฌ์ŠคํŠธ์—์„œ ์ œ์™ธํ•œ๋‹ค.

			if(workingTruck.get(0).index > bridge_length) {
				totalWeight -= workingTruck.get(0).weight;
				workingTruck.remove(0);
			}

 

* ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„Œ ํŠธ๋Ÿญ์ด ์žˆ๋Š”์ง€ ํ™•์ธ์€ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ํŠธ๋Ÿญ ๊ฐ์ฒด์˜ ์ธ๋ฑ์Šค์™€ ๋‹ค๋ฆฌ์˜ ๊ธธ์ด๋ฅผ ๋น„๊ตํ•˜๋ฉด ๋œ๋‹ค.

 

 

 

 

๊ทธ ํ›„ ํ˜„์žฌ ํ†ต๊ณผํ•˜๋ ค๋Š” ํŠธ๋Ÿญ๊ณผ, ๋‹ค๋ฆฌ์— ์žˆ๋Š” ํŠธ๋Ÿญ์„ ๋น„๊ตํ•˜๋ฉฐ ๋ฌด๊ฒŒ๋ฅผ ๋น„๊ตํ•œ๋‹ค.

๋งŒ์•ฝ ๋‹ค๋ฆฌ์œ„ ํŠธ๋Ÿญ + ํ˜„์žฌ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋”ํ•ด๋„ ๋‹ค๋ฆฌ๊ฐ€ ๊ฒฌ๋”œ ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ๋ณด๋‹ค ์ž‘์€๊ฒฝ์šฐ(์ง€ํƒฑ๊ฐ€๋Šฅํ•œ๊ฒฝ์šฐ)

if(!waitTruck.isEmpty() && totalWeight + waitTruck.peek() <= weight)

 

ํŠธ๋Ÿญ์„ ์˜ฌ๋ฆฌ๊ณ , ํ˜„์žฌ ๋‹ค๋ฆฌ์œ„์— ์žˆ๋Š” ๋ชจ๋“  ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋”ํ•ด์ค€๋‹ค.

int nextTruck = waitTruck.poll();
totalWeight += nextTruck;

 

๊ทธ ํ›„ ๋ฆฌ์ŠคํŠธ์— ํ˜„์žฌ ํŠธ๋Ÿญ ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

workingTruck.add(new Truck(nextTruck, 1));

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€