Skip to content

Commit 18a0d98

Browse files
authored
Merge pull request #137 from hyg4779/main
0906 스터디 문제풀이
2 parents 6fce1a3 + 7a48f9a commit 18a0d98

File tree

6 files changed

+242
-0
lines changed

6 files changed

+242
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def solution(n, k, arr):
2+
# 다리길이, 견딜 수 있는 무게, 트럭무게배열
3+
def bridge():
4+
nonlocal state, n, w
5+
tmp = False
6+
7+
for truck in state:
8+
truck[1] += 1
9+
if truck[1] == n:
10+
tmp = True
11+
else:
12+
if tmp:
13+
val, _ = state.pop(0)
14+
w -= val
15+
return
16+
17+
18+
answer = 0
19+
state, w = [], 0
20+
21+
idx = 0
22+
while idx < len(arr):
23+
bridge()
24+
now = arr[idx]
25+
flag = False
26+
27+
if len(state) < n and w+now <= k:
28+
flag = True
29+
30+
if flag:
31+
w += now
32+
state.append([now, 0])
33+
idx += 1
34+
35+
answer += 1
36+
37+
while state:
38+
bridge()
39+
answer += 1
40+
41+
return answer
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def solution(queue1, queue2):
2+
3+
n, m = sum(queue1), sum(queue2)
4+
5+
l = len(queue1)
6+
answer = l
7+
8+
dp = [[[0, 0] for _ in range(l*2)] for _ in range(l*2)]
9+
dp[0][0] = [n, m]
10+
11+
12+
def dfs(a, b):
13+
nonlocal answer
14+
if dp[a][b][0] == dp[a][b][1]:
15+
answer = min(answer, a+b)
16+
return a+b
17+
18+
if a+1 < l*2:
19+
if a//l:
20+
dp[a+1][b] = [dp[a][b][0]-queue2[a%l], dp[a][b][1]+queue2[a%l]]
21+
else:
22+
dp[a+1][b] = [dp[a][b][0]-queue1[a%l], dp[a][b][1]+queue1[a%l]]
23+
dfs(a+1, b)
24+
25+
if b+1 < l*2:
26+
if b//l:
27+
dp[a][b+1] = [dp[a][b][0]+queue1[b%l], dp[a][b][1]-queue1[b%l]]
28+
else:
29+
dp[a][b+1] = [dp[a][b][0]+queue2[b%l], dp[a][b][1]-queue2[b%l]]
30+
dfs(a, b+1)
31+
32+
dfs(0, 0)
33+
34+
return -1 if answer == l else answer
35+
36+
37+
print(solution([3, 2, 7, 2], [4, 6, 5, 1]))
38+
print(solution([1, 2, 1, 2], [1, 10, 1, 2]))
39+
print(solution([1, 1], [1, 5]))

programmers/Lv2_위장/s1_hyg4779.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def solution(clothes):
2+
3+
answer = 1
4+
5+
hash_map = {}
6+
# 종류 별 몇벌이 있는지 더한다
7+
for prod, kind in clothes:
8+
hash_map[kind] = hash_map.get(kind, 0)+1
9+
10+
# 해당 옷을 입는 모든 경우를 곱한다(+1 하는건 안입는 경우)
11+
for kind in hash_map:
12+
answer *= (hash_map[kind]+1)
13+
14+
# 전체를 안 입는 경우를 뺸다
15+
return answer-1
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from collections import deque
2+
3+
def solution(n, paths, gates, summits):
4+
5+
graph = [[] for _ in range(n+1)]
6+
for a, b, c in paths:
7+
graph[a].append([b, c])
8+
graph[b].append([a, c])
9+
10+
11+
dist = [10000001 for _ in range(n+1)]
12+
for gate in gates:
13+
dist[gate] = 0
14+
15+
Q = deque(gates)
16+
while Q:
17+
now = Q.popleft()
18+
if now in summits: continue
19+
for node, cost in graph[now]:
20+
cost = max(dist[now], cost)
21+
if dist[node] > cost:
22+
Q.append(node)
23+
dist[node] = cost
24+
25+
26+
min_dis = float('inf')
27+
min_summit = -1
28+
29+
for summit in summits:
30+
if min_dis > dist[summit]:
31+
min_dis = dist[summit]
32+
min_summit = summit
33+
elif min_dis == dist[summit] and min_summit > summit:
34+
min_summit = summit
35+
36+
return [min_summit, min_dis]
37+
38+
'''
39+
import collections
40+
41+
def bfs(n, graph, gates, summits, distance):
42+
que = collections.deque(gates)
43+
while que:
44+
cur_loc = que.popleft()
45+
if cur_loc in summits: continue
46+
for next_loc, way in graph[cur_loc]:
47+
if distance[next_loc] > max(distance[cur_loc], way):
48+
que.append(next_loc)
49+
distance[next_loc] = max(distance[cur_loc], way)
50+
51+
return distance
52+
53+
def solution(n, paths, gates, summits):
54+
answer = []
55+
graph = [[] for _ in range(n+1)]
56+
for a, b, c in paths:
57+
graph[a].append([b, c])
58+
graph[b].append([a, c])
59+
60+
summits_dict = {}
61+
min_dis = float('inf')
62+
min_summit = -1
63+
for summit in summits:
64+
summits_dict[summit] = 1
65+
66+
distance = [10000001 for _ in range(n+1)]
67+
for gate in gates:
68+
distance[gate] = 0
69+
70+
distance = bfs(n, graph, gates, summits_dict, distance)
71+
72+
for summit in summits:
73+
if min_dis > distance[summit]:
74+
min_dis = distance[summit]
75+
min_summit = summit
76+
elif min_dis == distance[summit] and min_summit > summit:
77+
min_summit = summit
78+
79+
return [min_summit, min_dis]
80+
'''
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def solution(operations):
2+
3+
Q = list()
4+
5+
def q_sort(arr):
6+
if len(arr) <= 1:
7+
return arr
8+
pivot = arr[0]
9+
l = [a for a in arr[1:] if a < pivot]
10+
r = [b for b in arr[1:] if b >= pivot]
11+
return q_sort(l) + [pivot] + q_sort(r)
12+
13+
for comm in operations:
14+
if comm[0] == 'I':
15+
Q.append(int(comm.split(' ')[1]))
16+
new = q_sort(Q)
17+
Q = new
18+
19+
else:
20+
if Q:
21+
if len(comm) > 3:
22+
Q.pop(0)
23+
else:
24+
Q.pop()
25+
26+
return [Q[-1], Q[0]] if Q else [0, 0]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import sys
2+
sys.setrecursionlimit(10000)
3+
4+
5+
def solution(alp, cop, problems):
6+
answer = 10000
7+
n = len(problems)
8+
9+
max_al, max_cl = max(problems, key=lambda x:x[0])[0], max(problems, key=lambda x:x[1])[1]
10+
11+
dp = [[float('inf')]*181 for _ in range(181)]
12+
dp[alp][cop] = 0
13+
14+
def dfs(a, c, t):
15+
nonlocal answer
16+
17+
if a >= max_al and c >= max_cl:
18+
answer = min(t, answer)
19+
return
20+
21+
if dp[a][c] < t:
22+
return
23+
24+
dp[a][c] = t
25+
26+
for i in range(n):
27+
now = problems[i]
28+
if a < now[0]:
29+
dfs(now[0], c, t + now[0]-a)
30+
if c < now[1]:
31+
dfs(a, now[1], t + now[1]-c)
32+
if a >= now[0] and c >= now[1]:
33+
dfs(a+now[2], c+now[3], t+now[4])
34+
35+
36+
dfs(alp, cop, 0)
37+
38+
return answer
39+
40+
print(solution(10, 10, [[10, 15, 2, 1, 2], [20, 20, 3, 3, 4]]))
41+
print(solution(0, 0, [[0, 0, 2, 1, 2], [4, 5, 3, 1, 2], [4, 11, 4, 0, 2], [10, 4, 0, 4, 2]]))

0 commit comments

Comments
 (0)