https://programmers.co.kr/learn/courses/30/lessons/42583
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
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
41
42
43
44
|
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
int solution(int bridge_length, int weight, vector<int> truck_weights) {
int answer = 0; //걸린시간
queue<pair<int, int>> on_bridge; //다리 위 트럭
queue<int> cp_tw;
int total_weights = 0; // 현재 다리 위 트럭의 총 무게
int cnt = 0; //건너간 트럭의 수
for (int i = 0; i < truck_weights.size(); i++) {
cp_tw.push(truck_weights[i]);
}
//트럭이 다 건너갈 때까지 반복
while (cnt != truck_weights.size()) {
if (on_bridge.size() != 0) {
for (int i = 0; i < on_bridge.size(); i++) {
pair<int, int> tmp(on_bridge.front().first + 1, on_bridge.front().second);
on_bridge.pop();
on_bridge.push(tmp);
}
if (on_bridge.front().first == bridge_length) {
total_weights -= on_bridge.front().second;
on_bridge.pop();
cnt++;
}
}
if (!cp_tw.empty()) {
if (total_weights + cp_tw.front() <= weight) {
total_weights += cp_tw.front();
on_bridge.push(make_pair(0, cp_tw.front()));
cp_tw.pop();
}
}
answer++;
}
return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'알고리즘(C++) > 프로그래머스 level2' 카테고리의 다른 글
쇠막대기 (0) | 2020.04.04 |
---|---|
탑 (0) | 2020.04.04 |
스킬트리 (0) | 2020.04.04 |
124 나라의 숫자 (0) | 2020.04.04 |
프린터 (0) | 2020.04.04 |