Skip to content

Commit 6a8e2ed

Browse files
committed
Lv3 파이썬 진행중
1 parent 66841bc commit 6a8e2ed

17 files changed

+367
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def solution(n):
2+
d = [0] * (n+1)
3+
d[1] = 1
4+
d[2] = 2
5+
for i in range(3, n+1):
6+
d[i] = (d[i-1] + d[i-2]) % 1000000007
7+
return d[n]
8+
9+
print(solution(4))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
ans = -1
2+
3+
def go(N, number, result, count):
4+
global ans
5+
if count > 8: # 종료조건
6+
return
7+
if number == result: # 완료조건
8+
if count < ans or ans == -1:
9+
ans = count
10+
return
11+
nn = 0
12+
for i in range(0,8):
13+
nn = nn * 10 + N
14+
go(N, number, result + nn, count + 1 + i)
15+
go(N, number, result - nn, count + 1 + i)
16+
go(N, number, result * nn, count + 1 + i)
17+
go(N, number, result // nn, count + 1 + i)
18+
19+
20+
def solution(N, number):
21+
go(N,number,0,0)
22+
return ans
23+
print(solution(5,12))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def solution(s):
2+
s = s.lower()
3+
answer = []
4+
if len(s)==1:
5+
return 1
6+
else:
7+
for i in range(len(s)):
8+
for j in range(0, i):
9+
temp = s[j:i + 1]
10+
11+
if temp == temp[::-1]: # 뒤집었을 때와 같은지
12+
answer.append(temp)
13+
if answer:
14+
return len(max(answer, key=len))
15+
else:
16+
return 1
17+
18+
print(solution("asdf"))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def solution(n, money):
2+
d = [0]*(n+1)
3+
d[0] = 1
4+
5+
for i in money:
6+
for j in range(i,n+1):
7+
d[j] = (d[j] + d[j-i])%1000000007
8+
return d[n]
9+
10+
print(solution(5,[1,2,5]))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def solution(n, stations, w):
2+
answer = 0
3+
index = 1
4+
5+
size = 2*w+1
6+
for i in stations:
7+
answer += (i-w-index)//size
8+
if (i-w-index)%size != 0:
9+
answer += 1
10+
index = i + w + 1
11+
if n >= index:
12+
answer += (n + 1 - index) // size
13+
if (n + 1 - index) % size != 0:
14+
answer += 1
15+
16+
return answer
17+
18+
print(solution( 16, [9], 2))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
net = [[0]*201 for i in range(201)]
2+
check = [False]*201
3+
4+
def dfs(x, n):
5+
global net
6+
global check
7+
8+
check[x] = 1
9+
10+
for i in range(n):
11+
if net[x][i] == 1 and check[i] != 1:
12+
dfs(i, n)
13+
14+
def solution(n, computers):
15+
answer = 0
16+
global check
17+
18+
for i in range(n):
19+
for j in range(n):
20+
if computers[i][j] == 1:
21+
net[i][j] = 1
22+
23+
for i in range(n):
24+
if check[i] != 1:
25+
dfs(i, n)
26+
answer += 1
27+
28+
return answer
29+
30+
print(solution(3, [[1, 1, 0], [1, 1, 0], [0, 0, 1]]))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def solution(routes):
2+
answer = 0
3+
cam = []
4+
routes.sort(key=lambda e: e[0]) # 출발지점 기준으로 정렬
5+
6+
for i in routes:
7+
cs = i[0] # 출발지점 저장
8+
flag =True
9+
for j in cam:
10+
if j[0] <= cs <= j[1]:
11+
flag = True
12+
else: # 출발 지점이 카메라 영역안에 없다면
13+
flag = False
14+
break
15+
if flag:
16+
cam.append(i)
17+
else:
18+
answer += 1
19+
cam.clear()
20+
cam.append(i)
21+
22+
answer += 1
23+
return answer
24+
25+
print(solution([[-20,15], [-14,-5], [-18,-13], [-5,-3]]))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def solution(n):
2+
d = [0]*(n+1)
3+
d[0] = 1
4+
d[1] = 1
5+
for i in range(2,n+1):
6+
d[i] = (d[i-1] + d[i-2])%1234567
7+
return d[n]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
dy = [0,1,0,-1]
2+
dx = [1,0,-1,0]
3+
4+
def solution(dirs):
5+
global dy
6+
global dx
7+
answer = 0
8+
visited = []
9+
10+
start = [0,0]
11+
for i in dirs:
12+
if i == 'U':
13+
if [start,3] not in visited:
14+
visited.append([start,3])
15+
start = [start[0]+dy[1], start[1]+dx[1]]
16+
if -5 > start[0] or start[0] > 5 or start[1] >5 or start[1] < -5:
17+
start = [start[0] - dy[1], start[1] - dx[1]]
18+
continue
19+
if [start,1] not in visited:
20+
visited.append([start,1])
21+
answer += 1
22+
elif i == 'R':
23+
if [start,2] not in visited:
24+
visited.append([start,2])
25+
start = [start[0] + dy[0], start[1] + dx[0]]
26+
if -5 > start[0] or start[0] > 5 or start[1] >5 or start[1] < -5:
27+
start = [start[0] - dy[0], start[1] - dx[0]]
28+
continue
29+
if [start, 0] not in visited:
30+
visited.append([start, 0])
31+
answer += 1
32+
elif i == 'D':
33+
if [start,1] not in visited:
34+
visited.append([start,1])
35+
start = [start[0] + dy[3], start[1] + dx[3]]
36+
if -5 > start[0] or start[0] > 5 or start[1] >5 or start[1] < -5:
37+
start = [start[0] - dy[3], start[1] - dx[3]]
38+
continue
39+
if [start, 3] not in visited:
40+
visited.append([start, 3])
41+
answer += 1
42+
elif i == 'L':
43+
if [start,0] not in visited:
44+
visited.append([start,0])
45+
start = [start[0] + dy[2], start[1] + dx[2]]
46+
if -5 > start[0] or start[0] > 5 or start[1] >5 or start[1] < -5:
47+
start = [start[0] - dy[2], start[1] - dx[2]]
48+
continue
49+
if [start, 2] not in visited:
50+
visited.append([start, 2])
51+
answer += 1
52+
53+
return answer
54+
55+
print(solution("LURDLLURD"))

Programmers/Lv3/Lv3_배달.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def search(cost, check):
2+
end = 0
3+
temp = 500001
4+
for i in range(1, len(cost)):
5+
if check[i] == False and cost[i] < temp: # 가장 작은 비용이 드는 마을 번호
6+
temp = cost[i]
7+
end = i
8+
return end
9+
10+
def solution(N, road, K):
11+
answer = 0
12+
13+
graph = [[500001 for col in range(N+1)] for row in range(N+1)] # 비용 정보
14+
cost = [500001] * (N + 1)
15+
check = [False] * (N + 1)
16+
cost[1] = 0 # 1번 지역은 0으로 초기화
17+
18+
for i in range(len(road)):
19+
if graph[road[i][0]][road[i][1]] == 500001:
20+
graph[road[i][0]][road[i][1]] = road[i][2]
21+
graph[road[i][1]][road[i][0]] = road[i][2]
22+
else:
23+
if graph[road[i][0]][road[i][1]] > road[i][2]:
24+
graph[road[i][0]][road[i][1]] = road[i][2]
25+
graph[road[i][1]][road[i][0]] = road[i][2]
26+
27+
for i in range(1,N+1):
28+
end = search(cost,check) # 가장 가까운 마을 찾기
29+
for j in range(1, N+1):
30+
cost[j] = min(cost[j], cost[end] + graph[end][j])
31+
check[end] = True
32+
33+
for i in range(1,len(cost)):
34+
if cost[i] <= K:
35+
answer += 1
36+
return answer
37+
38+
print(solution(5,[[1,2,1],[2,3,3],[5,2,2],[1,4,2],[5,3,1],[5,4,2]],3))

0 commit comments

Comments
 (0)