-
Notifications
You must be signed in to change notification settings - Fork 6
upload W3 - disk, nw, node, spicy #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # disk.py | ||
|
|
||
| def solution(jobs): | ||
| answer = 0 # 평균 작업 시간 저장 | ||
| current = 0 # 현재 몇ms가 흐른 상태인지 | ||
| finish = [] # 종료한 job의 인덱스 저장 | ||
|
|
||
| jobs.sort() | ||
|
|
||
| while(len(finish) != len(jobs)): # 모든 job을 clear했으면 종료 | ||
| for idx in range(len(jobs)): | ||
| # 인덱스를 늘리면서 처리되지 않은 job들을 찾음 | ||
| if idx not in finish: | ||
| if jobs[idx][0] > current: | ||
| current = jobs[idx][0] | ||
| break | ||
| vaild = [] # 현 시점에서 처리가능한 job들을 모아둠 (현재시간보다 요청시간이 이전이거나 같은 경우) | ||
| for job, i in zip(jobs[idx:], range(idx,len(jobs))): | ||
| if job[0] > current: # 작업의 요청시간이 현재보다 클 경우 | ||
| break | ||
| if i not in finish: | ||
| vaild.append(job + [i]) | ||
| next_job = sorted(vaild, key=lambda x : (x[1]))[0] | ||
| finish.append(next_job[2]) | ||
| answer += (current - next_job[0] + next_job[1]) | ||
| current += next_job[1] | ||
| break | ||
| return answer // (len(jobs)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # 프로그래머스 - 네트워크 | ||
| # network.py | ||
|
|
||
| network = [] | ||
| visited = [] | ||
|
|
||
| def recursive(p): | ||
| result = [] | ||
| global network | ||
| global visited | ||
|
Comment on lines
+7
to
+10
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 경험 상.. bfs나 dfs 함수 만들 때, solution 함수 내에서 선언하면 global 없이도 사용가능한 것 같더라구요? 코테상에서는 이런 방법도 편하게 쓸 수 있을 것 같아서 알려드려요!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 간단 예시 def solution():
network = []
visited = []
def recursive():
# network, visited 사용~ |
||
|
|
||
| if(visited[p]): | ||
| return [] | ||
| else: | ||
| visited[p] = 1 | ||
|
|
||
| # 빈 리스트는 for문 안들어가고 바로 빈리스트 반환 | ||
| for nw in network[p]: | ||
| result += ([nw] + recursive(nw)) | ||
| return result | ||
|
|
||
| def solution(n, computers): | ||
| answer = 0 | ||
| start = 0 | ||
|
|
||
| global visited | ||
| visited = [0] * n | ||
|
|
||
| #컴퓨터별 연결정보 리스트 저장 | ||
| global network | ||
| network = [[]] * n | ||
|
|
||
| for idx, computer in enumerate(computers): | ||
| network[idx] = [i for i, value in enumerate(computer) if idx != i and value==1] | ||
| if computer.count(1) == 1: #다른 컴퓨터와 연결이 없는 컴퓨터일 경우 | ||
| visited[idx] = 1 | ||
| answer += 1 | ||
|
|
||
| while(visited.count(0)): # 모두 방문 했을 경우 종료 | ||
| result = recursive(start) | ||
| if(result): # 방문 했던 노드가 아니였을 때 네트워크 수 추가 | ||
| answer += 1 | ||
| start += 1 | ||
|
|
||
| return answer | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from collections import deque, Counter | ||
|
|
||
| def get_node_list(n, edge): | ||
|
|
||
| graph = [[] for i in range(n+1)] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반복문에서 인덱스 i가 딱히 쓰이지 않는다면, 언더스코어(_)를 쓰는 게 좋다고 해요! (간지) |
||
|
|
||
| for e in edge: | ||
| graph[e[0]].append(e[1]) | ||
| graph[e[1]].append(e[0]) | ||
|
|
||
| return graph | ||
|
|
||
| def solution(n, edge): | ||
| answer = 0 | ||
| queue = deque([1]) | ||
| visited = [] | ||
| shortest = [0]*(n+1) #1로 부터의 최단 거리 저장 리스트, 0과 1 인덱스는 쓰이지 않음 | ||
|
|
||
| # step1. 노드의 정보를 담은 2차원 배열 구현 (행-노드 인덱스)(열-노드에 연결된 다른 노드들(순서X)) | ||
| graph = get_node_list(n, edge) | ||
|
|
||
| distance = 0 | ||
| # step2. | ||
| while(queue): | ||
| now = queue.popleft() | ||
| if now not in visited: | ||
| # print(now) | ||
| visited.append(now) | ||
| adj = graph[now] | ||
| for node in adj: | ||
| if not shortest[node] and node != 1: | ||
| shortest[node] = shortest[now] + 1 | ||
| queue += node | ||
|
|
||
| answer = shortest.count(max(shortest)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 한 줄로.. good입니당! |
||
|
|
||
| return answer | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # spicy.py | ||
| # 프로그래머스 - 더 맵게 | ||
|
|
||
| # 모든 음식의 스코빌 지수가 K를 넘겼는지 판별 (안넘겼으면 True) | ||
| def notK(scoville, K): | ||
| if(scoville[0] < K): | ||
| return True | ||
| return False | ||
|
|
||
| def solution(scoville, K): | ||
| answer = 0 | ||
|
|
||
| scoville.sort() | ||
|
|
||
| while(notK(scoville, K)): | ||
| answer+=1 | ||
| try: | ||
| scoville.append(scoville.pop(0) + (scoville.pop(0)*2)) | ||
| scoville.sort() | ||
|
Comment on lines
+15
to
+19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반복문을 돌면서 pop(0)과 sort()를 매번 실행하는 것 때문에 효율성테스트에 통과되지 못하는거 같아요! |
||
| except: | ||
| return -1 | ||
|
|
||
| return answer | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zip 사용은 볼때마다 놀랍네요.. 저는 enumerate 보고 신세계였는데 아직 배울게 많네요 ㅎㅎ