-
Notifications
You must be signed in to change notification settings - Fork 1
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
9-fnzksxl #37
Conversation
λ¬Έμ μ€λͺ λ§ λ³΄λ©΄ λμ ν κ·Έλν λ¬Έμ λ‘ μ보μ΄λλ°, μκ³ λ³΄λ νλ‘μ΄λ-μμ λ¬Έμ λ λ§μ΄μ£ ... μ λ μ΄κ±° μ²μν λ κ·Έλν μΉ΄ν κ³ λ¦¬λΌκ³ νκΈ° μν΄μ€¬μΌλ©΄ κ°νΌλ λͺ»μ‘μμ κ² κ°μμ. μ μ μμ μΈ νλ‘μ΄λ-μμ νμ΄λ‘ μ κ·Όνμ΅λλ€. λμ μ¬κΈ°μλ μ΅λ¨ 거리λ₯Ό ꡬνλ κ²μ΄ μλ, "οΏ½iκ° kλ₯Ό μ΄κ²Όκ³ kκ° jλ₯Ό μ΄κ²ΌμΌλ©΄ iλ jλ₯Ό μ΄κΈ΄ κ±°λ€!"μ κ°μ΄ κ΄κ³ λ₯Ό νμ₯ν΄κ°λ κ°λ μ΄λ μ΄ λΆλΆλ§ μ΄μ§ λ€λ₯΄κ² λ€μ. def solution(n, results):
graph = [[float('inf')] * (n + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
graph[i][i] = 0
for A, B in results:
graph[A][B] = 1 # Aλ Bλ₯Ό μ΄κ²ΌμΌλ―λ‘ 1
graph[B][A] = -1 # Bλ Aμκ² μ‘μΌλ―λ‘ -1
for k in range(1, n + 1):
for i in range(1, n + 1):
for j in range(1, n + 1):
if graph[i][k] == 1 and graph[k][j] == 1: # iκ° kλ₯Ό μ΄κ²Όκ³ , kκ° jλ₯Ό μ΄κ²Όλ€λ©΄
graph[i][j] = 1 # iλ jλ₯Ό μ΄κΈ΄κ±°λ€.
elif graph[i][k] == -1 and graph[k][j] == -1: # λ°λ κ²½μ°
graph[i][j] = -1
answer = 0
for i in range(1, n + 1): # 맀 iλ²μ§Έ μ¬λλ§λ€
if float('inf') not in graph[i][1:]: # κ΄κ³λ₯Ό μ μ μλ(inf) κ²½μ°κ° μλ€λ©΄
answer += 1 # μΉ΄μ΄νΈ + 1
return answer νν dictionaryλ₯Ό μ¨μ ν¨μ¬ κ°λ¨νκ² νΌ μ¬λμ΄ μλ€μ... updateλ ν¨μκ° μμ΄μ μκ° λ€ ν΄μ£Όλ€... from collections import defaultdict
def solution(n, results):
win, lose = defaultdict(set), defaultdict(set)
for A, B in results:
win[A].add(B)
lose[B].add(A)
for i in range(1, n + 1):
for winner in lose[i]: # iλ² μ μμκ² μ΄κΈ΄ μ μλ€
win[winner].update(win[i])
for loser in win[i]: # iλ² μ μμκ² μ§ μ μλ€
lose[loser].update(lose[i])
answer = 0
for i in range(1, n + 1):
if len(win[i]) + len(lose[i]) == n - 1:
answer += 1
return answer |
@9kyo-hwang μ μ²λΌ setμ μ¨μ νΈμ λΆμ΄λ€μ© defaultdictλ‘ set μλ£νμ λΉ μνλ‘ μ΄κΈ°νν΄μ μ°μ νμ΄ κ°μ΅λλ€. μ λ updateλ‘ ν λ²μ μμ μ€κΉλΌκ³ μκ°μ ν΄λ΄€λλ° μ νμ΄ λκ² κΉλνκ΅°μ.. κ·Έλ°λ° μ‘μ λλ₯Ό -1λ‘ λ€λ₯΄κ² νκΈ°νμ μ΄μ κ° μμκΉμ? |
γ
μ΄κΈ΄ κ±°λ μ§ κ±°λ₯Ό ꡬλΆν λ €κ³ ν 건λ°, μκ°ν΄λ³΄λ κ·Έλ΄ νμκ° μμκ΅°μ? |
@9kyo-hwang κ΅ν©λμ΄λΌ μ΄λ€ ν° λ»μ΄ μ¨μ΄μλ μ€ μμμ΅λλ€ γ γ γ γ γ γ |
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.
νλ‘μ΄λ μμ
κΈ°μ΅μ΄ μλμ κ΅ν©μ΄κΊΌ λ³΄κ³ νμλ€μ..γ
γ
μκΈ°ν΄μ μ’μμ΅λλ€ κ΅Ώ..
μ΄κΈ°κ³ μ΄κ²Όμ λ μ
λ, μ§κ³ μ‘μ λ μ
λ μ΄κ±Έ μκ° λͺ»νλ€μ
const val INFF = 100_000_000
class Solution {
fun solution(n: Int, results: Array<IntArray>): Int {
val graph = Array(n + 1) { i ->
Array(n + 1) { j -> if (i == j) 0 else INFF }
}
results.forEach {
graph[it[0]][it[1]] = 1
graph[it[1]][it[0]] = -1
}
for (k in 1..n) {
for (a in 1..n) {
for (b in 1..n) {
if (graph[a][k] == 1 && graph[k][b] == 1) {
graph[a][b] = 1
} else if (graph[a][k] == -1 && graph[k][b] == -1) {
graph[a][b] = -1
}
}
}
}
val answer = mutableListOf<Int>()
for (i in 1..n) {
var isNotINFF = true
for (j in 1..n) {
if (graph[i][j] != INFF) {
continue
}
isNotINFF = false
break
}
if (isNotINFF) {
answer.add(i)
}
}
return answer.count()
}
}
μ΄ λ¬Έμ μ λν΄μ PRμ΄ μ΄μ μ μ¬λΌμμλλ°, μ λ νλ‘μ΄λ μμ μΈμ§λ λͺ°λμ΄μ.. μ λ μ½κ° λ€λ₯΄κ² νμ΄μ νμ΄ κ³΅μ ν΄λ΄ λλ€!! |
μ λ λ°λ‘ μ΄κΈ°κ³ μ§λ κ΄κ³λ₯Ό μκ°ν΄μ νμμ΅λλ€!!
μμ κ°μ΄ κ³μ°ν΄μ μ€λ³΅μ μ κ±°νκ³ λ κ·Έλνμ ν©μ΄ n-1μΈλ§νΌ λΆλΆκΉμ§ μκ°νλλ° κ΅¬νμμ λ§νμ μΈν°λ·μ μ°Έκ³ νμ΅λλ€ γ
def solution(n, results):
answer = 0
win_graph = {} # μ΄κΈ΄ μ λ€
lose_graph = {} # μ§ μ λ€
for i in range(1, n+1):
win_graph[i] = set()
lose_graph[i] = set()
for winner, loser in results: # κ·Έλν λ§λ€κΈ°
if winner not in win_graph:
win_graph[loser] = set()
if loser not in lose_graph:
lose_graph[winner] = set()
win_graph[loser].add(winner)
lose_graph[winner].add(loser)
for i in range(1, n+1):
for winner in win_graph[i]: # iνν
μ§ μ λ€ -> iλ₯Ό μ΄κΈ΄ μ λ€νν
λ μ§
if i in lose_graph:
lose_graph[winner].update(lose_graph[i])
for loser in lose_graph[i]: # iνν
μ΄κΈ΄ μ λ€ -> iνν
μ§ μ λ€νν
λ μ΄κΉ
if i in win_graph:
win_graph[loser].update(win_graph[i])
for i in range(1, n+1):
if len(win_graph[i]) + len(lose_graph[i]) == n-1: # iνν
μ΄κΈ°κ³ μ§ μ λ€ ν©μ³μ n-1μ΄λ©΄ μμκ° κ²°μ λ κ²
answer += 1
return answer |
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.
def bfs (_dict, player,n) :
queue = [player]
checker = [0 for _ in range(0,n+1)]
count_node = 0
while queue :
current_player = queue.pop(0)
for checking_player in _dict[current_player] :
if checker[checking_player] == 0 :
checker[checking_player] = 1
queue.extend(_dict[current_player])
count_node += 1
return count_node
def solution(n, results):
answer = 0
win_dict = {index+1 : [] for index in range(len(results))}
lose_dict = {index+1 : [] for index in range(len(results))}
for result in results :
win, lose = result[0], result[1]
win_dict[win].append(lose)
lose_dict[lose].append(win)
for player in range(1,n+1) :
if bfs(win_dict, player,n) + bfs(lose_dict, player,n) == n-1 :
answer +=1
print(answer)
return answer
μ΄λ κ² νλλ°
μ΄λ κ² λ μ νκ° λλ€μ λκ° νλ ΈμκΉμ?
μ¬κ· ν¨μκ° λ무 κΉμ΄μ Έμ κ·Έλ° κ² κ°μλ°, μ½λ 맨 μμ import sys
sys.setrecursionlimit(1000000) λ£μ΄λ³΄μκ² μ΄μ? μ κ° μ§μ ν΄λ³΄λκΉ μμ§ κ·Έλλ μλ¬κ° λλ€μ.. μΈλ±μ€ κ΄λ ¨ μλ¬μΈκ°... |
@tgyuuAn μ μμ½λ€μ.. γ γ ν λ² λ€μ ν΄λ΄μΌκ² μ΄λ€ |
π λ¬Έμ λ§ν¬
μμ
βοΈ μμλ μκ°
μ½ 1μκ°.?
β¨ μλ μ½λ
μ²μμ λ¬Έμ μ€
μ²μμ μμ΄λμ΄κ° μ μ λ μ¬λΌμ ν λ² κ·Έλ₯ κ·Έλ¦ΌνμΌλ‘ νλ¦μ 무μμ λ°λΌκ°λ΄€μ΅λλ€. λ§μ°μ€λΌμ κΈμ¨κ° λλ½κ΅°μ
μν! κΉ¨λ¬μμ΅λλ€. μ΄ λ μ.. λλ¨Έμ§ λͺ¨λ λ Έλλ€κ³Ό μ°κ²°λμ΄ μλ€λ©΄ μμλ₯Ό μ μ μκ΅°μ?
μ λ λ Έλλ€μ ν©μ§ν©μ΄ λ³ΈμΈμ μ μΈν λ Έλλ€μ΄λΌλ©΄(μ¬μ€μ n-1κ°μ λ ΈλλΌλ©΄) μμλ₯Ό μ μ μλ λ Έλμ λλ€.
μ¬κΈ°κΉμ§ μμλ΄κ³ μ, νλ‘μ΄λ-μμ μκ³ λ¦¬μ¦μ μ°λ λ¬Έμ μΈκ°?κΉμ§ μ μΆν΄λλλ°..
μ€λλ§μ λ€μ΄λ³΄λ μ΄λ¦λ€μ΄λΌ μ΄λ»κ² ꡬννλμ§ κΉλ¨Ήμ΄λ²λ Έμ§ λμμ.
κ·Έλμ μ λ§μλλ‘ μΌλ¨ ꡬνν΄λ΄€μ΅λλ€. μ½λκ° λλ¬μ보μ΄λ μ΄μ λ λ€ κ·Έκ² λλ¬Έμ΄μμ.
κ°λ¨νκ² μ κ° κ΅¬νν λ°©μμ μ€λͺ ν΄λ³΄κ² μ΅λλ€.
π μλ‘κ² μκ²λ λ΄μ©
λ¬Έμ λ₯Ό κΌΌκΌΌν μ½μ.