[Programmers] Lv2. ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ | C++

2023. 6. 25. 21:34ใ†Coding Test/Programmers

๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

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

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

programmers.co.kr

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

ํ(Queue)๋ฅผ ํ†ตํ•ด ๋‹ค๋ฆฌ ์œ„์— ์žˆ๋Š” ํŠธ๋Ÿญ๋“ค์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋Ÿญ์ด ๊ฐ€์ง€๋Š” ์ •๋ณด๋Š” ๋‹ค๋ฆฌ ์ง„์ž… ์‹œ๊ฐ(enterTime)๊ณผ ํŠธ๋Ÿญ ๋ฌด๊ฒŒ(weight)์ž…๋‹ˆ๋‹ค.

struct Truck
{
	Truck(int enterTime, int weight) : _enterTime(enterTime), _weight(weight) { }
	int _enterTime;
	int _weight;
};

 

๊ฒฝ๊ณผ ์‹œ๊ฐ„(elapsedTime), ๋‹ค๋ฆฌ ์œ„ ํŠธ๋Ÿญ๋“ค์˜ ์ด ๋ฌด๊ฒŒ ํ•ฉ(weightSum)๋„ ๊ฐ™์ด ๊ด€๋ฆฌํ•˜๋ฉฐ, ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ ํŠธ๋Ÿญ ๊ด€๋ฆฌ์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.

int elapsedTime = 1;
int weightSum = 0;
queue<Truck> trucksOnBridge;

 

๊ธฐ๋ณธ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ๋ฆ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๋‹ค๋ฆฌ ์œ„์— ํŠธ๋Ÿญ์ด ์—†๊ฑฐ๋‚˜, ๋‹ค๋ฆฌ ์œ„์— ์ƒˆ๋กœ ํŠธ๋Ÿญ์„ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์ƒˆ๋กœ์šด ํŠธ๋Ÿญ์„ ๋‹ค๋ฆฌ ์œ„์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค.
    • ์ด ๋•Œ, ์ง„์ž… ์‹œ๊ฐ์„ ์ €์žฅํ•ด ๋†“์Šต๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ๋„์ฐฉ ์‹œ๊ฐ„ ๊ณ„์‚ฐํ•  ๋•Œ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

 

 

๊ฒฝ๊ณผ ์‹œ๊ฐ„์„ 1์ดˆ์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ๋Œ๋ฆฌ๊ธฐ์—๋Š” ๋„ˆ๋ฌด ๋น„ํšจ์œจ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ํŠธ๋Ÿญ์˜ ๋„์ฐฉ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•  ์ˆ˜๋งŒ ์žˆ๋‹ค๋ฉด, 1์ดˆ์”ฉ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ๋Œ๋ ค๋„ ๋˜์ง€ ์•Š์œผ๋‹ˆ๊นŒ์š”. ๊ทธ๋ž˜์„œ ์ €๋Š” ํŠธ๋Ÿญ์˜ ๋„์ฐฉ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํŠธ๋Ÿญ์˜ ๋„์ฐฉ ์‹œ๊ฐ„ ๊ณต์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

"๋„์ฐฉ ์‹œ๊ฐ„" = "๋‹ค๋ฆฌ ์ง„์ž… ์‹œ๊ฐ" + "๋‹ค๋ฆฌ์˜ ๊ธธ์ด"

 

์ด ๊ณ„์‚ฐ์„ ์œ„ํ•ด ์ง„์ž… ์‹œ๊ฐ์„ ์ ์–ด ๋†“์€ ์ฑ„, ๋‹ค๋ฆฌ์— ์ง„์ž…์‹œํ‚จ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฆฌ์— ์ถ”๊ฐ€๋กœ ํŠธ๋Ÿญ์„ ๋” ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๊ณ„์† ๋„ฃ์–ด์ฃผ์„ธ์š”.

 

๋งŒ์•ฝ ๋” ์˜ฌ๋ฆด ์ˆ˜ ์—†๋‹ค๋ฉด, ๋‹ค๋ฆฌ ์œ„ ๋งจ ์•ž ํŠธ๋Ÿญ์„ ๋นผ๋‚ด์–ด ๋„์ฐฉ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜์—ฌ ๊ทธ ์‹œ๊ฐ„์œผ๋กœ ๊ฒฝ๊ณผ ์‹œ๊ฐ„์„ ์ดˆ๊ธฐํ™” ์‹œ์ผœ ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ๋‹ค๋ฆฌ ๊ธธ์ด๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด ๊ณ„์‚ฐํ•œ ๋„์ฐฉ ์‹œ๊ฐ„๊ณผ ํ˜„์žฌ ๊ฒฝ๊ณผ ์‹œ๊ฐ„์ด ์•ˆ ๋งž์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋‘˜ ์ค‘ ํฐ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ค๋‹ˆ๋‹ค.

 

๋‹ค๋ฆฌ ์œ„ ๋งจ ์•ž ํŠธ๋Ÿญ์„ ๋นผ๋‚ธ ํ›„์— ๋ฐ”๋กœ ๋‹ค์Œ ํŠธ๋Ÿญ์„ ์˜ฌ๋ฆฌ๋ฉด ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ ํ›„, ์˜ฌ๋ฆด ์ˆ˜ ์—†๋‹ค๋ฉด ๋‹ค์‹œ ๋งจ ์•ž ํŠธ๋Ÿญ์„ ๋นผ๋‚ด์–ด ๊ณ„์‚ฐ ํ›„ ๊ฐฑ์‹ ํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๊ณ„์‚ฐ ๊ณผ์ •์„ ๋ฌด์‚ฌํžˆ ๋งˆ์ณค๋‹ค๋ฉด, ํ˜„์žฌ ํŠธ๋Ÿญ์„ ๋‹ค๋ฆฌ์— ์˜ฌ๋ฆฌ๋ฉด ๋˜๊ฒ ์ง€์š”.

๋ชจ๋“  ํŠธ๋Ÿญ์„ ๋‹ค๋ฆฌ์— ์˜ฌ๋ ธ๋‹ค๋ฉด, ๋‹ค๋ฆฌ ์œ„์— ํŠธ๋Ÿญ์ด ์—†์„ ๋•Œ๊นŒ์ง€ ๋„์ฐฉ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜์—ฌ ๊ฐฑ์‹ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <vector>
#include <queue>
using namespace std;

struct Truck
{
	Truck(int enterTime, int weight) : _enterTime(enterTime), _weight(weight) { }
	int _enterTime;
	int _weight;
};

int solution(int bridge_length, int weight, vector<int> truck_weights) {
	int elapsedTime = 1;
	int weightSum = 0;
	queue<Truck> trucksOnBridge;

	for (int i = 0; i < truck_weights.size(); i++) {
		int truckWeight = truck_weights[i];

        // #1. ๋‹ค๋ฆฌ ์œ„์— ํŠธ๋Ÿญ์ด ์—†๊ฑฐ๋‚˜, ํŠธ๋Ÿญ์„ ๋” ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์œผ๋ฉด ์˜ฌ๋ฆฌ๊ธฐ
		if ((trucksOnBridge.empty()) or (weightSum + truckWeight <= weight)) {
			trucksOnBridge.emplace(elapsedTime++, truckWeight);
			weightSum += truckWeight;
			continue;
		}

        // #2. ๋‹ค๋ฆฌ ์œ„์˜ ๋งจ ์•ž ํŠธ๋Ÿญ์„ ๋นผ๋‚ด์–ด ๋„์ฐฉ ์‹œ๊ฐ„ ๊ณ„์‚ฐ ํ›„, ์ •๋ณด ๊ฐฑ์‹ ํ•˜๊ธฐ
		Truck frontTruck = trucksOnBridge.front();
		trucksOnBridge.pop();
		elapsedTime = max(frontTruck._enterTime + bridge_length, elapsedTime);
		weightSum -= frontTruck._weight;

        
        // #3. ๋‹ค๋ฆฌ์— ๋‹ค์Œ ํŠธ๋Ÿญ์„ ์‹ค์—ˆ์„ ๋•Œ ์ตœ๋Œ€ ํ•˜์ค‘์„ ๋„˜์œผ๋ฉด, ํ˜„์žฌ ๋ฃจํ”„๋ฅผ ๋‹ค์‹œ ๋ฐ˜๋ณตํ•˜์—ฌ #2๋ฒˆ ๋กœ์ง ์‹คํ–‰
		bool isOverWeight = weight < (weightSum + truckWeight);
		if (isOverWeight) {
			i -= 1;
			continue;
		}

        // #4. ๋‹ค๋ฆฌ ์œ„ ๋งจ ์•ž ํŠธ๋Ÿญ์„ ๋„์ฐฉ ์ง€์ ์— ๋ณด๋ƒˆ๋‹ค๋ฉด, ์ƒˆ๋กœ ์‹ค์„ ํŠธ๋Ÿญ์„ ๋‹ค๋ฆฌ ์œ„์— ์˜ฌ๋ฆฌ๊ธฐ
		trucksOnBridge.emplace(elapsedTime, truckWeight);
		weightSum += truckWeight;
		elapsedTime++;
	}

        // #5. ๋‹ค๋ฆฌ ์œ„์— ๋‚จ์•„ ์žˆ๋Š” ํŠธ๋Ÿญ๋“ค์˜ ๋„์ฐฉ ์‹œ๊ฐ„ ๊ณ„์‚ฐํ•˜๊ธฐ
	while (!trucksOnBridge.empty()) {
		Truck truck = trucksOnBridge.front();
		trucksOnBridge.pop();
		elapsedTime = truck._enterTime + bridge_length;
	}

	return elapsedTime;
}

 

 

 

728x90
๋ฐ˜์‘ํ˜•