Skip to content

Commit dbfd162

Browse files
17_pushedrumex (#98)
* feat: 코딩테스트 연습 퍼즐 조각 채우기 * 🍊귤 고르기 * 콜라문제 * 블록게임 * (효율성 fail)무지의 먹방 라이브 * (success)무지의 먹방 라이브 * (fail)길 찾기 게임 * (파일 이동)무지의 먹방 라이브 * (파일 이동)무지의 먹방 라이브 * 길찾기 게임 --------- Co-authored-by: bkRyusim <[email protected]>
1 parent 2a1255e commit dbfd162

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

Diff for: week-17/pushedrumex/귤 고르기.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from collections import defaultdict
2+
3+
def solution(k, tangerine):
4+
dic = defaultdict(int)
5+
6+
for t in tangerine:
7+
dic[t] += 1
8+
9+
answer = 0
10+
for t in sorted(dic.keys(), key=lambda t: dic[t], reverse=True):
11+
k -= dic[t]
12+
answer += 1
13+
if k <= 0: break
14+
15+
return answer

Diff for: week-17/pushedrumex/길 찾기 게임.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import sys
2+
sys.setrecursionlimit(10**9)
3+
4+
graph = {}
5+
nums = {}
6+
answer = [[], []]
7+
def solution(nodeinfo):
8+
N = len(nodeinfo)
9+
for i in range(N):
10+
nodeinfo[i] = tuple(nodeinfo[i])
11+
12+
for i in range(N):
13+
nums[nodeinfo[i]] = i+1
14+
graph[nodeinfo[i]] = [None, None]
15+
16+
nodeinfo.sort(key=lambda x: -x[1])
17+
root = nodeinfo[0]
18+
for i in range(1, N):
19+
make_tree(root, nodeinfo[i])
20+
21+
preorder(root)
22+
postorder(root)
23+
24+
return answer
25+
26+
def make_tree(parent, child):
27+
if child[0] < parent[0]:
28+
if graph[parent][0] == None:
29+
graph[parent][0] = child
30+
else:
31+
make_tree(graph[parent][0], child)
32+
elif parent[0] < child[0]:
33+
if graph[parent][1] == None:
34+
graph[parent][1] = child
35+
else:
36+
make_tree(graph[parent][1], child)
37+
38+
# Root -> L -> R
39+
def preorder(node):
40+
left, right = graph[node]
41+
answer[0].append(nums[node])
42+
if left != None:
43+
preorder(left)
44+
if right != None:
45+
preorder(right)
46+
47+
# L -> R -> Root
48+
def postorder(node):
49+
left, right = graph[node]
50+
if left != None:
51+
postorder(left)
52+
if right != None:
53+
postorder(right)
54+
55+
answer[1].append(nums[node])

Diff for: week-17/pushedrumex/무지의 먹방 라이브.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 파이썬 1초에 2000만
2+
# 이분탐색 O(logN)
3+
# 200,000 * log(100,000,000) = 5,200,000
4+
def solution(food_times, k):
5+
left, right = 1, int(1e8)
6+
N = len(food_times)
7+
rotate, t = 0, 0
8+
times = sorted(food_times)
9+
while left <= right:
10+
mid = (left + right) // 2
11+
temp = N * mid
12+
for time in times:
13+
if time >= mid:
14+
break
15+
temp -= mid - time
16+
17+
if temp <= k:
18+
rotate = mid
19+
t = temp
20+
left = mid + 1
21+
else:
22+
right = mid - 1
23+
24+
k -= t
25+
for i in range(len(food_times)):
26+
food_times[i] -= rotate
27+
if food_times[i] > 0:
28+
k -= 1
29+
if k < 0:
30+
return i + 1
31+
32+
return -1

Diff for: week-17/pushedrumex/블록게임.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 일단 위쪽에 비어있는 부분들을 검은 블록으로 꽉 채움
2+
# 꽉착 직사각형이 있는 지 탐색
3+
# 있다면 제거하고 1번 부터 재시작
4+
# 없다면 끝
5+
board = None
6+
N, M = None, None
7+
def solution(_board):
8+
global board, N, M
9+
board = _board
10+
11+
N, M = len(board), len(board[0])
12+
13+
answer = 0
14+
flag = True
15+
while flag:
16+
flag = False
17+
for j in range(M):
18+
for i in range(N):
19+
if board[i][j] > 0: break
20+
board[i][j] = -1 # 검은 블록
21+
22+
for i in range(N):
23+
for j in range(M):
24+
if isMatch(i, j, 2, 3):
25+
remove(i, j, 2, 3)
26+
flag = True
27+
answer += 1
28+
elif isMatch(i, j, 3, 2):
29+
remove(i, j, 3, 2)
30+
flag = True
31+
answer += 1
32+
33+
return answer
34+
35+
def isMatch(x, y, x_size, y_size):
36+
num = None
37+
block = 0
38+
for i in range(x, x+x_size):
39+
for j in range(y, y+y_size):
40+
if oob(i, j) or board[i][j] == 0: return False
41+
if board[i][j] == -1: continue
42+
block += 1
43+
if num == None:
44+
num = board[i][j]
45+
elif num != board[i][j]:
46+
return False
47+
48+
return block == 4
49+
50+
def remove(x, y, x_size, y_size):
51+
for i in range(x, x+x_size):
52+
for j in range(y, y+y_size):
53+
board[i][j] = 0
54+
55+
def oob(x, y):
56+
return not (0 <= x < N and 0 <= y < M)
57+

Diff for: week-17/pushedrumex/콜라문제.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def solution(a, b, n):
2+
answer = 0
3+
while n >= a:
4+
cola = n // a * b
5+
answer += cola
6+
n = n % a + cola
7+
8+
return answer

0 commit comments

Comments
 (0)