Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11 ljedd2 #161

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions LJEDD2/2024-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
| 3μ°¨μ‹œ | 2024.07.24 | 깊이/λ„ˆλΉ„ μš°μ„  탐색(DFS/BFS) | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/43164">μ—¬ν–‰κ²½λ‘œ</a> | [#3](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/131) |
| 4μ°¨μ‹œ | 2024.07.27 | κ·Έλž˜ν”„ | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/49189">κ°€μž₯ λ¨Ό λ…Έλ“œ</a> | [#4](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/133) |
| 5μ°¨μ‹œ | 2024.07.31 | λ‹€μ΄λ‚˜λ―Ήν”„λ‘œκ·Έλž˜λ° | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/43105">μ •μˆ˜ μ‚Όκ°ν˜•</a> | [#5](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/139) |
| 6μ°¨μ‹œ | 2024.08.03 | 완전탐색 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/87946">ν”Όλ‘œλ„</a> | [#6](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/141) |
| 7μ°¨μ‹œ | 2024.09.01 | μœ„μƒμ •λ ¬ | <a href="https://www.acmicpc.net/problem/2252">쀄 μ„Έμš°κΈ°</a> | [#7](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/152) |
| 8μ°¨μ‹œ | 2024.09.04 | μœ„μƒμ •λ ¬ | <a href="https://www.acmicpc.net/problem/1766">λ¬Έμ œμ§‘</a> | [#8](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/154) |
| 9μ°¨μ‹œ | 2024.09.07 | μŠ€νƒ | <a href="https://www.acmicpc.net/problem/2176">μ›μˆ­μ΄ 맀달기</a> | [#9](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/156) |
| 10μ°¨μ‹œ | 2024.09.11 | μš°μ„ μˆœμœ„ 큐 | <a href="https://www.acmicpc.net/problem/1715">μΉ΄λ“œ μ •λ ¬ν•˜κΈ°</a> | [#10](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/159) |
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 더 κ°„λ‹¨ν•œ 풀이 ... (λ¬Έμžμ—΄)
import sys
input = sys.stdin.readline

for _ in range(int(input())):
cnt = 0
s = input().rstrip()
while "[]" in s:
s = s.replace("[]", "")
cnt += 1

print(2**cnt)



# import sys

# input = sys.stdin.readline

# for _ in range(int(input())):
# s = input().rstrip() # κ΄„ν˜Έ λ¬Έμžμ—΄

# max_depth = 0 # μ΅œλŒ€ 깊이λ₯Ό μ €μž₯ν•  λ³€μˆ˜
# tree = list() # ν˜„μž¬ μ—΄λ¦° κ΄„ν˜Έλ₯Ό μ €μž₯ν•  μŠ€νƒμž„

# for i in s:
# # μ—¬λŠ” κ΄„ν˜ΈμΌ 경우 μŠ€νƒμ— μ—΄λ¦° κ΄„ν˜Έ μΆ”κ°€
# if i == '[':
# tree.append('[')
# continue
# # λ‹«λŠ” κ΄„ν˜ΈμΌ 경우
# max_depth = max(len(tree), max_depth)
# # ν˜„μž¬ κΉŠμ΄μ™€ μ΅œλŒ€ 깊이 쀑 큰 값을 μ €μž₯ν•˜κ³ 
# tree.pop() # λ§ˆμ§€λ§‰ μ—΄λ¦° κ΄„ν˜Έλ₯Ό 제거

# print(2 ** max_depth) # μŒμ„ μ΄λ£°λ•Œ 가지 생성, 가지2-.1κ°œμ”© λ‚˜λˆ κ°€μ§
# # κ²°κ΅­μ—” 2의 depth승
49 changes: 49 additions & 0 deletions LJEDD2/2024-2/완전탐색/ν”Όλ‘œλ„..py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 첫 번째 μ½”λ“œ
def solution(k, dungeons):

def DFS(k, cnt):
nonlocal answer

answer = max(answer, cnt)
for i in range(len(dungeons)):
# λ˜μ „ = ["μ΅œμ†Œ ν•„μš” ν”Όλ‘œλ„", "μ†Œλͺ¨ ν”Όλ‘œλ„"]
min_fatigue, use_fatigue = dungeons[i][0], dungeons[i][1]

# ν˜„μž¬ ν”Όλ‘œλ„κ°€ ν•΄λ‹Ή λ˜μ „μ„ λ°©λ¬Έν•˜κΈ° μœ„ν•œ μ΅œμ†Œ ν”Όλ‘œλ„λ³΄λ‹€ 클 λ•Œ
if not visited[i] and k >= min_fatigue:
visited[i] = True

# λ°±νŠΈλž˜ν‚Ή : 이전 λ…Έλ“œλ‘œ λ‹€μ‹œ backν•  λ•Œ,
# ν•΄λ‹Ή λ…Έλ“œλ₯Ό λ°©λ¬Έν•˜κΈ° μ „μ˜ ν”Όλ‘œλ„λ‘œ λ‹€μ‹œ 볡ꡬ
DFS(k-use_fatigue, cnt+1)
visited[i] = False

answer = 0
visited = [False] * len(dungeons) # visited
DFS(k, 0)

return answer


# κ°œμ„ λœ μ½”λ“œ
def solution(k, dungeons):
def dfs(k, cnt):
# ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ 값을 κ°±μ‹ ν•  수 μžˆλŠ” λΉ„μ „μ—­ λ³€μˆ˜λ‘œ μ„ μ–Έ
nonlocal answer

answer = max(answer, cnt)

# enumerate()λ₯Ό ν™œμš©ν•˜μ—¬ 각 λ˜μ „μ˜ μΈλ±μŠ€μ™€ 값을 λ™μ‹œμ— 반볡
for i, (min_fatigue, use_fatigue) in enumerate(dungeons):

if not visited[i] and k >= min_fatigue:

visited[i] = True
dfs(k - use_fatigue, cnt + 1)
visited[i] = False

answer = 0
visited = [False] * len(dungeons)
dfs(k, 0)

return answer
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys, heapq
input = sys.stdin.readline

n = int(input())
heap = []

for _ in range(n):
for number in map(int, input().split()):

if len(heap) < n: # 비ꡐ λŒ€μƒμ΄ λͺ¨μžλž„ 경우
heapq.heappush(heap, number) #κ·ΈλŒ€λ‘œ μΆ”κ°€

else:
if number > heap[0]: # 제일 μž‘μ€κ²ƒλ³΄λ‹€ 크면
heapq.heapreplace(heap, number) #μž‘μ€κ±° λΉΌκ³  큰거 λ„£μ–΄μ€Œ
Copy link
Member

@janghw0126 janghw0126 Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μš°μ™€! 파이썬 heapq λͺ¨λ“ˆ μ€‘μ—μ„œ heapreplace ν•¨μˆ˜λΌλŠ” 것이 μžˆμ—ˆκ΅°μš”!
μ •μ€λ‹˜ 덕뢄에 heapreplace ν•¨μˆ˜κ°€ heapμ—μ„œ μ΅œμ†Œκ°’μ„ μ œκ±°ν•˜κ³  item을 μΆ”κ°€ν•˜λŠ” 역할을 ν•œλ‹€λŠ” 것을 μ•Œμ•„κ°‘λ‹ˆλ‹€πŸ«’
λ‹€μŒμ— 저도 ν•œλ²ˆ μ¨λ³΄κ² μŠ΅λ‹ˆλ‹·!


print(heap[0]) # λ§¨μ•žμ— μžˆλŠ”κ²Œ N번째둜 ν°λ†ˆ
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys
import heapq
input = sys.stdin.readline

cards = list(int(input()) for _ in range(int(input().strip()))) # 데이터 μž…λ ₯λ°›μŒ
heapq.heapify(cards) # 리슀트λ₯Ό νž™κ΅¬μ‘°λ‘œ λ°”κΏ”μ£ΌλŠ” ν•¨μˆ˜

result = 0
while len(cards) > 1 :
f = heapq.heappop(cards) # 첫번째 뭉탱이
s = heapq.heappop(cards) # λ‘λ²ˆμ§Έ 뭉탱이

result += f+s

# λ°”κΏ”μΉ˜κΈ° νšŸμˆ˜κ°€ μ΅œμ†Œκ°€ λ˜κ²Œλ”
heapq.heappush(cards, f+s)

print(result)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import heapq
n, m = map(int,input().split())

#νž™ ꡬ쑰둜 λ³€ν™˜
numbers = list(map(int,input().split()))
heapq.heapify(numbers)

for _ in range(m):

x = heapq.heappop(numbers)
y = heapq.heappop(numbers)

# x와 y λ‘˜ λ‹€ κ°’ ꡐ체
heapq.heappush(numbers, x+y)
heapq.heappush(numbers, x+y)

print(sum(numbers))
33 changes: 33 additions & 0 deletions LJEDD2/2024-2/μœ„μƒμ •λ ¬/λ¬Έμ œμ§‘.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# BOJ1766 λ¬Έμ œμ§‘
# μœ„μƒμ •λ ¬ + μš°μ„ μˆœμœ„ 큐

import heapq

n, m = map(int, input().split())
graph = [[] for _ in range(n + 1)]
indegree = [0] * (n + 1) # μ§„μž… 차수 리슀트

for i in range(m):
s, e = map(int, input().split())
graph[s].append(e)
indegree[e] += 1 # μ§„μž… 차수 데이터 μ €μž₯

# λ¬Έμ œμ—μ„œ κ°€λŠ₯ν•œ μ•ž 번호의 λ¬Έμ œλΆ€ν„° ν’€μ–΄μ•Ό 함 -> μš°μ„ μˆœμœ„ν,,?
# 1~N의 λ‚œμ΄λ„ 순 λŒ€λ‘œ

queue = []

# μˆœμ„œ
for i in range(1, n + 1):
if indegree[i] == 0:
heapq.heappush(queue, i) # μ§„μž…μ°¨μˆ˜κ°€ 0인거 λ¨Όμ € λ‚œμ΄λ„λž‘ μ €μž₯

while queue:
now = heapq.heappop(queue)
print(now, end=' ')

for i in graph[now]: # μœ„μƒ μ •λ ¬ μˆ˜ν–‰ , μ§„μž… 차수 μ—†μ• !
indegree[i] -= 1

if indegree[i] == 0:
heapq.heappush(queue, i)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# BOJ2252 쀄 μ„Έμš°κΈ°
from collections import deque

n, m = map(int, input().split())
graph = [[] for _ in range(n + 1)]
indegree = [0] * (n + 1) # μ§„μž… 차수 μ €μž₯ν•  리슀트

for i in range(m):
s,e = map(int, input().split())
graph[s].append(e) # zz
indegree[e] += 1 # μ§„μž… 차수 데이터λ₯Ό μ €μž₯

# Bλ₯Ό ν•˜κΈ° μœ„ν•΄ AλΌλŠ” μž‘μ—…μ„ λ¨Όμ € ν•΄μ•Ό ν•˜λŠ” ꡬ쑰가 μžˆμ„ λ•Œ,
# κ·Έ μž‘μ—… μˆœμ„œλ₯Ό κ΅¬ν•΄μ£ΌλŠ” 것 = μœ„μƒμ •λ ¬

queue = deque()
for i in range(1, n + 1):
if not indegree[i]:
queue.append(i)

while queue: # μœ„μƒ μ •λ ¬ μˆ˜ν–‰ , μ§„μž… 차수 μ—†μ• !
now = queue.popleft()
print(now, end=' ')

for next in graph[now]:
indegree[next] -= 1 # μ§„μž… 차수 0인 정점 큐에 μ‚½μž…
if not indegree[next]:
queue.append(next)