Skip to content

Commit 1cadecf

Browse files
authored
Merge branch '24-Algorithm:main' into main
2 parents 08717cb + fc19b01 commit 1cadecf

9 files changed

+276
-0
lines changed

β€Ž.DS_Storeβ€Ž

0 Bytes
Binary file not shown.

β€ŽHongjoo/lv0/평행.pyβ€Ž

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
(1)평행선 -> 같은 기울기
3+
(2) 두 직선이 μΌμΉ˜ν•˜λŠ” κ²½μš°λ„
4+
"""
5+
6+
def solution(dots):
7+
# λͺ¨λ“  μ§μ„ μ˜ 기울기 κ΅¬ν•˜κΈ°
8+
# 1. 점 2개 선택
9+
# 2. 기울기
10+
answer = 0
11+
loops= []
12+
13+
for i in range(len(dots)) :
14+
for j in range(i+1, len(dots)) :
15+
x1 , y1 = dots[i]
16+
x2 , y2 = dots[j]
17+
print(f"### {x1} ,{y1} , {x2} , {y2}")
18+
output= (y1-y2)/(x1-x2+0.0001)
19+
loop = abs(output)
20+
print(f"##{loop}")
21+
for l in loops :
22+
if loop == l :
23+
return 1
24+
loops.append(loop)
25+
print("$" ,loops)
26+
27+
return answer

β€ŽHongjoo/lv2/μ••μΆ•.pyβ€Ž

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
LZW
3+
1. Dic = {seq_length = 1 단어듀 }
4+
5+
2. in Dic ν˜„μž¬ μž…λ ₯ matching 된 단어쀑 κ°€μž₯ max length κ°€μ§„ λ¬Έμžμ—΄ μ°ΎκΈ° => w
6+
3.print(W의 index), input μ—μ„œ 제거 pop
7+
4 μž…λ ₯μ—μ„œ 처리 λ˜μ§€ μ•Šμ€ λ‹€μŒ κΈ€μž c 있으면 -> w+cλ₯Ό 사전에 λ“±μž¬
8+
5.2둜 regress
9+
| condition
10+
1. λŒ€λ¬Έμžλ§Œ
11+
2,dic_index : 1 ~
12+
13+
'''
14+
dic = [0 , "A" , "B" , "C" , "D" ,"E", "F" , "G" , "H" , "I" , "J" , "K" , "L", "M" , "N" , "O", "P", "Q", "R", "S" , "T" , "U" , "V" , "W", "X" , "Y" , "Z"]
15+
def compression_check(left_w , dic):
16+
# 1 . dic에 w 의 μ—¬λΆ€
17+
# 2. W 와 total_c μ°ΎκΈ° : 2point matching 된 단어쀑 κ°€μž₯ max length -
18+
# answer =[] ;
19+
count = 0
20+
i = 0; j = len(left_w) # i : start ,j : end
21+
while i != j and j >= 0: # cκ°€ μ—†μŒ -> 끝
22+
# print("1" ,left_w[i:j] ,i , j)
23+
count+=1
24+
if left_w[i:j]in dic :
25+
print("2" , i, j)
26+
w = left_w[i:j]
27+
c = left_w[j]
28+
dic.append(w+c)
29+
print(w ,c,dic)
30+
idx = dic.index(w)
31+
# answer.append(idx)
32+
print("w_idx" , idx)
33+
# left_w = "" + c # μ΄ˆκΈ°ν™”
34+
i = j #μ—…λ°μ΄νŠΈ
35+
return idx , j
36+
# print("#" , w , c , dic , left_w)
37+
38+
# print("-1")
39+
j=j-1
40+
# print(j)
41+
42+
43+
def solution(msg):
44+
answer = []
45+
46+
47+
# 1. inintialize dic
48+
# dic = [0 , "A" , "B" , "C" , "D" ,"E", "F" , "G" , "H" , "I" , "J" , "K" , "L", "M" , "N" , "O", "P", "Q", "R", "S" , "T" , "U" , "V" , "W", "X" , "Y" , "Z"] # total 26 + "0" = 1 -> 1~ 26 only valid
49+
#2.
50+
a = 0 ; c ="a"
51+
answer = []
52+
a , j_c = compression_check(msg , dic)
53+
answer.append(a)
54+
55+
while j_c > 0 :
56+
a,j_c = compression_check(msg[j_c:] , dic)
57+
print(a)
58+
59+
# print("####", answer)
60+
return answer
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
n = int(input())
2+
origin = [int(n) for n in input().split()] # 4,1,5,2,3
3+
m = int(input())
4+
compare = [int(m) for m in input().split()] # 1,3,7,9,5
5+
6+
origin.sort() # 1,2,3,4,5
7+
8+
for i in range(m): #
9+
tag = False
10+
low = 0
11+
high = len(origin) - 1
12+
target = compare[i] # 5
13+
while low <= high:
14+
mid = (low + high) // 2
15+
if origin[mid] == target:
16+
tag = True
17+
print(1)
18+
break
19+
elif origin[mid] > target:
20+
high = mid - 1
21+
else:
22+
low = mid + 1
23+
24+
if tag is False:
25+
print(0)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# PGS. 2024 KAKAO WINTER INTERNSHIP - κ°€μž₯ 많이 받은 μ„ λ¬Ό
2+
def solution(friends, gifts):
3+
gift_map = {} # 각 친ꡬ 쌍 κ°„ μ„ λ¬Ό 기둝
4+
send_count = {} # μ„ λ¬Ό μ€€ 횟수 λ”•μ…”λ„ˆλ¦¬
5+
receive_count = {} # μ„ λ¬Ό 받은 횟수 λ”•μ…”λ„ˆλ¦¬
6+
gift_count = {} # μ„ λ¬Ό μ§€μˆ˜ λ”•μ…”λ„ˆλ¦¬
7+
next_month_gift_cnt = {} # λ‹€μŒ 달에 받을 μ„ λ¬Όμ˜ 개수λ₯Ό μΉœκ΅¬λ“€λ³„λ‘œ μ €μž₯ν•˜λŠ” λ”•μ…”λ„ˆλ¦¬
8+
9+
for friend in friends: # μ΄ˆκΈ°ν™”
10+
gift_map[friend] = {}
11+
send_count[friend] = 0
12+
receive_count[friend] = 0
13+
gift_count[friend] = 0
14+
next_month_gift_cnt[friend] = 0
15+
16+
# μ„ λ¬Ό 기둝 뢄석(각 친ꡬ 간에 λˆ„κ°€ λͺ‡ 번 선물을 μ£Όκ³ λ°›μ•˜λŠ”μ§€)
17+
for gift in gifts:
18+
sender, receiver = gift.split()
19+
if receiver not in gift_map[sender]: # ν•΄λ‹Ή 친ꡬ κ°„μ˜ 기둝이 μ—†λ‹€λ©΄ 0으둜 μ΄ˆκΈ°ν™”
20+
gift_map[sender][receiver] = 0
21+
if sender not in gift_map[receiver]: # ν•΄λ‹Ή 친ꡬ κ°„μ˜ 기둝이 μ—†λ‹€λ©΄ 0으둜 μ΄ˆκΈ°ν™”
22+
gift_map[receiver][sender] = 0
23+
gift_map[sender][receiver] += 1 # senderκ°€ receiverμ—κ²Œ μ„ λ¬Όν•œ 횟수 +1
24+
25+
send_count[sender] += 1 # senderκ°€ μ€€ μ„ λ¬Ό 횟수 +1
26+
receive_count[receiver] += 1 # receiverκ°€ 받은 μ„ λ¬Ό 횟수 +1
27+
28+
# μ„ λ¬Ό μ§€μˆ˜ 계산
29+
for friend in friends:
30+
# μ„ λ¬Όμ§€μˆ˜: μ€€ μ„ λ¬Ό 수 - 받은 μ„ λ¬Ό 수
31+
gift_count[friend] = send_count[friend] - receive_count[friend]
32+
33+
# λͺ¨λ“  친ꡬ μŒμ„ λΉ„κ΅ν•˜μ—¬ λ‹€μŒ 달에 받을 μ„ λ¬Ό 수 계산
34+
for i in range(len(friends)):
35+
for j in range(i + 1, len(friends)):
36+
A = friends[i]
37+
B = friends[j]
38+
if gift_map[A].get(B, 0) > gift_map[B].get(A, 0): # A>B이면, Aκ°€ λ‹€μŒλ‹¬μ— λ°›μŒ
39+
next_month_gift_cnt[A] += 1
40+
elif gift_map[A].get(B, 0) < gift_map[B].get(A, 0): #A<B이면, Bκ°€ λ‹€μŒλ‹¬μ— λ°›μŒ
41+
next_month_gift_cnt[B] += 1
42+
else: # 두 μ‚¬λžŒμ΄ 주고받은 μˆ˜κ°€ κ°™λ‹€λ©΄, μ„ λ¬Ό μ§€μˆ˜κ°€ 더 큰 μ‚¬λžŒμ΄ λ°›μŒ
43+
if gift_count[A] > gift_count[B]:
44+
next_month_gift_cnt[A] += 1
45+
elif gift_count[A] < gift_count[B]:
46+
next_month_gift_cnt[B] += 1
47+
# μ„ λ¬Ό μ§€μˆ˜λ„ κ°™λ‹€λ©΄ 아무도 λ°›μ§€ μ•ŠμŒ
48+
49+
# κ°€μž₯ 많이 받을 친ꡬ λ°˜ν™˜
50+
return max(next_month_gift_cnt.values())
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
μ–‘ λͺ¨μ€ μˆ˜λ³΄λ‹€ λŠ‘λŒ€μ˜ μˆ˜κ°€ κ°™κ±°λ‚˜ λ§Žμ•„μ§€λ©΄ μ’…λ£Œλ¨
3+
λΆ€λͺ¨ λ…Έλ“œλŠ” λ°©λ¬Έν–ˆμ§€λ§Œ μžμ‹ λ…Έλ“œλŠ” λ°©λ¬Έν•˜μ§€ μ•Šμ•˜μ„ λ•Œ λ°©λ¬Έν•  수 있음
4+
5+
'''
6+
7+
8+
def solution(info, edges):
9+
# info - 0: μ–‘, 1: λŠ‘λŒ€
10+
global answer
11+
answer = 0
12+
visited = [False] * len(info)
13+
visited[0] = True # 0번 λ…Έλ“œλŠ” 항상 양이 있음
14+
15+
def dfs(sheep, wolf):
16+
global answer
17+
18+
if wolf < sheep: # 양이 λŠ‘λŒ€λ³΄λ‹€ 더 많으면 λͺ¨μ„ 수 μžˆλŠ” μ΅œλŒ€ μ–‘ 마리 수 μ—…λ°μ΄νŠΈ
19+
answer = max(answer, sheep)
20+
else: # λŠ‘λŒ€κ°€ μ–‘μ˜ μˆ˜μ™€ κ°™κ±°λ‚˜ 더 λ§Žμ„ 경우 μ’…λ£Œλ¨
21+
return
22+
23+
for parent, child in edges:
24+
# λΆ€λͺ¨ λ…Έλ“œλŠ” λ°©λ¬Έν–ˆμ§€λ§Œ μžμ‹ λ…Έλ“œλŠ” λ°©λ¬Έν•˜μ§€ μ•Šμ•˜μ„ λ•Œ
25+
if visited[parent] and not visited[child]:
26+
visited[child] = True
27+
if info[child] == 0: # 양이라면 μ–‘ 마리 수 + 1
28+
dfs(sheep + 1, wolf)
29+
else: # λŠ‘λŒ€λΌλ©΄ λŠ‘λŒ€ 마리 수 + 1
30+
dfs(sheep, wolf + 1)
31+
visited[child] = False
32+
33+
dfs(1, 0) # (sheep, wolf)
34+
35+
return answer
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
n = int(input()) # 수 μž…λ ₯ λ°›κΈ°
5+
t, p = [0 for _ in range(n + 1)], [0 for _ in range(n + 1)]
6+
for i in range(1, n + 1):
7+
t[i], p[i] = map(int, input().split())
8+
dp = [0] * (n+1) #dp 리슀트 μ΄ˆκΈ°ν™”
9+
10+
for i in range(1, n+1):
11+
time = t[i]
12+
pay = p[i]
13+
day = i + time - 1
14+
dp[i] = max(dp[i], dp[i - 1]) # μ΄μ „κΉŒμ§€μ˜ μ΅œλŒ“κ°’
15+
if day <= n:
16+
dp[day] = max(dp[day], dp[i-1]+pay)
17+
18+
print(max(dp))
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
5+
def check_winner(x, y, color):
6+
# 였λͺ© 4개 λ°©ν–₯
7+
directions = [(-1, 0), (0, -1), (-1, -1), (-1, 1)]
8+
# (-1, 0) - μ„Έλ‘œ λ°©ν–₯ (μœ„μͺ½)
9+
# (0, -1) - κ°€λ‘œ λ°©ν–₯ (μ™Όμͺ½)
10+
# (-1, -1) - μ’Œμƒν–₯ λŒ€κ°μ„  λ°©ν–₯
11+
# (-1, 1) - μš°μƒν–₯ λŒ€κ°μ„  λ°©ν–₯
12+
13+
for dx, dy in directions:
14+
count = 1 # ν˜„μž¬ 돌이 ν¬ν•¨λœ μ—°μ†λœ 돌의 개수λ₯Ό μ…€ λ³€μˆ˜ μ΄ˆκΈ°ν™”
15+
16+
# μ£Όμ–΄μ§„ λ°©ν–₯으둜 μ—°μ†λœ 돌 개수 μ„ΈκΈ°
17+
nx, ny = x + dx, y + dy
18+
while 1 <= nx < 20 and 1 <= ny < 20 and board[nx][ny] == color:
19+
count += 1
20+
# λ°©ν–₯ dx, dy둜 μ΄λ™ν•˜λ©° ν•΄λ‹Ή λ°©ν–₯에 같은 μƒ‰μ˜ 돌이 μžˆλŠ” ν•œ 계속 탐색
21+
nx += dx
22+
ny += dy
23+
24+
# λ°˜λŒ€ λ°©ν–₯μœΌλ‘œλ„ μ—°μ†λœ 돌 개수 μ„ΈκΈ°
25+
nx, ny = x - dx, y - dy
26+
while 1 <= nx < 20 and 1 <= ny < 20 and board[nx][ny] == color:
27+
count += 1
28+
# λ°˜λŒ€ λ°©ν–₯μœΌλ‘œλ„ μ΄λ™ν•˜μ—¬ μ—°μ†λœ 돌의 개수λ₯Ό μΆ”κ°€λ‘œ μ…ˆ
29+
nx -= dx
30+
ny -= dy
31+
32+
# 5개일 경우만 승리둜 인정
33+
if count == 5:
34+
return True
35+
36+
return False # 5개의 돌이 μ—°μ†λ˜μ§€ μ•Šμ„ 경우
37+
38+
N = int(input().strip()) # 돌의 개수
39+
board = [[0] * 20 for _ in range(20)] # 19x19 λ°”λ‘‘νŒ / λ°”λ‘‘λŒμ΄ 놓인 μœ„μΉ˜ 기둝 (1,1)~(19,19)
40+
41+
# μž…λ ₯된 μ’Œν‘œμ— λŒμ„ 기둝
42+
for i in range(N):
43+
x, y = map(int, input().strip().split()) # 각 수의 μ’Œν‘œ
44+
color = 1 if i % 2 == 0 else 2 # ν™€μˆ˜ 번째 수: ν‘λŒ(1), 짝수 번째 수: 백돌(2)
45+
board[x][y] = color # ν•΄λ‹Ή μ’Œν‘œμ— λŒμ„ λ†“μŒ (λ°”λ‘‘νŒ 배열에 기둝)
46+
47+
if check_winner(x, y, color): # ν˜„μž¬ 놓은 돌둜 인해 μŠΉλ¦¬κ°€ κ²°μ •λ˜λŠ”μ§€ 확인
48+
print(i + 1) # μŠΉλ¦¬κ°€ κ²°μ •λ˜λ©΄ ν˜„μž¬ λͺ‡ 번째 μˆ˜μΈμ§€ 좜λ ₯ν•˜κ³  ν”„λ‘œκ·Έλž¨ μ’…λ£Œ
49+
sys.exit()
50+
51+
print(-1) # λͺ¨λ“  λŒμ„ λ†“μ•˜μ§€λ§Œ μŠΉνŒ¨κ°€ κ²°μ •λ˜μ§€ μ•ŠμœΌλ©΄ -1 좜λ ₯
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
K = int(input())
2+
stack = []
3+
for i in range(K):
4+
num = int(input())
5+
stack.append(num)
6+
if num == 0:
7+
stack.pop()
8+
stack.pop()
9+
10+
print(sum(stack))

0 commit comments

Comments
Β (0)