본문 바로가기

problem solving

[프로그래머스]다리를 지나는 트럭

처음에 무작정 풀려하다가 삽질만 한 거 같다.

결론적으로 큐를 이용해서 풀면 쉽게 풀 수 있는데

딱히 어려운 부분은 없다.

#include <string>
#include <vector>
#include <queue>
#include <algorithm>
 
using namespace std;
 
int solution(int bridge_length, int weight, vector<int> truck_weights) {
    int answer = 0;
    
    queue<std::pair<intint>> mQ;
    int seq = 0;
    int time = 1;
    int total_weights = 0;
    mQ.push({seq, time});
    total_weights += truck_weights[seq++];
    while(!mQ.empty()) {
        if(time - mQ.front().second + 1 >= bridge_length) {
            total_weights -= truck_weights[mQ.front().first];
            mQ.pop();
        }
        time++;
        if(total_weights + truck_weights[seq] <= weight && seq < truck_weights.size()) {
            mQ.push({seq, time});
            total_weights += truck_weights[seq++]; 
        }
        
    }
    answer = time;
    return answer;
}
cs

'problem solving' 카테고리의 다른 글

[프로그래머스]완주하지 못한 선수  (0) 2020.03.06
[프로그래머스]124나라의 숫자  (0) 2020.03.06
[프로그래머스]멀쩡한 사각형  (0) 2020.03.05
2186번: 문자판  (0) 2020.03.05
2133번: 타일 채우기  (0) 2020.03.05