Stack & Quene

주식가격

1차 답안

1
2
3
4
5
6
7
8
9
def solution(prices):
answer = []
for i in range(len(prices)):
cnt = 0
for j in range(i+1, len(prices)):
if prices[i] <= prices[j]:
cnt= cnt+1
answer.append(cnt)
return answer

비 효율성 문제로 통과 x

2차 답안

1
2
3
4
5
6
7
8
def solution(prices):
answer = [0] * len(prices)
for i in range(len(prices)):
for j in range(i+1, len(prices)):
answer[i] += 1
if prices[i] > prices[j]:
break
return answer

기능개발

1차 답안

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def solution(progresses, speeds):
answer = []
days = []
for i in range(len(progresses)):
days.append((100 - progresses[i]) / speeds[i])
for j,k in enumerate(days):
if j == 0:
max = k
answer.append(1)
continue
if k <= max:
answer[-1] += 1
else:
max = k
answer.append(1)
return answer

케이스 2통과 불가..?

2차답안

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def solution(progresses, speeds):
answer = []
days = []

for p,s in zip(progresses,speeds):
if (100-p) % s > 0:
days.append((100-p) // s + 1)
else:
days.append((100-p) // s)

for j,k in enumerate(days):
if j == 0:
max = k
answer.append(1)
continue
if k <= max:
answer[-1] += 1
else:
max = k
answer.append(1)
return answer

소수점 관련 요소 수정 후 통과

다리를 지나는 트럭

1차 답안

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
def solution(bridge_length, weight, truck_weights):  
truck_in = []
truck_out = []
cnt = 0
trucks_weight = 0
n = len(truck_weights)

while(len(truck_out) < n):
idx = 0
cnt = cnt + 1

if len(truck_in) == 0:
truck_in.append([1, truck_weights[0]])
trucks_weight += truck_weights.pop(0)
else:
if len(truck_weights) == 0:
for i in range(len(truck_in)):
truck_in[i][0] += 1

if truck_in[i][0] > bridge_length:
idx = 1

if idx == 1 and i == (len(truck_in) -1):
truck_out.append(truck_in.pop(0))

else:
for j in range(len(truck_in)):
truck_in[j][0] += 1

if truck_in[j][0] > bridge_length:
trucks_weight -= truck_in[j][1]
idx = 1

if idx == 1 and j == (len(truck_in) -1):
truck_out.append(truck_in.pop(0))

if len(truck_weights) == 0:
pass

elif (trucks_weight + truck_weights[0]) <= weight:
truck_in.append([1,truck_weights[0]])
trucks_weight += truck_weights.pop(0)
return cnt

비효율 적인 코드여서 통과 불가

2차 답안

1
2
3
4
5
6
7
8
9
10
11
12
def solution(bridge_length, weight, truck_weights):
bridge = [0] * bridge_length
sec = 0
while bridge :
bridge.pop(0)
sec += 1
if truck_weights :
if (sum(bridge) + truck_weights[0]) <= weight :
bridge.append(truck_weights.pop(0))
else:
bridge.append(0)
return sec

sum(List)
List 요소의 합을 구하는 함수

프린터

1차 답안

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def solution(priorities, location):

pi_list = [(p, i) for i, p in enumerate(priorities)]
waiting_q = []

while pi_list:
pi = pi_list.pop(0)
priority = pi[0]
p_list = [priority for priority, idx in pi_list]
if p_list:
max_p = max(p_list)
if priority >= max_p:
waiting_q.append(pi)
else:
pi_list.append(pi)
for i, item in enumerate(waiting_q):
if item[1] == location:
return i + 1