다리를 지나는 트럭

고장난·2021년 5월 16일
0

코딩 스터디

목록 보기
3/15
#include <string>
#include <vector>
#include <queue>
using namespace std;

int solution(int bridge_length, int weight, vector<int> truck_weights) {
    int answer = 0;
    int current_weight=0;
    
    queue<int> bridge;
    
    
    for(int i=0;i<bridge_length;i++){
        bridge.push(0);
        }

    for(int i=0;i<truck_weights.size();){
        if(truck_weights.size()-1==i){
            if(current_weight+truck_weights[i]-bridge.front()<=weight){
                bridge.push(truck_weights[i]);
                current_weight=current_weight+truck_weights[i]-bridge.front();
                bridge.pop();
                i++;            
                answer=answer+bridge_length+1;
            }
            else{
                bridge.push(0);
                current_weight=current_weight-bridge.front();
                bridge.pop();
                answer++;       
            }
        }
        else{
            if(current_weight+truck_weights[i]-bridge.front()<=weight){
                bridge.push(truck_weights[i]);
                current_weight=current_weight+truck_weights[i]-bridge.front();
                bridge.pop();
                i++;
                answer++;
            }
            else{
                bridge.push(0);
                current_weight=current_weight-bridge.front();
                bridge.pop();
                answer++;            
        }

        }
    }
        
    
    return answer;
}

정확성 테스트
테스트 1 〉 통과 (0.02ms, 3.94MB)
테스트 2 〉 통과 (0.13ms, 3.95MB)
테스트 3 〉 통과 (0.01ms, 3.99MB)
테스트 4 〉 통과 (0.10ms, 3.95MB)
테스트 5 〉 통과 (0.94ms, 3.96MB)
테스트 6 〉 통과 (0.28ms, 3.89MB)
테스트 7 〉 통과 (0.01ms, 3.91MB)
테스트 8 〉 통과 (0.01ms, 3.96MB)
테스트 9 〉 통과 (0.05ms, 3.95MB)
테스트 10 〉 통과 (0.01ms, 3.95MB)
테스트 11 〉 통과 (0.01ms, 3.96MB)
테스트 12 〉 통과 (0.01ms, 3.95MB)
테스트 13 〉 통과 (0.02ms, 3.77MB)
테스트 14 〉 통과 (0.01ms, 3.73MB)
채점 결과
정확성: 100.0
합계: 100.0 / 100.0

profile
훈련중

0개의 댓글

관련 채용 정보