1import heapq 2 3def time_calc(time): 4 time_split = time.split(':') 5 return int(time_split[0])*60 + int(time_split[1]) 6 7def solution(book_time): 8 time = [] 9 q = [] 10 for start, end in book_time: 11 time.append((time_calc(start), time_calc(end))) 12 time.sort(key=lambda x: x[0]) 13 print (time) 14 for start, end in time: 15 if q: 16 top = heapq.heappop(q) 17 if top > start: 18 heapq.heappush(q, top) 19 heapq.heappush(q, end+10) 20 else: 21 heapq.heappush(q, end+10) 22 return len(q) ๋ฌธ์ ํธํ
์ ์์ฝ์๊ฐ์ด ๋ด๊ธด 2์ฐจ์ ๋ฐฐ์ด์ด ์ฃผ์ด์ง๋ค ex) [[“12:00”, “12:30”], [“15:00”, “16:00”]] ํด์ค ์๊ฐ 10๋ถ ์ดํ์ ๋ค์ ์๋์ด ์
์ค ํ ์ ์๋ค ์ต์ ๊ฐ์ค์ ๊ฐ์๋ฅผ ๊ตฌํ์ฌ๋ผ TC input [[“15:00”, “17:00”], [“16:40”, “18:20”], [“14:20”, “15:20”], [“14:10”, “19:20”], [“18:20”, “21:20”]]
1from collections import deque 2 3vx = [0, 0, -1, 1] 4vy = [-1, 1, 0, 0] 5 6def bfs(maps, N, M, p1, p2): 7 visited = [[False]*M for _ in range(N)] 8 q = deque() 9 q.append(list(p1)+[0]) 10 11 while q: 12 curY, curX, cnt = q.popleft() 13 14 if p2 == (curY, curX): 15 return cnt 16 17 for i in range(4): 18 x = curX + vx[i] 19 y = curY + vy[i] 20 if 0 <= x < M and 0 <= y < N and not visited[y][x] and maps[y][x] !
1V = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)], [(-1, 1), (1, -1)]] 2P = [[(1, 0), (1, 1), (1, 2)], [(0, 1), (1, 1), (2, 1)], [(1, 1)], [(1, 1)]] 3 4def solution(board): 5 answer = -1 6 o_line = 0 7 x_line = 0 8 9 for i, p_list in enumerate(P): 10 for p in p_list: 11 if board[p[0]][p[1]] != '.' and board[p[0]][p[1]]== board[p[0]+V[i][0][0]][p[1]+V[i][0][1]] == board[p[0]+V[i][1][0]][p[1]+V[i][1][1]]: 12 if board[p[0]][p[1]] == 'O': 13 o_line += 1 14 elif board[p[0]][p[1]] == 'X': 15 x_line += 1 16 17 o_cnt = 0 18 x_cnt = 0 19 20 21 for i in ''.
1def solution(m, n, startX, startY, balls): 2 answer = [] 3 for bx, by in balls: 4 tmp = [] 5 for x, y in [(-1*startX, startY), (startX, -1*startY), (startX, 2*n-startY), (2*m-startX, startY)]: 6 if bx==x and (y < 0 < by < startY or startY < by < n < y): 7 continue 8 if by==y and (x < 0 < bx < startX or startX < bx < m < x): 9 continue 10 tmp.
1from collections import deque 2visited = [] 3vx = [0, 0, -1, 1] 4vy = [-1, 1, 0, 0] 5 6def bfs(board, N, M, sp): 7 global visited 8 q = deque() 9 q.append(sp+[1]) 10 11 while q: 12 cy, cx, cnt = q.popleft() 13 visited[cy][cx] = True 14 for i in range(4): 15 x = cx 16 y = cy 17 while True: 18 x += vx[i] 19 y += vy[i] 20 if not (0 <= x < N and 0 <= y < M) or board[y][x] == 'D': 21 x -= vx[i] 22 y -= vy[i] 23 break 24 if board[y][x] == 'G': 25 return cnt 26 if not visited[y][x]: 27 q.
1def solution(picks, minerals): 2 bundles = [] 3 tmp = [0, 0, 0] 4 DATA = [[1, 1, 1], [5, 1, 1], [25, 5, 1]] 5 answer = 0 6 7 for i in range(len(minerals)): 8 if minerals[i] == "diamond": 9 for j in range(3): 10 tmp[j] += DATA[j][0] 11 elif minerals[i] == "iron": 12 for j in range(3): 13 tmp[j] += DATA[j][1] 14 elif minerals[i] == "stone": 15 for j in range(3): 16 tmp[j] += DATA[j][2] 17 18 if (i+1) % 5 == 0 or i == len(minerals)-1: 19 bundles.
1def solution(sequence, k): 2 answer = [] 3 e = len(sequence)-1 4 s = len(sequence) 5 cur = 0 6 7 while s >= 0: 8 if cur < k: 9 s -= 1 10 cur += sequence[s] 11 elif cur > k: 12 cur -= sequence[e] 13 e -= 1 14 else: 15 answer.append((s, e)) 16 s -= 1 17 cur += sequence[s] 18 answer.sort(key=lambda x: (x[1]-x[0], x[0])) 19 return answer[0] ๋ฌธ์ ์์ด๊ณผ k๊ฐ ์ฃผ์ด์ง๋ค ์์ด์ ๋ถ๋ถํฉ์ด k๊ฐ ๋๊ฒ ํ๋ ์์์ธ๋ฑ์ค์ ๋์ธ๋ฑ์ค๋ฅผ ๊ตฌํ๋ผ ์ด๋, ๊ธธ์ด๊ฐ ์งง์ ์์ด์ ์ฐพ๋๋ค, ๊ธธ์ด๊ฐ ๊ฐ์๊ฒ์ด ์ฌ๋ฌ๊ฐ์ง๋ผ๋ฉด ์์์ธ๋ฑ์ค๊ฐ ์์ ๊ฒ์ ์ฐพ๋๋ค TC input [1, 2, 3, 4, 5], 7
1def solution(targets): 2 answer = 0 3 targets.sort(key=lambda x:x[1]) 4 cur = 0 5 6 for i in targets: 7 if i[0] >= cur: 8 answer += 1 9 cur = i[1] 10 11 return answer ๋ฌธ์ ๊ฐ๊ตฌ๊ฐ (s, e)์ ๋ฆฌ์คํธ๊ฐ ์ฃผ์ด์ง๋ค. ์ด๋, ๋ชจ๋ ๊ฐ๊ตฌ๊ฐ์ ํฌํจํ๋ ์ต์ ์ซ์์ ์๋ฅผ ๊ตฌํ์ฌ๋ผ TC input [[4,5],[4,8],[10,14],[11,13],[5,12],[3,7],[1,4]]
ouput 3
ํด๊ฒฐ๋ฐฉ๋ฒ ๊ฐ๊ตฌ๊ฐ์ (s, e)์์ e๋ฅผ ์ ๋ ฌํ์ฌ ํด๊ฒฐํ์๋ค.