배움 저장소
[프로그래머스] 다리를 지나는 트럭 C++ 본문
programmers.co.kr/learn/courses/30/lessons/42583
자료구조 공부할 때 배웠던 circular queue가 생각나 직접 구현하고 싶었다.
사이즈가 꽉 차면 자동적으로 처음 들어온 자료를 지우는 형태로 enqueue를 구현하였다.
코드를 짤 때 이미 들어가있는 자료를 지우면 weight를 초과하는지 아닌지를 체크하지 않아 고생했다.
질문하기 탭에서 Edge케이스를 해결할 수 있는 예제를 실행해보고서 위와 같은 문제가 생김을 알고 고쳤다.
#include <string>
#include <queue>
#include <vector>
using namespace std;
struct que{
int front,len;
vector<int> bridge;
int sum,max_sum;
que(int lengh, int weight){
front=0,sum=0;
len = lengh;
max_sum = weight;
bridge = vector<int>(len,0);
}
void enqueue(int truck){
if(front==len || bridge[front]!=0 ){
front%=len;
sum -= bridge[front];
}
bridge[front++] = truck;
sum+=truck;
}
};
int solution(int bridge_length, int weight, vector<int> truck) {
que q(bridge_length, weight);
int time=0;
for(int i=0; i<truck.size(); ++i){
while(true){
time++;
if(q.sum-q.bridge[q.front%q.len]+truck[i]<=weight) break;
else q.enqueue(0);
}
q.enqueue(truck[i]);
if(i==truck.size()-1)
return time+bridge_length;
}
return -1;
}
'PS > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 체육복 C++ (0) | 2021.04.10 |
---|---|
[프로그래머스] 이중우선순위큐 C++ (0) | 2021.04.10 |
[프로그래머스] 디스크 컨트롤러 C++ (0) | 2021.04.10 |
[프로그래머스] 더 맵게 C++ (0) | 2021.04.10 |
[프로그래머스] 기능개발 C++ (0) | 2021.04.10 |
Comments