Skip to content

Commit 7135b10

Browse files
committed
2 parents 3d830cb + ad34790 commit 7135b10

File tree

11 files changed

+380
-0
lines changed

11 files changed

+380
-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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
n = int(input())
2+
3+
arr = []
4+
temp = [[0] * n for _ in range(n)]
5+
teacher = []
6+
find = 0
7+
for i in range(n):
8+
arr.append(list(map(str, input().split())))
9+
for j in range(n):
10+
if arr[i][j] == 'T':
11+
teacher.append((i, j))
12+
13+
14+
def dfs(count):
15+
global find
16+
if count == 3: # 장애물 3개가 다 설치된 경우
17+
for i in range(n): # 장애물 설치된 맵을 복사한 후에
18+
for j in range(n):
19+
temp[i][j] = arr[i][j]
20+
21+
if not check(): # 감시를 피하면 YES 출력후 종료
22+
print("YES")
23+
quit()
24+
25+
else: # 감시 못피하면 다른 경우도 검사해보기
26+
return
27+
28+
for i in range(n): # 빈 공간에 대해서 장애물 3개까지 설치해보는 과정
29+
for j in range(n):
30+
if arr[i][j] == 'X':
31+
arr[i][j] = 'O' # 알파벳 O임
32+
count += 1
33+
dfs(count)
34+
count -= 1
35+
arr[i][j] = 'X'
36+
37+
38+
def check():
39+
result = 0
40+
for x, y in teacher:
41+
for i in range(4): # 상하좌우 방향 검사
42+
if see(x, y, i): # 학생의 위치를 파��한 경우
43+
return True
44+
45+
return False # 학생의 위치를 파악하지 못한 경우
46+
47+
48+
def see(x, y, dir): # 상하좌우 순으로 검사
49+
if dir == 0:
50+
while 0 <= x < n:
51+
if temp[x][y] == 'S':
52+
return True
53+
if temp[x][y] == 'O':
54+
return False
55+
else:
56+
x -= 1
57+
if dir == 1:
58+
while 0 <= x < n:
59+
if temp[x][y] == 'S':
60+
return True
61+
if temp[x][y] == 'O':
62+
return False
63+
else:
64+
x += 1
65+
if dir == 2:
66+
while 0 <= y < n:
67+
if temp[x][y] == 'S':
68+
return True
69+
if temp[x][y] == 'O':
70+
return False
71+
else:
72+
y -= 1
73+
if dir == 3:
74+
while 0 <= y < n:
75+
if temp[x][y] == 'S':
76+
return True
77+
if temp[x][y] == 'O':
78+
return False
79+
else:
80+
y += 1
81+
82+
return False
83+
84+
85+
dfs(0)#dfs 함수안에서 한번만이라도 감시를 피했으면 YES 출력후 종료함
86+
print("NO")
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
n, m = map(int, input().split())
2+
3+
arr = []
4+
temp = [[0] * m for _ in range(n)]
5+
result = 0
6+
7+
dx = [-1, 1, 0, 0]
8+
dy = [0, 0, -1, 1]
9+
10+
for i in range(n):
11+
arr.append(list(map(int, input().split())))
12+
13+
14+
15+
def dfs(count):
16+
global result
17+
18+
if count == 3:
19+
for i in range(n):
20+
for j in range(m):
21+
temp[i][j] = arr[i][j]
22+
23+
for i in range(n):
24+
for j in range(m):
25+
if temp[i][j] == 2:
26+
virus(i, j)
27+
28+
result = max(result, get_score())
29+
return
30+
31+
for i in range(n):
32+
for j in range(m):
33+
if arr[i][j] == 0:
34+
arr[i][j] = 1
35+
count += 1
36+
dfs(count)
37+
arr[i][j] = 0
38+
count -= 1
39+
40+
41+
def virus(x, y):
42+
for i in range(4):
43+
nx = x + dx[i]
44+
ny = y + dy[i]
45+
if 0 <= nx < n and 0 <= ny < m:
46+
if temp[nx][ny] == 0:
47+
temp[nx][ny] = 2
48+
virus(nx, ny)
49+
50+
51+
def get_score():
52+
score = 0
53+
for i in range(n):
54+
for j in range(m):
55+
if temp[i][j] == 0:
56+
score += 1
57+
return score
58+
59+
60+
dfs(0)
61+
print(result)
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)