Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 제어반전
- Algorithm
- goland
- 두 동전
- 안드로이드
- insert
- broadcastreceiver
- IntelliJ
- 알고리즘
- activity
- data
- 백준
- 16197
- intent
- 17837
- Android
- ubuntu
- 단축키
- 데이터
- Java
- github
- spring
- 프로그래머스
- Jenknis
- 큐빙
- mysql
- git
- vscode
- service
- 데이터전달
Archives
- Today
- Total
해보자
프로그래머스_LV2_다리를 지나는 트럭 본문
https://programmers.co.kr/learn/courses/30/lessons/42583
코딩테스트 연습 - 다리를 지나는 트럭 | 프로그래머스
트럭 여러 대가 강을 가로지르는 일 차선 다리를 정해진 순으로 건너려 합니다. 모든 트럭이 다리를 건너려면 최소 몇 초가 걸리는지 알아내야 합니다. 트럭은 1초에 1만큼 움직이며, 다리 길이는 bridge_length이고 다리는 무게 weight까지 견딥니다. ※ 트럭이 다리에 완전히 오르지 않은 경우, 이 트럭의 무게는 고려하지 않습니다. 예를 들어, 길이가 2이고 10kg 무게를 견디는 다리가 있습니다. 무게가 [7, 4, 5, 6]kg인 트럭이 순서
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 | #include <iostream> #include <string> #include <vector> #include <deque> using namespace std; int solution(int bridge_length, int weight, vector<int> truck_weights) { int answer = 1; int cur_weight = 0; deque<pair<int, int>> truck_on_bridge; int i = 0; while (!truck_on_bridge.empty() || i < truck_weights.size()) { if (i < truck_weights.size()) { if (cur_weight + truck_weights[i] <= weight) { truck_on_bridge.push_back(make_pair(truck_weights[i], 0)); cur_weight += truck_weights[i]; i++; } } answer++; int pop_count = 0; for (int j = 0; j < truck_on_bridge.size(); j++) { truck_on_bridge[j].second++; if (truck_on_bridge[j].second == bridge_length) { cur_weight = cur_weight - truck_on_bridge[j].first; pop_count++; } } while(pop_count-- > 0) truck_on_bridge.pop_front(); } return answer; } | cs |
'C++ > Solve & Think' 카테고리의 다른 글
| 백준_15686번_치킨 배달 (0) | 2020.10.08 |
|---|---|
| 백준 14503번 로봇 청소기 (0) | 2020.04.19 |
| 프로그래머스_LV2_기능개발 (0) | 2020.02.21 |
| 백준_7576번_토마토 (0) | 2020.02.20 |
| 프로그래머스_LV2_프린터 (0) | 2020.02.19 |