Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2-H0ngJu #153

Merged
merged 1 commit into from
Mar 25, 2024
Merged

2-H0ngJu #153

merged 1 commit into from
Mar 25, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Mar 7, 2024

πŸ”— 문제 링크

닀리λ₯Ό μ§€λ‚˜λŠ” 트럭

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

2μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

문제의 쑰건은 κ°„λ‹¨ν•©λ‹ˆλ‹€.

  1. 닀리에 μ˜¬λΌκ°„ 트럭의 무게 <= weight
  2. 닀리에 μ˜¬λΌκ°„ 트럭의 수 <= bridge_length

μ €λŠ” 이 문제의 μΌ€μ΄μŠ€λ„ 1. 트럭이 닀리λ₯Ό μ§€λ‚˜κ°€λŠ” κ²½μš°μ™€ 2. μ˜¬λΌκ°€λŠ” 경우둜 2κ°€μ§€μ˜ 경우둜 λ‚˜λˆ μ„œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

  1. μ§€λ‚˜κ°€λŠ” 경우
  • onBridgeκ°€ λΉ„μ–΄ μžˆμ§€ μ•Šκ³  (흐λ₯Έμ‹œκ°„)-(트럭이 닀리 μœ„μ— μžˆμ—ˆλ˜ μ‹œκ°„)이 bridge_length보닀 ν¬κ±°λ‚˜ μž‘μ€ 경우
    => ν•΄λ‹Ή νŠΈλŸ­μ„ dequeμ—μ„œ 제거 & λ¬΄κ²Œλ„ 제거
  1. μ˜¬λΌκ°€λŠ” 경우
  • truck_weightsκ°€ λΉ„μ–΄ μžˆμ§€ μ•Šκ³  μ•žμ„œ μ–ΈκΈ‰ν–ˆλ˜ 2가지 쑰건을 λ§Œμ‘±ν•˜λŠ” 경우
    => truck_weightsμ—μ„œ popν•œ ν›„, onBridge, sum에 각각 μΆ”κ°€ & 닀리 μœ„μ˜ 트럭 수 증가

λ‹€λ§Œ μ €λŠ” μ‹œκ°„μ˜ 흐름 처리λ₯Ό μ–΄λ–»κ²Œ ν•΄μ•Ό 할지 λͺ°λΌ 쑰금 λ‚œν•­μ„ κ²ͺμ—ˆμŠ΅λ‹ˆλ‹€.

μ§€λ‚˜κ°€λŠ” 경우, λ‚΄λ¦¬λŠ” κ²½μš°μ— λŒ€ν•΄μ„œ μ‹œκ°„ 처리λ₯Ό 각각 ν•˜λ‹€λ³΄λ‹ˆ κ·Έλƒ₯ while 문을 λŒλ©΄μ„œ time은 계속 μ¦κ°€ν•˜λ„λ‘ ν•˜λ©΄ λ˜κ² κ΅¬λ‚˜ (=트럭이 μ˜¬λΌκ°€κ³  λ‚΄λ €κ°€λŠ” κ²½μš°λŠ” if문으둜 검사λ₯Όν•˜λ‹ˆ) ν•΄μ„œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

배운 λ‚΄μš©λ“€λ‘œ μ΅œλŒ€ν•œ 제 힘으둜 ν’€λ €κ³  ν–ˆλŠ”λ° 생각보닀 문제λ₯Ό ν‘ΈλŠ”λ° μ‹œκ°„μ΄ μ˜€λž˜κ±Έλ Έμ§€λ§Œ λΏŒλ“―ν•˜λ„€μš” πŸ”₯

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

  • dequeμ—μ„œ apped()와 extend()의 차이
    append()λŠ” 리슀트의 끝에 μ›μ†Œ 1개λ₯Ό μΆ”κ°€ν•˜λŠ” 것이고, extend()λŠ” 리슀트의 끝에 λͺ¨λ“  μš”μ†Œλ₯Ό μ‚½μž… κ°€λŠ₯ν•˜λ‹€.
    extend([A, B, C])λ₯Ό ν•˜λ©΄ 리슀트의 끝에 A, B, Cκ°€ 각각의 μ›μ†Œλ‘œμ„œ μΆ”κ°€λœλ‹€

@H0ngJu H0ngJu self-assigned this Mar 7, 2024
@H0ngJu H0ngJu requested review from pknujsp, Munbin-Lee and tgyuuAn March 7, 2024 15:28
@H0ngJu H0ngJu changed the title 2024-03-07 2-H0ngJu Mar 7, 2024
@pknujsp pknujsp changed the title 2-H0ngJu 2-H0ngJu Mar 7, 2024
Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ € μ•„μ£Ό μ˜›λ‚ μ— ν’€μ—ˆμ—ˆλŠ”λ°

μ§€κΈˆ λ‹€μ‹œ μ œκ°€ μ§  μ½”λ“œλ₯Ό λ³΄λ‹ˆ λ„ˆλ¬΄ ν˜•νŽΈμ—†λ„€μš”...

ν™μ£Όλ‹˜μ΄ μ§œμ‹  μ½”λ“œκ°€ 더 κΉ”λ”ν•˜κ³  직관적이고 보기 μ’‹μ•„μš”.

from collections import deque

def solution(bridge_length, weight, truck_weights):
    truck = deque(truck_weights)
    time = 1
    bridge_weight = 0
    dict = {}

    while True:
        time += 1
        if truck:
            if bridge_weight + truck[0] <= weight:
                temp = truck.popleft()
                dict[time] = [temp,0]
                bridge_weight += temp

        keys = list(dict.keys())
        for i in keys:
            dict[i][1] += 1
            if dict[i][1] == bridge_length:
                bridge_weight -= dict.pop(i)[0]

        if bridge_weight == 0 and not truck:
            break

    return time

λ‹€μ‹œλ³΄λ‹ˆ 이 문제 큐 μ•Œκ³ λ¦¬μ¦˜μ΄κΈ΄ ν•œλ° μŠ¬λΌμ΄λ”© μœˆλ„μš°λž‘ 거의 μœ μ‚¬ν•˜λ„€μš”.

μŠ¬λΌμ΄λ”© μœˆλ„μš°λ„ 큐 처럼 μ•žμ—μ„œ λΉ μ Έλ‚˜κ°€λ‹ˆ..




ν˜Ήμ‹œ λ„€νŠΈμ›Œν¬λ‚˜ λ°μ΄ν„°ν†΅μ‹ μ—μ„œ 버퍼 관리할 λ•Œ μŠ¬λΌμ΄λ”© μœˆλ„μš° λ°°μš°μ…¨λ‚˜μš”?!

def solution(bridge_length, weight, truck_weights):
time = 0
onBridge = deque() # ν˜„μž¬ 닀리 μœ„μ— μžˆλŠ” 트럭
truckNum = 0 # ν˜„μž¬ 닀리 μœ„μ— μžˆλŠ” 트럭 수
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν˜Ήμ‹œ truckNum이 μ½”λ“œμ—μ„œ 무슨 역할을 ν•˜κ³  μžˆλŠ” 지 μ•Œ 수 μžˆμ„κΉŒμš”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

truckNum은 λ‹€λ¦¬μœ„μ— μžˆλŠ” 트럭 수λ₯Ό μ €μž₯ν•˜λŠ” λ³€μˆ˜μž…λ‹ˆλ‹Ή

κ·Έλž˜μ„œ μ•„λž˜μ˜ μ½”λ“œμ—μ„œ λ‹€λ¦¬μ—μ„œ μ˜¬λΌκ°€λ©΄ ++, λ‚΄λ €κ°€λ©΄ --λ₯Ό ν•΄μ€λ‹ˆλ‹€
μΆ”κ°€λ‘œ, 트럭이 닀리에 μ˜¬λΌκ°€λŠ” κ²½μš°μ—λŠ” trcuckNum + 1<= bridge_length인지 ν™•μΈν•˜κ³  올라갈 수 μžˆλ„λ‘ ν•©λ‹ˆλ‹€!

Comment on lines +14 to +24
if onBridge and time - onBridge[0][1] >= bridge_length:
sum -= onBridge.popleft()[0]
truckNum -= 1

# λ‹€μŒ 트럭이 닀리에 올라갈 수 μžˆλŠ” 경우 처리
# 트럭이 있고 합이 weight을 λ„˜μ§€ μ•ŠμœΌλ©°, μˆ˜κ°€ bridge_lengthλ₯Ό λ„˜κΈ°μ§€ μ•ŠλŠ” 경우
if len(truck_weights) != 0 and sum + truck_weights[0] <= weight and truckNum + 1 <= bridge_length:
truck = truck_weights.popleft() # pop
onBridge.append((truck, time)) # 닀리 μœ„μ˜ truck에 tuple μΆ”κ°€
sum += truck # 무게 μΆ”κ°€
truckNum += 1 # κ±΄λ„ˆκ³  μžˆλŠ” 트럭 μΆ”κ°€
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

popleft() πŸ‘πŸ‘πŸ‘

Copy link
Collaborator

@pknujsp pknujsp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ‹œκ°„μ²˜λ¦¬ λ•Œλ¬Έμ— μ–΄λ ΅κ²Œ 돌고 λŒμ•„ ν‘ΈλŠ”λ° ν•œμ°Έ 걸렸던 기얡이 λ‚˜λŠ” λ¬Έμ œκ΅°μš”.

μ‹œκ°„ μžμ²΄λŠ” 문제 풀이에 큰 μš”μ†ŒλŠ” μ•„λ‹ˆκ³  λ‹¨μˆœν•˜κ²Œ 큐만 닀루면 λ˜λŠ” κ±°λΌλŠ”κ±Έ 늦게 κΉ¨λ‹¬μ•˜μ—ˆλ„€μš”.

λ˜‘κ°™μ΄ 덱을 ν™œμš©ν•΄μ„œ
νŠΈλŸ­λ“€μ„ 덱 였λ₯Έμͺ½μ—μ„œ μ™Όμͺ½λ°©ν–₯으둜 움직이기 μœ„ν•΄μ„œ

반볡문이 λŒμ•„κ°ˆ λ•Œ λ§ˆλ‹€ 덱의 맨 μ•ž μ›μ†Œλ₯Ό λΊΈ ν›„

닀리 μœ„μ˜ 무게λ₯Ό μ²΄ν¬ν•΄μ„œ λ¬΄κ²Œκ°€ ν•˜μ€‘μ΄ˆκ³Όλ©΄ μ°¨κ°€ 닀리 μœ„μ— μƒˆλ‘œ μ˜¬λΌκ°€μ§€ μ•ŠλŠ” λ‹€λŠ” κ±Έ λœ»ν•˜λŠ” 의미둜 큐의 맨 끝에 0을 μΆ”κ°€ν•˜κ³ 

μ•„λ‹ˆλΌλ©΄ μƒˆ μ°¨λ₯Ό truck_weightsμ—μ„œ λΉΌμ„œ 닀리 큐에 μΆ”κ°€ν•΄μ„œ μ§„ν–‰ν•˜λŠ” λ°©μ‹μœΌλ‘œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

from collections import *
V = 0

def solution(bridge_length, weight, truck_weights):
    t = 0
    total_weight = 0
    b = deque([V] * bridge_length)

    while truck_weights:
        t += 1
        total_weight -= b.popleft()

        if total_weight + truck_weights[0] > weight:
            b.append(V)
        else:
            truck = truck_weights.pop(0)
            total_weight += truck
            b.append(truck)

    t += bridge_length
    return t

(https://www.acmicpc.net/problem/13335) 백쀀에 λ˜‘κ°™μ€ 게 μžˆμ–΄μ„œ 곡짜둜 λ°±μ€€ 점수 κ°€μ Έκ°ˆ 수 μžˆμ–΄μš”

@Munbin-Lee
Copy link
Member

Code
#include <string>
#include <vector>
#include <deque>

using namespace std;

int solution(int bridge_length, int max_weights, vector<int> truck_weights) {
    int answer = 1;
    int cur_weights = 0;
    int truck_index = 0;
    deque<int> bridge(bridge_length, 0);
    
    while (true) {
        int weight = bridge.front();
        bridge.pop_front();
        cur_weights -= weight;
        
        if (cur_weights + truck_weights[truck_index] <= max_weights) {
            cur_weights += truck_weights[truck_index];
            bridge.emplace_back(truck_weights[truck_index]);
            truck_index++;
            
            if (truck_index == truck_weights.size()) {
                return answer + bridge_length;
            }
        } else {
            bridge.emplace_back(0);
        }
        
        answer++;
    }
    
    return answer;
}

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 특유의 μ“°λ ˆκΈ° 같은 지문...

μ²˜μŒμ—λŠ” 1초 λ‹¨μœ„λ‘œ νŒλ³„ν•˜κ²Œ 짜렀 ν•˜λ‹€κ°€

2번 예제보고 λ„ˆλ¬΄ λΉ„νš¨μœ¨μ μΌ 것 κ°™μ•„ λ‹€λ₯Έ ν’€μ΄λ‘œ ν’€λ‹€κ°€ λ§‰ν˜€μ„œ

κ²°κ΅­ 1초 λ‹¨μœ„λ‘œ νŒλ³„ν•˜κ²Œ μ§°λŠ”λ° ν†΅κ³Όν•˜λ„€μš”

트럭 ν•˜λ‚˜λ‹Ή μ΅œλŒ€ 1만 μ΄ˆκ°€ 걸리고, 트럭이 1만 κ°œκ°€ μžˆμœΌλ‹ˆ 1μ–΅ 번의 연산을 ν•΄μ•Ό λ˜μ–΄μ„œ μ–΄λŠ 정도 μ‹œκ°„μ΄ 걸리리라 μƒκ°ν–ˆλŠ”λ° μƒκ°λ³΄λ‹€λŠ” μ‹œκ°„μ΄ 적게 κ±Έλ¦¬λ„€μš”.

문제 ν—ˆκ²μ§€κ² ν’€λ‹€λ³΄λ‹ˆ ν™μ£Όλ‹˜ 풀이에 λΉ„ν•΄ λ„ˆλ¬΄λ‚˜ μ‘°μž‘ν•œλ°μš”...γ…‹γ…‹

@H0ngJu H0ngJu merged commit 1ee7e98 into main Mar 25, 2024
1 check passed
@H0ngJu H0ngJu deleted the 2-H0ngJu branch March 25, 2024 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants