Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค / C++] ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ ํ’€์ด

meeeeejin 2020. 8. 14. 23:43

๋ฌธ์ œ

 

<๋ฌธ์ œ ์„ค๋ช…>

 

ํŠธ๋Ÿญ ์—ฌ๋Ÿฌ ๋Œ€๊ฐ€ ๊ฐ•์„ ๊ฐ€๋กœ์ง€๋ฅด๋Š” ์ผ ์ฐจ์„  ๋‹ค๋ฆฌ๋ฅผ ์ •ํ•ด์ง„ ์ˆœ์œผ๋กœ ๊ฑด๋„ˆ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋ ค๋ฉด ์ตœ์†Œ ๋ช‡ ์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๋Š”์ง€ ์•Œ์•„๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋Ÿญ์€ 1์ดˆ์— 1๋งŒํผ ์›€์ง์ด๋ฉฐ, ๋‹ค๋ฆฌ ๊ธธ์ด๋Š” bridge_length์ด๊ณ  ๋‹ค๋ฆฌ๋Š” ๋ฌด๊ฒŒ weight๊นŒ์ง€ ๊ฒฌ๋”ฅ๋‹ˆ๋‹ค.

โ€ป ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ์— ์™„์ „ํžˆ ์˜ค๋ฅด์ง€ ์•Š์€ ๊ฒฝ์šฐ, ์ด ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, ๊ธธ์ด๊ฐ€ 2์ด๊ณ  10kg ๋ฌด๊ฒŒ๋ฅผ ๊ฒฌ๋””๋Š” ๋‹ค๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌด๊ฒŒ๊ฐ€ [7, 4, 5, 6] kg์ธ ํŠธ๋Ÿญ์ด ์ˆœ์„œ๋Œ€๋กœ ์ตœ๋‹จ ์‹œ๊ฐ„ ์•ˆ์— ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฑด๋„ˆ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๊ฒฝ๊ณผ ์‹œ๊ฐ„ ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚œ ํŠธ๋Ÿญ ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋Š” ํŠธ๋Ÿญ ๋Œ€๊ธฐ ํŠธ๋Ÿญ
0 [] [] [7,4,5,6]
1~2 [] [7] [4,5,6]
3 [7] [4] [5,6]
4 [7] [4,5] [6]
5 [7,4] [5] [6]
6~7 [7,4,5] [6] []
8 [7,4,5,6] [] []

๋”ฐ๋ผ์„œ, ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋ ค๋ฉด ์ตœ์†Œ 8์ดˆ๊ฐ€ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.

 

solution ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋‹ค๋ฆฌ ๊ธธ์ด bridge_length, ๋‹ค๋ฆฌ๊ฐ€ ๊ฒฌ๋”œ ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ weight, ํŠธ๋Ÿญ ๋ณ„ ๋ฌด๊ฒŒ truck_weights๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋ ค๋ฉด ์ตœ์†Œ ๋ช‡ ์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

 

 

 

<์ œํ•œ ์กฐ๊ฑด>

  • bridge_length๋Š” 1 ์ด์ƒ 10,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • weight๋Š” 1 ์ด์ƒ 10,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • truck_weights์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 10,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋Š” 1 ์ด์ƒ weight ์ดํ•˜์ž…๋‹ˆ๋‹ค.

 

 

<์ž…์ถœ๋ ฅ ์˜ˆ>


bridge_length weight truck_weights return
2 10 [7,4,5,6] 8
100 100 [10] 101
100 100 [10,10,10,10,10,10,10,10,10,10] 110

 

 

https://programmers.co.kr/learn/courses/30/lessons/42583

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ

ํŠธ๋Ÿญ ์—ฌ๋Ÿฌ ๋Œ€๊ฐ€ ๊ฐ•์„ ๊ฐ€๋กœ์ง€๋ฅด๋Š” ์ผ ์ฐจ์„  ๋‹ค๋ฆฌ๋ฅผ ์ •ํ•ด์ง„ ์ˆœ์œผ๋กœ ๊ฑด๋„ˆ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ํŠธ๋Ÿญ์ด ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ˆ๋ ค๋ฉด ์ตœ์†Œ ๋ช‡ ์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๋Š”์ง€ ์•Œ์•„๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋Ÿญ์€ 1์ดˆ์— 1๋งŒํผ ์›€์ง์ด๋ฉฐ, ๋‹ค๋ฆฌ ๊ธธ์ด๏ฟฝ๏ฟฝ

programmers.co.kr

 

 

 

 

๋ฌธ์ œ ํ’€์ด

 

๋‹ค๋ฆฌ ์œ„์— ์žˆ๋Š” ํŠธ๋Ÿญ์„ on_bridge๋ผ๋Š” ํ์— ์ €์žฅํ•ด์„œ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ €์žฅํ•˜๋Š” ๊ฐ’์€ ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋Œ€๊ธฐํ•˜๊ณ  ์žˆ๋Š” ํŠธ๋Ÿญ ์ค‘ ๊ฐ€์žฅ ์•ž์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” next_truck๊ณผ ๋‹ค๋ฆฌ ์œ„ ํŠธ๋Ÿญ๋“ค์˜ ๋ฌด๊ฒŒ ์ดํ•ฉ์„ ์ €์žฅํ•˜๋Š” weight_sum ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. 

 

์˜ˆ๋ฅผ ๋“ค์–ด bridge_length = 2, weight = 10, truck_weights = {7, 4, 5, 6}์ด ์ฃผ์–ด์กŒ์„ ๋•Œ answer์— ๋”ฐ๋ผ on_bridge์˜ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๋€๋‹ˆ๋‹ค. 

 

answer on_bridge truck_weights[next_truck] weight_sum
0 [0, 0] 7 0
1 [0, 7] 4 7
2 [7, 0] 4 7
3 [0, 4] 5 4
4 [4, 5] 6 9
5 [5, 0] 6 5
6 [0, 6]   6
7 [6]   6
8 []   0

 

on_bridge์˜ ์›์†Œ๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ ๋‹ค์Œ์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. 

 

1. answer์— 1์„ ๋”ํ•˜๊ณ  on_bridge์˜ ๋งจ ์•ž ์›์†Œ๋ฅผ popํ•˜๋ฉฐ weight_sum์—์„œ ๋ฌด๊ฒŒ๋ฅผ ๋นผ์คŒ

2. ๋Œ€๊ธฐ ํŠธ๋Ÿญ์ด ๋‚จ์•„ ์žˆ๋Š” ๊ฒฝ์šฐ

   1) ๋‹ค๋ฆฌ ์œ„์— ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ

       on_bridge์— ํ•ด๋‹น ํŠธ๋Ÿญ ๋ฌด๊ฒŒ๋ฅผ pushํ•˜๊ณ  weight_sum์— ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋”ํ•จ

   2) ๋‹ค๋ฆฌ ์œ„์— ๋ชป์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒฝ์šฐ

       on_bridge์— 0์„ push ํ•จ

 

 

 

 

์†Œ์Šค์ฝ”๋“œ

 

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
// ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ
#include <string>
#include <vector>
#include <queue>
 
using namespace std;
 
int solution(int bridge_length, int weight, vector<int> truck_weights) {
    int answer = 0;
    int next_truck = 0;     // ๋Œ€๊ธฐ ํŠธ๋Ÿญ ์ค‘ ๋งจ ์•ž์˜ ํŠธ๋Ÿญ์˜ ์ธ๋ฑ์Šค
    int weight_sum = 0;     // ๋‹ค๋ฆฌ ์œ„์— ์žˆ๋Š” ํŠธ๋Ÿญ์˜ ๋ฌด๊ฒŒ ํ•ฉ
    queue<int> on_bridge;   // ๋‹ค๋ฆฌ ์œ„์— ์žˆ๋Š” ํŠธ๋Ÿญ
 
    for (int i = 0; i < bridge_length; i++)
        on_bridge.push(0);
 
    while (!on_bridge.empty()) {
        answer++;
        weight_sum -= on_bridge.front();
        on_bridge.pop();
 
        // ๋Œ€๊ธฐ ํŠธ๋Ÿญ์ด ์žˆ๋Š” ๊ฒฝ์šฐ
        if (next_truck < truck_weights.size()) {
            if (weight_sum + truck_weights[next_truck] <= weight) {
                weight_sum += truck_weights[next_truck];
                on_bridge.push(truck_weights[next_truck]);
                next_truck++;
            }
            else {
                on_bridge.push(0);
            }
        }
        
    }
 
    return answer;
}
cs

 

 

์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ

 

 

 

 

๊ณต๋ถ€ํ•œ ๊ฒƒ์„ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ˆ˜์ •ํ•  ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค :)

 

 

728x90