-
Notifications
You must be signed in to change notification settings - Fork 2
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
37-Munbin-Lee #150
37-Munbin-Lee #150
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ν¨μ¨μ± ν μ€νΈ μΌμ΄μ€ μ νμ¬ν
μ£Όμ΄μ§ 쑰건 μΈ μΆκ° μ νμ¬ν μμ΅λλ€.
λ°±μ€λ κ°λ μ νμ¬νμ΄ λ¬΄μμΈμ§ μλ €μ£Όμ§ μλλ°, κ°μΈμ μΌλ‘λ μ‘°κΈ λΆνΈνλ€.
μ λ§ μ νμ¬νμ΄ μλ€λ©΄ μ λ ₯μ΄ 1μ΅ κ° λκ² λ€μ΄μ¬ μλ μλ€λ 건λ°, μ€μ λ‘ κ·Έλ΄ λ¦¬λ μμΌλκΉ...
μ΄μ¨λ μ΄ μ νμ¬νμ λ³΄κ³ μ΅λν ν¨μ¨μ μΌλ‘ ꡬνν΄μΌκ² λ€κ³ μκ°νλ€.
ν¨μ¨μ± ν
μ€νΈμ μ΅λ n = 500
μ΄λΌκ³ λμ΄ μλ€μ !!!!!
μ λ λ¬ΈλΉλμ΄λ λκ°μ΄ BFS, DFS, μ λμ¨ νμΈλ μκ°νμ΄μ.
κ·Όλ° μ λμ¨ νμΈλλ λ무 λμκ°λ κ±°λΌκ³ μκ°νκ³ , DFS BFSλ‘λ κ°λ¨ν νλ¦¬κ² λ€ μκ°ν΄μ DFSλ‘ ν΄κ²° νμλλ€,
λ¬Έμ μ¬λ°λ€μ νν
from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)
temp = 0
def dfs(y, x, visited, land, col_info):
global temp
n_y = len(land)
n_x = len(land[0])
dx = [0,0,-1,1]
dy = [-1,1,0,0]
for dir in range(4):
new_y = y+dy[dir]
new_x = x+dx[dir]
if new_y < 0 or new_y >= n_y: continue
if new_x < 0 or new_x >= n_x: continue
if (new_y, new_x) in visited: continue
if land[new_y][new_x] == 0: continue
visited.add((new_y, new_x))
col_info.add(new_x)
temp += 1
dfs(new_y, new_x, visited, land, col_info)
def solution(land):
global temp
visited = set()
n_row = len(land)
n_col = len(land[0])
oil_info = defaultdict(int)
for row in range(n_row):
for col in range(n_col):
if land[row][col] == 1 and (row, col) not in visited:
temp = 1
col_info = {col,}
visited.add((row,col))
dfs(row, col, visited, land, col_info)
for c in col_info:
oil_info[c] += temp
return max(list(oil_info.values()))
land = [[0, 0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0], [1, 1, 0, 0, 0, 1, 1, 0], [1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 1, 1]]
print(solution(land))
μκ³ νμ ¨μ΅λλ€ λ¬Έμ κΉλνκ³ μ’λ€μ μ λ BFSλ₯Ό κΈ°λ³ΈμΌλ‘ ν΄μ νμμ΄μ
from collections import *
def solution(land):
answer = 0
cols = [0] * len(land[0])
dr = [1,-1,0,0]
dc = [0,0,-1,1]
for row in range(len(land)):
for col in range(len(land[0])):
if land[row][col] == 0:
continue
q = deque([(row, col)])
check_cols = set()
t = 0
land[row][col] = 0
while q:
r, c = q.popleft()
check_cols.add(c)
t += 1
for i in range(4):
nr = r + dr[i]
nc = c + dc[i]
if not 0 <= nr < len(land) or not 0 <= nc < len(land[0]):
continue
if land[nr][nc] == 0:
continue
land[nr][nc] = 0
q.append((nr, nc))
for c in check_cols:
cols[c] += t
return max(cols) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€μ€ μ΄κ±° μ λ²νκΈ° μλ£κ΅¬μ‘° λ λ°°μ λ λ―Έλ‘νμΆ λ¬Έμ λ λΉμ·νλ€μ
κ°λ₯ν κ²½λ‘λ₯Ό λͺ¨λ νμνλ κ±°μλλ°, κ·Έ λλ bfsλ‘ κ΅¬ννλ€μ
κ·Έλμ bfs, dfsλ λ μ¬λ¦΄ μ μμλλ° μ λμ¨ νμΈλλ μ²μ λ€μ΄λ³΄λ κ°λ
μ΄λ€μ ..γ
γ
λλΆμ νλ λ λ°°μκ°λλ€. μκ³ νμ
¨μ΄λ€ -!
π λ¬Έμ λ§ν¬
https://school.programmers.co.kr/learn/courses/30/lessons/250136
βοΈ μμλ μκ°
1μκ°
β¨ μλ μ½λ
λ¬Έμ μ€λͺ
μ΄ νλλ₯Ό κ΄ν΅νλλ‘ μμΆκ΄μ κ½λλ€.
μμΆκ΄μ ν΄λΉ μ΄μ ν¬ν¨λ λͺ¨λ μμ λ©μ΄λ¦¬μ μμ λ₯Ό λ½λλ€.
μμμ μ΄μ μμΆκ΄μ κ½μμ λ, λ½μ μ μλ μ΅λμ μμ λμ?
λ°±μ€λ κ°λ μ νμ¬νμ΄ λ¬΄μμΈμ§ μλ €μ£Όμ§ μλλ°, κ°μΈμ μΌλ‘λ μ‘°κΈ λΆνΈνλ€.
μ λ§ μ νμ¬νμ΄ μλ€λ©΄ μ λ ₯μ΄ 1μ΅ κ° λκ² λ€μ΄μ¬ μλ μλ€λ 건λ°, μ€μ λ‘ κ·Έλ΄ λ¦¬λ μμΌλκΉ...
μ΄μ¨λ μ΄ μ νμ¬νμ λ³΄κ³ μ΅λν ν¨μ¨μ μΌλ‘ ꡬνν΄μΌκ² λ€κ³ μκ°νλ€.
λ¨Όμ , μμ λ©μ΄λ¦¬λ₯Ό μ΄λ»κ² 체ν¬ν μ§ μκ°νλ€.
BFS, DFS, μ λμ¨νμΈλ μ λκ° μκ°λ¬λλ°,
μ΄μ°¨νΌ κ° μ μ ν λ²μ©λ§ λ°©λ¬Ένλ―λ‘
O(landμ λμ΄)
λΌ λ¬΄μ¨ λ°©λ²μ μ ννλ ν¨μ¨μ μΈλ°κ΅³μ΄ μ λμ¨νμΈλλ‘ λμκ° νμλ μλ€κ³ μκ°νκ³ ,
μ¬κ·λ₯Ό μ μ°κ³ ꡬννλ κ² κΉλν κ² κ°μ BFSλ‘ μ²΄ν¬νκΈ°λ‘ νλ€.
κ° μμΆκ΄μ κ½μ λλ§λ€ BFSλ‘ μ²΄ν¬νλ€λ©΄, μ΄λ―Έ νλ μ°μ°μ λ€μ νκΈ° λλ¬Έμ λΉν¨μ¨μ μ΄λ€.
κ·Έλμ μ μ²λ¦¬λ‘ BFSλ₯Ό νλ²λ§ νκΈ°λ‘ νλ€.
λλ΅μ μΈ λ°©λ²μ λ€μκ³Ό κ°λ€.
κ° μ€μΌμ λνμ¬ μ΄λ€ μ²ν¬(λ©μ΄λ¦¬)μΈμ§ λ²νΈλ₯Ό λ§€κΈ°κ³ ,
κ° μ²ν¬μ μ€μΌλμ΄ μΌλ§μΈμ§ μ μ₯νλ κ²μ΄λ€.
μ΄μ μ½λλ‘ κ΅¬νν΄λ³΄μ.
chunks
λ κ° μ μ΄ λͺ λ² μ²ν¬μΈμ§λ₯Ό μ μ₯νλ€. μμ κ° μλλΌλ©΄ -1μ΄λ€.oils
λ κ° μ²ν¬μ μμ λμ μ μ₯νλ€.λͺ¨λ μ μ λν΄ BFSλ₯Ό λλ €
chunks
μoils
λ₯Ό μμ±νλ€.νΉλ³ν μκ³ λ¦¬μ¦μ΄λ μμ΄λμ΄λ₯Ό μ°μ§ μκ³ ν μ μκ² λ§λ μ¬λ°λ λ¬Έμ μΈ κ² κ°λ€.
μ§λ¬Έλ νλ‘κ·Έλλ¨Έμ€λ΅μ§ μκ² κ΅μ₯ν κΉλνλ° PCCP λ¬Έμ λΌμ κ·Έλ°κ°...
λ€μμλ PCCP λ¬Έμ λ₯Ό νμ΄μ λΉκ΅ν΄λ΄μΌκ² λ€.
π μ 체 μ½λ