반응형
https://programmers.co.kr/learn/courses/30/lessons/42583
글쓴이의 답
개인적인 풀이 임으로
이것보다 더 좋은 알고리즘은 많음...
이렇게도 풀이하는구나.. 공유하기 위해 올림...
#include <string>
#include <vector>
#include <deque>
#include <iostream>
using namespace std;
int solution(int bridge_length, int weight, vector<int> truck_weights) {
int answer = 0;
deque<int> bridge;
size_t resultSize = truck_weights.size();
vector<int> result;
for(int i=0; i<bridge_length; i++){
bridge.push_back(0);
}
int bridgeWeights = 0;
int truckCount=0;
while(result.size() != resultSize){
if(0 != bridge[0]){
result.push_back(bridge[0]);
bridgeWeights -= bridge[0];
}
bridge.pop_front();
bridge.push_back(0);
if(bridgeWeights < weight){
if(bridgeWeights+ truck_weights[truckCount] <= weight ){
if(truck_weights.size() > truckCount){
bridge[bridge_length-1]=truck_weights[truckCount];
bridgeWeights += bridge[bridge_length-1];
truckCount++;
}
}
}
answer++;
}
return answer;
}
꾸준히 하다보면 실력이 늘겠지..
반응형
'코딩테스트' 카테고리의 다른 글
[프로그래머스][C++] 카펫 (0) | 2022.04.15 |
---|---|
[프로그래머스][C++] H-Index (0) | 2022.03.25 |
[프로그래머스][C++] 위장 (0) | 2022.03.25 |
[프로그래머스][C++] 배달 (0) | 2022.03.25 |
[프로그래머스][C++] 괄호 회전하기 (0) | 2022.03.25 |