Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4b7d84c

Browse files
committed
Lv3 파이썬 완료
1 parent ef46865 commit 4b7d84c

12 files changed

+319
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def solution(n, edge):
2+
answer = 0
3+
graph = [set() for x in range(n+1)]
4+
dist = [0] * (n+1)
5+
check = [False] * (n+1)
6+
q = []
7+
8+
for i in edge: # 연결 정보 저장
9+
graph[i[0]].add(i[1])
10+
graph[i[1]].add(i[0])
11+
12+
q.append(1)
13+
check[1] = True
14+
dist[1] = 0
15+
16+
while q:
17+
start = q.pop(0)
18+
for i in graph[start]:
19+
if not check[i]:
20+
check[i] = True
21+
dist[i] = dist[start] + 1
22+
q.append(i)
23+
24+
inf = max(dist) # 가장 먼 거리
25+
for i in range(1, n+1):
26+
if dist[i] == inf:
27+
answer += 1
28+
return answer
29+
30+
print(solution(6, [[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]])) # 3

‎Programmers/Lv3/Lv3_단어변환.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
answer = 999999999
2+
check = []
3+
4+
def DFS(cur, target, cnt, words):
5+
global answer
6+
global check
7+
8+
if cur == target:
9+
if answer > cnt:
10+
answer = cnt
11+
return
12+
13+
for i in range(len(words)):
14+
if check[i]: # 이미 사용한 단어라면 패스
15+
continue
16+
diff = 0
17+
for j in range(len(cur)):
18+
if cur[j] != words[i][j]:
19+
diff += 1
20+
if diff != 1: # 다른 글자가 1개가 아니면 패스
21+
continue
22+
23+
check[i] = True
24+
DFS(words[i], target, cnt + 1, words)
25+
check[i] = False
26+
27+
def solution(begin, target, words):
28+
global answer
29+
global check
30+
31+
check = [False]*len(words)
32+
33+
for i in range(len(words)):
34+
diff = 0
35+
for j in range(len(begin)):
36+
if begin[j] != words[i][j]:
37+
diff += 1
38+
if diff != 1:
39+
continue
40+
check[i] = True
41+
DFS(words[i], target, 1, words)
42+
check[i] = False
43+
44+
if answer == 999999999:
45+
return 0
46+
else:
47+
return answer
48+
print(solution("hit", "cog", ["hot", "dot", "dog", "lot", "log"])) # 0

‎Programmers/Lv3/Lv3_등굣길.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def solution(m, n, puddles):
2+
road = [[0 for col in range(m+1)] for row in range(n+1)]
3+
check = [[0 for col in range(m+1)] for row in range(n+1)]
4+
5+
for i in puddles:
6+
check[i[1]][i[0]] = -1
7+
8+
road[1][0] = 1
9+
for i in range(1, n+1):
10+
for j in range(1, m+1):
11+
if check[i][j] == -1:
12+
road[i][0] = 0
13+
else:
14+
road[i][j] = (road[i][j-1] + road[i-1][j]) % 1000000007
15+
16+
return road[n][m]
17+
18+
print(solution(4, 3, [[2, 2]]))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import operator
2+
3+
# SRF 스케쥴링 구현!
4+
def solution(jobs):
5+
answer = 0
6+
pq_len = []
7+
pq_arrive = []
8+
9+
for i in jobs:
10+
pq_arrive.append(i)
11+
pq_arrive.sort(key=operator.itemgetter(0)) # 가장 빨리 도착한 작업 순으로 정렬
12+
time = pq_arrive[0][0] # 가장 빨리 도착한 작업의 시간
13+
pq_len.append(pq_arrive.pop(0)) # 작업길이 순으로 정렬할 배열에 추가
14+
15+
while pq_len or pq_arrive: # 두 배열 중 하나라도 작업이 남아있으면
16+
if pq_arrive and time >= pq_arrive[0][0]: # 현재 시간보다 도착한 시간이 빠르다면
17+
pq_len.append(pq_arrive.pop(0)) # 작업길이 순으로 정렬할 배열에 추가
18+
pq_len.sort(key=operator.itemgetter(1))
19+
elif pq_len: # 아직 다른 작업이 도착하지 않았을 때
20+
arrive = pq_len[0][0]
21+
length = pq_len[0][1]
22+
answer += length # 작업시간을 answer에 추가
23+
if time < arrive: # 도착시간 보다 이른 시간이라면
24+
time = arrive
25+
answer += time-arrive # 작업을 하기까지 기다린 대기시간을 더하기기
26+
time += length # 현재시간 갱신
27+
pq_len.pop(0)
28+
else:
29+
time = pq_arrive[0][0] # 현재시간 갱신
30+
return answer//len(jobs)
31+
32+
print(solution([[0, 3], [1, 9], [2, 6]])) # 9
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import operator
2+
import functools
3+
4+
def cmp(a,b):
5+
if a[1] > b[1]:
6+
return -1
7+
elif a[1] == b[1]:
8+
if a[2] < b[2]:
9+
return -1
10+
else:
11+
return 1
12+
else:
13+
return 1
14+
15+
def solution(genres, plays):
16+
answer = []
17+
total = dict()
18+
info = []
19+
for i in range(len(genres)):
20+
if genres[i] in total:
21+
total[genres[i]] += plays[i]
22+
else:
23+
total[genres[i]] = plays[i]
24+
info.append([genres[i], plays[i], i])
25+
26+
totalArr = sorted(total.items(), key=operator.itemgetter(1), reverse=True)
27+
28+
for i in totalArr:
29+
temp = []
30+
for j in info:
31+
if j[0] == i[0]:
32+
temp.append(j)
33+
tempArr = sorted(temp, key=functools.cmp_to_key(cmp))
34+
if len(tempArr) > 1:
35+
for j in range(2):
36+
answer.append(tempArr[j][2])
37+
else:
38+
answer.append(tempArr[0][2])
39+
40+
return answer
41+
42+
print(solution( ["classic", "pop", "classic", "classic", "pop"], [150, 600, 150, 150, 2500]))
43+
# [4, 1, 3, 0]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 프림알고리즘(앞 단계에서 만들어진 신장 트리 집합에 인접한 정점들 중에서 최소 간선으로 연결된 정점을 선택하여 트리를 확장)
2+
import operator
3+
4+
def isEnd(check, n):
5+
for i in range(n):
6+
if check[i] == False:
7+
return False
8+
return True
9+
10+
def solution(n, costs):
11+
answer = 0
12+
check = [False]*101
13+
14+
check[costs[0][0]] = True # 시작지점 True
15+
16+
while not isEnd(check,n):
17+
island = []
18+
for i in range(n):
19+
if check[i]: # 검사할 정점
20+
for j in costs:
21+
if (j[0] == i and check[j[1]] == False) or (j[1] == i and check[j[0]] == False): # 아직 안 사용한 간선일 때
22+
island.append(j)
23+
24+
island.sort(key=operator.itemgetter(2))
25+
temp = island[0]
26+
answer += temp[2]
27+
check[temp[0]] = True
28+
check[temp[1]] = True
29+
costs.remove(temp)
30+
31+
return answer
32+
33+
print(solution(4, [[0, 1, 1], [0, 2, 2], [1, 2, 5], [1, 3, 1], [2, 3, 8]])) # 4

‎Programmers/Lv3/Lv3_여행경로.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
check = []
2+
tmp = "a"
3+
4+
def DFS(tickets, end, count, path):
5+
global check
6+
global tmp
7+
8+
if count >= len(tickets):
9+
if tmp > path:
10+
tmp = path
11+
return
12+
13+
for i in range(len(tickets)): # 사용할 티켓 찾기
14+
if check[i] == False and tickets[i][0] == end:
15+
check[i] = True
16+
DFS(tickets, tickets[i][1], count + 1, path+tickets[i][1])
17+
check[i] = False
18+
19+
20+
def solution(tickets):
21+
global check
22+
global tmp
23+
24+
check = [False] * len(tickets)
25+
DFS(tickets, "ICN", 0, "ICN")
26+
answer = []
27+
28+
for i in range(0,len(tmp),3):
29+
answer.append(tmp[i:i+3])
30+
31+
return answer
32+
33+
print(solution( [["ICN", "SFO"], ["ICN", "ATL"], ["SFO", "ATL"], ["ATL", "ICN"], ["ATL", "SFO"]]))
34+
# ["ICN", "ATL", "ICN", "SFO", "ATL", "SFO"]

‎Programmers/Lv3/Lv3_예산.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def solution(budgets, M):
2+
right = max(budgets)
3+
left = 0
4+
5+
while left <= right:
6+
total = 0
7+
mid = (left+right)>>1
8+
for i in budgets:
9+
if i < mid:
10+
total += i
11+
else:
12+
total += mid
13+
if total > M:
14+
right = mid-1
15+
else:
16+
left = mid + 1
17+
return right
18+
print(solution([120, 110, 140, 150], 485)) # 127
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def solution(operations):
2+
num = []
3+
for i in operations:
4+
temp = i.split()
5+
if temp[0] == 'I':
6+
num.append(int(temp[1]))
7+
else:
8+
if num:
9+
if temp[1] == '1':
10+
num.sort()
11+
num.pop()
12+
else:
13+
num.sort()
14+
num.pop(0)
15+
num.sort()
16+
if num:
17+
return [num[-1], num[0]]
18+
else:
19+
return [0,0]
20+
21+
22+
print(solution(["I -45", "I 653", "D 1", "I -642", "I 45", "I 97", "D 1", "D -1", "I 333"]))
23+
# [7,5]

‎Programmers/Lv3/Lv3_입국심사.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def solution(n, times):
2+
left = 0
3+
right = n * max(times)
4+
5+
while left < right:
6+
mid = (left+right)>>1
7+
count = 0
8+
for i in range(len(times)):
9+
count += mid//times[i]
10+
if count >= n:
11+
right = mid
12+
else:
13+
left = mid+1
14+
15+
return left
16+
17+
print(solution( 6, [7, 10])) # 28

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /