Skip to content

Commit

Permalink
[level 2] Title: 다리를 지나는 트럭, Time: 0.01 ms, Memory: 4.28 MB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
developeSHG committed Jul 23, 2023
1 parent 5e7e9b3 commit 3e321f7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

### 성능 요약

메모리: 33.5 MB, 시간: 0.05 ms
메모리: 4.28 MB, 시간: 0.01 ms

### 구분

코딩테스트 연습 > 스택/큐

### 채점결과

<br/>정확성: 100.0<br/>합계: 100.0 / 100.0
Empty

### 문제 설명

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <queue>
#include <vector>

using namespace std;

int solution(int bridge_length, int weight, vector<int> truck_weights) {
int answer = 0;

int idx=0; //차량 지목용 idx
int sum=0; //현재 다리에 올라와있는 차량 무게 총합
queue<int> q; //현재 다리를 건너는 트럭 체크용 큐

while(1){

if(idx == truck_weights.size()){ //마지막 트럭일 때
answer += bridge_length; //마지막 트럭이 지나는 시간 추가
break;
}

answer++; //시간초 증가
int tmp = truck_weights[idx];

//차가 다리를 다 건넜을 경우
if(q.size() == bridge_length){
sum -= q.front(); //다 건넜으니 현재 다리에 있는 차들의 무게에서 제외
q.pop();
}

if(sum + tmp <= weight){ //다리에 다음 차가 진입할 수 있다면
sum += tmp; //차량 무게 추가
q.push(tmp);
idx++; //다음 차량을 위해서
}else{
q.push(0); //진입할 수 없다면 0을 푸시해서 시간초 계산
}
}

return answer;
}

0 comments on commit 3e321f7

Please sign in to comment.