-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
There was a problem hiding this 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 # νμ¬ λ€λ¦¬ μμ μλ νΈλ μ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νΉμ truckNum
μ΄ μ½λμμ λ¬΄μ¨ μν μ νκ³ μλ μ§ μ μ μμκΉμ?
There was a problem hiding this comment.
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μΈμ§ νμΈνκ³ μ¬λΌκ° μ μλλ‘ ν©λλ€!
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 # 건λκ³ μλ νΈλ μΆκ° |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
popleft()
πππ
There was a problem hiding this 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) λ°±μ€μ λκ°μ κ² μμ΄μ 곡μ§λ‘ λ°±μ€ μ μ κ°μ Έκ° μ μμ΄μ
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μ΅ λ²μ μ°μ°μ ν΄μΌ λμ΄μ μ΄λ μ λ μκ°μ΄ κ±Έλ¦¬λ¦¬λΌ μκ°νλλ° μκ°λ³΄λ€λ μκ°μ΄ μ κ² κ±Έλ¦¬λ€μ. λ¬Έμ νκ²μ§κ² νλ€λ³΄λ νμ£Όλ νμ΄μ λΉν΄ λ무λ μ‘°μ‘νλ°μ...γ γ |
π λ¬Έμ λ§ν¬
λ€λ¦¬λ₯Ό μ§λλ νΈλ
βοΈ μμλ μκ°
2μκ°
β¨ μλ μ½λ
λ¬Έμ μ 쑰건μ κ°λ¨ν©λλ€.
μ λ μ΄ λ¬Έμ μ μΌμ΄μ€λ 1. νΈλμ΄ λ€λ¦¬λ₯Ό μ§λκ°λ κ²½μ°μ 2. μ¬λΌκ°λ κ²½μ°λ‘ 2κ°μ§μ κ²½μ°λ‘ λλ μ ν΄κ²°νμ΅λλ€.
=> ν΄λΉ νΈλμ dequeμμ μ κ±° & 무κ²λ μ κ±°
=> truck_weightsμμ popν ν, onBridge, sumμ κ°κ° μΆκ° & λ€λ¦¬ μμ νΈλ μ μ¦κ°
λ€λ§ μ λ μκ°μ νλ¦ μ²λ¦¬λ₯Ό μ΄λ»κ² ν΄μΌ ν μ§ λͺ°λΌ μ‘°κΈ λνμ κ²ͺμμ΅λλ€.
μ§λκ°λ κ²½μ°, λ΄λ¦¬λ κ²½μ°μ λν΄μ μκ° μ²λ¦¬λ₯Ό κ°κ° νλ€λ³΄λ κ·Έλ₯ while λ¬Έμ λλ©΄μ timeμ κ³μ μ¦κ°νλλ‘ νλ©΄ λκ² κ΅¬λ (=νΈλμ΄ μ¬λΌκ°κ³ λ΄λ €κ°λ κ²½μ°λ ifλ¬ΈμΌλ‘ κ²μ¬λ₯Όνλ) ν΄μ ν΄κ²°νμ΅λλ€.
λ°°μ΄ λ΄μ©λ€λ‘ μ΅λν μ νμΌλ‘ νλ €κ³ νλλ° μκ°λ³΄λ€ λ¬Έμ λ₯Ό νΈλλ° μκ°μ΄ μ€λκ±Έλ Έμ§λ§ λΏλ―νλ€μ π₯
π μλ‘κ² μκ²λ λ΄μ©
append()λ 리μ€νΈμ λμ μμ 1κ°λ₯Ό μΆκ°νλ κ²μ΄κ³ , extend()λ 리μ€νΈμ λμ λͺ¨λ μμλ₯Ό μ½μ κ°λ₯νλ€.
extend([A, B, C])λ₯Ό νλ©΄ 리μ€νΈμ λμ A, B, Cκ° κ°κ°μ μμλ‘μ μΆκ°λλ€