Notice
Recent Posts
Recent Comments
-
[프로그래머스] 레벨 2 - 다리를 지나는 트럭 본문
https://programmers.co.kr/learn/courses/30/lessons/42583
최소한의 시간내에 트럭이 모두 지나가는데 소요되는 시간을 답으로 내라길래
그리디 문제인줄 알고 트럭의 무게를 내림차순 한 뒤 답을 구했다.
근데 그렇게 생각할것까지 없었고 그냥 주어진 순서대로 트럭을 보내 걸리는 시간을 구하는 문제였다.
아래 11번째줄만 주석처리하니 바로 정답이었다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <functional>
using namespace std;
int solution(int bridge_length, int weight, vector<int> truck_weights) {
int answer = 0;
int brid_weight = 0;
deque<int> weights;
deque<pair<int, int> > onbridge; // 무게, 경과시간
for(int i=0; i<truck_weights.size(); i++) weights.push_back(truck_weights[i]);
// weights에 무게 정보.
// 총 합 + 다음에 넣을
answer++;
// 다리 위 이동.
if(onbridge.size() > 0){
for(int i=0; i < onbridge.size(); i++){
onbridge[i].second++;
}
if(onbridge[0].second > bridge_length){
brid_weight -= onbridge[0].first;
onbridge.pop_front();
}
}
// 대기트럭이 있다면 다리로 이동.
if(weights.size() > 0){
if(weights[0] + brid_weight <= weight){
brid_weight += weights[0];
onbridge.push_back(make_pair(weights[0], 1));
weights.pop_front();
}
}
}
return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
'1-4. 프로그래머스' 카테고리의 다른 글
[프로그래머스] 레벨 2 - 가장 큰 수 (0) | 2020.02.04 |
---|---|
[프로그래머스] 레벨 2 - 주식가격 (0) | 2020.02.04 |
[프로그래머스] 레벨 2 - 기능개발 (0) | 2020.02.03 |
[프로그래머스] 레벨 2 - 프린터 (0) | 2020.02.03 |
[프로그래머스] 레벨 2 - 탑 (0) | 2020.02.03 |
Comments