Skip to content

Commit b8845a1

Browse files
zaqquumMingguriguri
authored andcommitted
[PGS]#42577. 전화번호 목록/lv2/실패
https://school.programmers.co.kr/learn/courses/30/lessons/42577
1 parent 7390fc1 commit b8845a1

File tree

5 files changed

+301
-18
lines changed

5 files changed

+301
-18
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
프로그래머스. 이중 우선순위 큐
3+
https://school.programmers.co.kr/learn/courses/30/lessons/42628
4+
유형: Priority Queue
5+
"""

_WeeklyChallenges/W29-[PriorityQueue]/Study_PGS_42627_디스크컨트롤러.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,30 @@ def solution(jobs):
4343
"""
4444
import heapq
4545

46-
4746
def solution(jobs):
48-
answer = 0
49-
now = 0 # 현재시간
50-
i = 0 # 처리개수
51-
start = -1 # 마지막 완료시간
52-
heap = []
47+
jobs.sort() # 요청시간 기준 정렬
48+
job_len = len(jobs)
49+
i = 0 # jobs 인덱스
50+
end_time = 0 # 현재 시간
51+
return_time = 0 # 작업 반환 시간
52+
count = 0 # 작업 처리한 개수
5353

54-
while i < len(jobs):
55-
for job in jobs:
56-
if start < job[0] <= now:
57-
heapq.heappush(heap, [job[1], job[0]])
54+
heap = []
5855

59-
if heap:
60-
current = heapq.heappop(heap)
61-
start = now
62-
now += current[0]
63-
answer += now - current[1] # 요청으로부터 처리시간
56+
while count < job_len:
57+
# 현재 시간에 요청된 작업 처리
58+
while i < job_len and jobs[i][0] <= end_time:
59+
heapq.heappush(heap, (jobs[i][1], jobs[i][0], i)) # 소요시간, 요청시간, 작업번호 순서
6460
i += 1
65-
else:
66-
now += 1
6761

68-
return answer // len(jobs)
62+
# 대기 큐에 작업이 있다면, 시간을 업데이트한다.
63+
if len(heap) > 0:
64+
work_time, start_time, num = heapq.heappop(heap)
65+
end_time += work_time
66+
return_time += end_time - start_time
67+
count += 1
68+
else:
69+
# 대기 큐가 비었다면, 다음 작업이 올 때까지 기다려야 한다.
70+
end_time = jobs[i][0]
6971

72+
return return_time // job_len
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
프로그래머스. 디스크 컨트롤러
3+
https://school.programmers.co.kr/learn/courses/30/lessons/42627
4+
유형: Priority Queue
5+
"""
6+
"""
7+
풀이1
8+
"""
9+
import heapq
10+
11+
def solution(jobs):
12+
jobs.sort() # 요청시간 기준 정렬
13+
job_len = len(jobs)
14+
i = 0 # jobs 인덱스
15+
end_time = 0 # 현재 시간
16+
return_time = 0 # 작업 반환 시간
17+
count = 0 # 작업 처리한 개수
18+
19+
heap = []
20+
21+
while count < job_len:
22+
# 현재 시간에 요청된 작업 처리
23+
while i < job_len and jobs[i][0] <= end_time:
24+
heapq.heappush(heap, (jobs[i][1], jobs[i][0], i)) # 소요시간, 요청시간, 작업번호 순서
25+
i += 1
26+
27+
# 대기 큐에 작업이 있다면, 시간을 업데이트한다.
28+
if len(heap) > 0:
29+
work_time, start_time, num = heapq.heappop(heap)
30+
end_time += work_time
31+
return_time += end_time - start_time
32+
count += 1
33+
else:
34+
# 대기 큐가 비었다면, 다음 작업이 올 때까지 기다려야 한다.
35+
end_time = jobs[i][0]
36+
37+
return return_time // job_len
38+
39+
40+
"""
41+
풀이2
42+
출처: https://velog.io/@kiwoong96/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4PythonLevel3-%EB%94%94%EC%8A%A4%ED%81%AC-%EC%BB%A8%ED%8A%B8%EB%A1%A4%EB%9F%AC
43+
"""
44+
import heapq
45+
46+
def solution(jobs):
47+
jobs.sort() # 요청시간 기준 정렬
48+
job_len = len(jobs)
49+
i = 0 # jobs 인덱스
50+
end_time = 0 # 현재 시간
51+
return_time = 0 # 작업 반환 시간
52+
count = 0 # 작업 처리한 개수
53+
54+
heap = []
55+
56+
while count < job_len:
57+
# 현재 시간에 요청된 작업 처리
58+
while i < job_len and jobs[i][0] <= end_time:
59+
heapq.heappush(heap, (jobs[i][1], jobs[i][0], i)) # 소요시간, 요청시간, 작업번호 순서
60+
i += 1
61+
62+
# 대기 큐에 작업이 있다면, 시간을 업데이트한다.
63+
if len(heap) > 0:
64+
work_time, start_time, num = heapq.heappop(heap)
65+
end_time += work_time
66+
return_time += end_time - start_time
67+
count += 1
68+
else:
69+
# 대기 큐가 비었다면, 다음 작업이 올 때까지 기다려야 한다.
70+
end_time = jobs[i][0]
71+
72+
return return_time // job_len
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
BOJ #2468. 안전 영역 (실버2)
3+
https://www.acmicpc.net/problem/2468
4+
유형: Graph, DFS, BFS
5+
"""
6+
7+
"""
8+
DFS 풀이
9+
"""
10+
import sys
11+
sys.setrecursionlimit(100000)
12+
13+
input = sys.stdin.readline
14+
15+
16+
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
17+
def dfs(x, y):
18+
visited[x][y] = True
19+
for dx, dy in directions:
20+
nx, ny = x + dx, y + dy
21+
if 0 <= nx < N and 0 <= ny < N and \
22+
not visited[nx][ny] and map[nx][ny] > h:
23+
dfs(nx, ny)
24+
25+
26+
N = int(input())
27+
map = [list(map(int, input().split())) for _ in range(N)]
28+
max_height = 0
29+
30+
# 맵 내의 최대값 구하기
31+
for m in map:
32+
max_height = max(max_height, max(m))
33+
34+
answer = 0
35+
36+
for h in range(0, max_height + 1): # 물이 잠기지 않는 상황을 고려하여 0부터 시작한다.
37+
visited = [[False for _ in range(N)] for _ in range(N)]
38+
count = 0
39+
40+
for i in range(N):
41+
for j in range(N):
42+
if not visited[i][j] and map[i][j] > h:
43+
dfs(i, j)
44+
count += 1
45+
46+
answer = max(answer, count)
47+
48+
print(answer)
49+
50+
"""
51+
BFS 풀이
52+
"""
53+
from collections import deque
54+
55+
n = int(input())
56+
graph = []
57+
maxNum = 0
58+
59+
for i in range(n):
60+
graph.append(list(map(int, input().split())))
61+
for j in range(n):
62+
if graph[i][j] > maxNum:
63+
maxNum = graph[i][j]
64+
65+
dx = [0, 0, 1, -1]
66+
dy = [1, -1, 0, 0]
67+
68+
69+
def bfs(a, b, value, visited):
70+
q = deque()
71+
q.append((a, b))
72+
visited[a][b] = 1
73+
74+
while q:
75+
x, y = q.popleft()
76+
77+
for i in range(4):
78+
nx = x + dx[i]
79+
ny = y + dy[i]
80+
if 0 <= nx < n and 0 <= ny < n:
81+
if graph[nx][ny] > value and visited[nx][ny] == 0:
82+
visited[nx][ny] = 1
83+
q.append((nx, ny))
84+
85+
86+
result = 0
87+
for i in range(maxNum):
88+
visited = [[0] * n for i in range(n)]
89+
cnt = 0
90+
91+
for j in range(n):
92+
for k in range(n):
93+
if graph[j][k] > i and visited[j][k] == 0:
94+
bfs(j, k, i, visited)
95+
cnt += 1
96+
97+
if result < cnt:
98+
result = cnt
99+
100+
print(result)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
PGS #81032. 거리두기 확인하기 (Level 2)
3+
https://school.programmers.co.kr/learn/courses/30/lessons/81302
4+
유형: Brute Force, Graph, BFS
5+
"""
6+
7+
"""
8+
풀이1: 완전 탐색
9+
"""
10+
11+
12+
def solution(places):
13+
def is_safe(place):
14+
for i in range(5):
15+
for j in range(5):
16+
if place[i][j] != 'P':
17+
continue
18+
19+
for dx, dy in dirs:
20+
ni, nj = i + dx, j + dy
21+
if not (0 <= ni < 5 and 0 <= nj < 5):
22+
continue
23+
if place[ni][nj] != 'P':
24+
continue
25+
26+
dist = abs(dx) + abs(dy)
27+
if dist == 1:
28+
return 0 # 거리 1에서 바로 P면 위반
29+
elif dist == 2:
30+
# 파티션 여부 확인
31+
if dx == 0: # 수평
32+
if place[i][j + dy // 2] != 'X':
33+
return 0
34+
elif dy == 0: # 수직
35+
if place[i + dx // 2][j] != 'X':
36+
return 0
37+
else: # 대각선
38+
if place[i][nj] != 'X' or place[ni][j] != 'X':
39+
return 0
40+
return 1
41+
42+
dirs = [
43+
(-1, 0), (1, 0), (0, -1), (0, 1), # 거리 1
44+
(-2, 0), (2, 0), (0, -2), (0, 2), # 일직선 거리 2
45+
(-1, -1), (-1, 1), (1, -1), (1, 1) # 대각선 거리 2
46+
]
47+
48+
answer = []
49+
for place in places:
50+
answer.append(is_safe(place))
51+
52+
return answer
53+
54+
55+
"""
56+
풀이2: BFS
57+
"""
58+
from collections import deque
59+
60+
61+
def bfs(p):
62+
start = []
63+
64+
for i in range(5): # 시작점이 되는 P 좌표 구하기
65+
for j in range(5):
66+
if p[i][j] == 'P':
67+
start.append([i, j])
68+
69+
for s in start:
70+
queue = deque([s]) # 큐에 초기값
71+
visited = [[0] * 5 for i in range(5)] # 방문 처리 리스트
72+
distance = [[0] * 5 for i in range(5)] # 경로 길이 리스트
73+
visited[s[0]][s[1]] = 1
74+
75+
while queue:
76+
y, x = queue.popleft()
77+
78+
dx = [-1, 1, 0, 0] # 좌우
79+
dy = [0, 0, -1, 1] # 상하
80+
81+
for i in range(4):
82+
nx = x + dx[i]
83+
ny = y + dy[i]
84+
85+
if 0 <= nx < 5 and 0 <= ny < 5 and visited[ny][nx] == 0:
86+
87+
if p[ny][nx] == 'O':
88+
queue.append([ny, nx])
89+
visited[ny][nx] = 1
90+
distance[ny][nx] = distance[y][x] + 1
91+
92+
if p[ny][nx] == 'P' and distance[y][x] <= 1:
93+
return 0
94+
return 1
95+
96+
97+
def solution(places):
98+
answer = []
99+
100+
for i in places:
101+
answer.append(bfs(i))
102+
103+
return answer

0 commit comments

Comments
 (0)