Skip to content

Commit ad34790

Browse files
authored
Kcm hw (#84)
* 주말코테 * 변경사항 * kcmHW * 찬민 프로그래머스 숙제
1 parent 9aef3af commit ad34790

File tree

8 files changed

+178
-0
lines changed

8 files changed

+178
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from _collections import deque
2+
3+
4+
def solution(numbers, target):
5+
answer = 0
6+
7+
q = deque()
8+
q.append((0, 0))
9+
10+
while q:
11+
value, index = q.popleft()
12+
13+
#print('value, index = ', value, index)
14+
15+
if index == len(numbers):
16+
if value == target:
17+
answer += 1
18+
19+
else:
20+
temp = numbers[index]
21+
q.append((value + temp, index + 1))
22+
q.append((value - temp, index + 1))
23+
24+
return answer
25+
26+
27+
#print(solution([1, 1, 1, 1, 1], 3))

‎cmkim/Programmers/DP/N으로 표현.py‎

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from collections import deque
2+
3+
4+
def solution(n, edge):
5+
answer = 0
6+
visited = [-1] * (n + 1)
7+
arr = [[] for _ in range(n + 1)]
8+
9+
for x, y in edge:
10+
arr[x].append(y)
11+
arr[y].append(x)
12+
13+
bfs(1, visited, arr)
14+
15+
max_v = max(visited)
16+
17+
for value in visited:
18+
if value == max_v:
19+
answer += 1
20+
21+
return answer
22+
23+
24+
def bfs(v, visited, arr):
25+
count = 0
26+
q = deque([[v, count]]) # 왜 이렇게 해야 되는지? deque 넣을 때 () 와 [] 차이?
27+
28+
while q:
29+
value = q.popleft()
30+
v = value[0]
31+
count = value[1]
32+
if visited[v] == -1:
33+
visited[v] = count
34+
count += 1
35+
for e in arr[v]:
36+
q.append([e, count])
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def solution(progresses, speeds):
2+
answer = []
3+
stack = []
4+
count = len(progresses)
5+
for i in range(count):
6+
div = (100-progresses[i])//speeds[i]
7+
if progresses[i] + div == 100:
8+
stack.append(div)
9+
else:
10+
stack.append(div+1)
11+
12+
idx = stack[0]
13+
count = 1
14+
for i in range(1, len(stack)):
15+
if stack[i] > idx:
16+
answer.append(count)
17+
count = 1
18+
idx = stack[i]
19+
else:
20+
count +=1
21+
22+
answer.append(count)
23+
24+
25+
return answer
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def solution(answers):
2+
answer = []
3+
a = [1, 2, 3, 4, 5]
4+
b = [2, 1, 2, 3, 2, 4, 2, 5]
5+
c = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
6+
count = [0, 0, 0]
7+
for i in range(len(answers)):
8+
if answers[i] == a[i % 5]:
9+
count[0] += 1
10+
11+
for i in range(len(answers)):
12+
if answers[i] == b[i % 8]:
13+
count[1] += 1
14+
15+
for i in range(len(answers)):
16+
if answers[i] == c[i % 10]:
17+
count[2] += 1
18+
19+
max_v = 0
20+
for i in range(3):
21+
max_v = max(max_v, count[i])
22+
23+
for i in range(3):
24+
if count[i] == max_v:
25+
answer.append(i + 1)
26+
27+
return answer
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def solution(participant, completion):
2+
participant.sort()
3+
completion.sort()
4+
num = len(completion)
5+
6+
for idx in range(num) :
7+
if participant[idx] != completion[idx] : return participant[idx]
8+
return participant[num]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import heapq
2+
3+
def solution(scoville, K):
4+
answer = 0
5+
heap = []
6+
for i in range(len(scoville)):
7+
heapq.heappush(heap, scoville[i])
8+
9+
while heap[0] < K:
10+
if len(heap) < 2:
11+
return -1
12+
13+
first = heapq.heappop(heap)
14+
second = heapq.heappop(heap)
15+
result = first + second * 2
16+
heapq.heappush(heap, result)
17+
answer += 1
18+
19+
return answer
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
n = int(input())
2+
arr = list(map(int, input().split()))
3+
add, sub, mul, div = map(int, input().split()) # [+] [-] [*] [//]
4+
5+
min_value = 1e9
6+
max_value = -1e9
7+
8+
def dfs(count, value):
9+
global min_value, max_value, add, sub, mul, div
10+
11+
if count == n:
12+
min_value = min(min_value, value)
13+
max_value = max(max_value, value)
14+
15+
else:
16+
if add > 0:
17+
add -= 1
18+
dfs(count + 1, value + arr[count])
19+
add += 1
20+
if sub > 0:
21+
sub -= 1
22+
dfs(count + 1, value - arr[count])
23+
sub += 1
24+
if mul > 0:
25+
mul -= 1
26+
dfs(count + 1, value * arr[count])
27+
mul += 1
28+
if div > 0:
29+
div -= 1
30+
dfs(count + 1, int(value / arr[count]))
31+
div += 1
32+
33+
dfs(1, arr[0])
34+
35+
print(max_value)
36+
print(min_value)

0 commit comments

Comments
 (0)