Skip to content

Commit

Permalink
37_pushedrumex (#155)
Browse files Browse the repository at this point in the history
* n+1 카드게임

* 교점에 별 만들기

* 귤 고르기

* 섬 연결하기

* n진법 뒤집기
  • Loading branch information
pushedrumex authored Jul 9, 2024
1 parent e9bf069 commit d6c792c
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
47 changes: 47 additions & 0 deletions week-37/pushedrumex/n+1 카드게임.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from collections import deque

INF = int(1e9)
# 일단 카드를 모두 갖고, 카드를 사용해야할 때 coin 지불
def solution(coin, cards):
global my_cards, cost, n

n = len(cards)
cards = deque(cards)
my_cards = []
cost = [INF] * (n+1)
for _ in range(n//3):
card = cards.popleft()
my_cards.append(card)
cost[card] = 0

answer = 1
while len(cards) > 0:
# 카드 두 장 뽑기
for _ in range(2):
card = cards.popleft()
my_cards.append(card)
cost[card] = 1

_cost, _cards = find()

if _cost > coin:
break

coin -= _cost
for card in _cards:
my_cards.remove(card)

answer += 1
return answer

# 가장 저렴한 비용으로 n+1이 되는 카드 찾기
def find():
_cost = INF
_cards = []
for card in my_cards:
temp = cost[card] + cost[n+1-card]
if _cost > temp:
_cost = temp
_cards = [card, n+1-card]

return (_cost, _cards)
11 changes: 11 additions & 0 deletions week-37/pushedrumex/n진법 뒤집기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def solution(n):
third = []
while n > 0:
third.append(n%3)
n //= 3
answer = 0
m = 0
for k in third[::-1]:
answer += k *(3**m)
m += 1
return answer
34 changes: 34 additions & 0 deletions week-37/pushedrumex/교점에 별 만들기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
INF = int(1e15)
def solution(line):
points = set()
min_x = INF
min_y = INF
max_x = -INF
max_y = -INF
N = len(line)
for i in range(N-1):
A, B, E = line[i]
for j in range(i+1, N):
C, D, F = line[j]
xc = B*F - E*D
yc = E*C - A*F
p = A*D - B*C
if p == 0:
continue
x = xc / p
y = yc / p
int_x = int(x)
int_y = int(y)
if x == int_x and y == int_y:
points.add((int_x, int_y))
min_x, min_y = min(min_x, int_x), min(min_y, int_y)
max_x, max_y = max(max_x, int_x), max(max_y, int_y)

result = [["."] * (abs(max_x-min_x)+1) for _ in range(abs(max_y-min_y)+1)]
for x, y in points:
result[y-min_y][x-min_x] = "*"

answer = []
for row in result:
answer.append("".join(row))
return answer[::-1]
11 changes: 11 additions & 0 deletions week-37/pushedrumex/귤 고르기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def solution(k, tangerine):
count = [0] * (10_000_000+1)
for t in tangerine:
count[t] += 1
count.sort(reverse=True)
i = 0
while k > 0:
k -= count[i]
i += 1

return i
29 changes: 29 additions & 0 deletions week-37/pushedrumex/섬 연결하기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def find_parent(node):
if parent[node] != node:
parent[node] = find_parent(parent[node])
return parent[node]

def union(node1, node2):
parent1 = find_parent(node1)
parent2 = find_parent(node2)
if parent1 < parent2:
parent[parent2] = parent1
else:
parent[parent1] = parent2

def solution(n, costs):
global parent

parent = [0] * n
for i in range(n):
parent[i] = i

answer = 0
costs.sort(key = lambda x: x[2])

for node1, node2, cost in costs:
if find_parent(node1) != find_parent(node2):
union(node1, node2)
answer += cost

return answer

0 comments on commit d6c792c

Please sign in to comment.