티스토리 뷰
문제가 쉬워보이는데, 정작 큐로 넣고 나면 다리에 이미 여러 차량이 있었을 경우의 time 측정에서 복잡해졌다.
사실 사고가 복잡하지... 그걸 정리하면 코드는 정말 쉬워진다는 느낌을 받았다.
#include <string>
#include <vector>
#include <queue>
#include <numeric>
#include <iostream>
using namespace std;
int solution(int bridge_length, int weight, vector<int> truck_weights) {
int answer = 0;
int cur_count = 0;
int cur_weight = 0;
int index = 0;
queue<int> bridge;
int truck = 0;
while(true) {
if(index == truck_weights.size()) {
answer += bridge_length;
break;
}
answer++;
if(bridge.size() == bridge_length) { //다리가 꽉 참
cur_weight -= bridge.front();
bridge.pop();
}
if(cur_weight + truck_weights[index] <= weight) { //적재 가능 무게
cur_weight += truck_weights[index];
bridge.push(truck_weights[index]);
index++;
} else { //적재 불가능 무게
bridge.push(0);
}
}
return answer;
}
1초의 시간동안, 차가 다리에서 내림과 동시에 조건을 만족한다면 적재도 함께 이루어진다는 것을 잊으면 안된다.
이것때문에 골머리를 앓았던듯. ㅋㅋㅋ
'■ 알고리즘 > ◻ Programmers' 카테고리의 다른 글
[C++]전력망을 둘로 나누기 (0) | 2023.03.01 |
---|---|
[C++]주식 가격 (1) | 2023.01.06 |
[C++] 프린터 (0) | 2022.11.26 |
[C++] 더 맵게 (0) | 2022.11.23 |
[C++]게임 맵 최단거리 (0) | 2022.11.21 |