-
Notifications
You must be signed in to change notification settings - Fork 5
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
6-ljedd2 #141
6-ljedd2 #141
Conversation
class Solution {
static boolean[] visited;
static int max = 0;
public int solution(int k, int[][] dungeons) {
visited = new boolean[dungeons.length];
checkDungeon(dungeons, k, 0);
return max;
}
static void checkDungeon(int[][] dungeons, int k, int cnt) {
if(max < cnt) max = cnt;
for(int i = 0; i < dungeons.length; i++) {
if(!visited[i] && dungeons[i][0] <= k) {
visited[i] = true;
checkDungeon(dungeons, k - dungeons[i][1], cnt + 1);
visited[i] = false;
}
}
}
} μ΄μ μ νμλ μ½λμΈλ° μ μ λ νμ΄λ μμ λκ°μ΄ ν΄κ²°νμλ€μ...!! ν λ² λ λ¬Έμ 볡기νλ©΄μ νμ΄ κ³ λ―Όν΄λ³Ό μ μμμ΅λλΉ μ΄λ² μ°¨μλ μκ³ νμ ¨μ΄μ! |
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.
μ λ λͺ¨λ κ²½μ°μ μλ₯Ό νμνλ μμ νμ μκ³ λ¦¬μ¦μΌλ‘ λ¬Έμ λ₯Ό νμμ΅λλ€.
μ λ κ°λ₯ν λμ λ°©λ¬Έμ μμλ₯Ό μ»κΈ° μν΄μ μμ΄μ μ¬μ©νλλ° μ΄λ² λ¬Έμ λ₯Ό νλ©΄μ μμ΄ λΌμ΄λΈλ¬λ¦¬λ₯Ό μ°λ λ°©λ²μ μ ννκ² μκ² λμλ€μ!
from itertools import permutations
def solution(k, dungeons):
answer = 0
# λͺ¨λ λμ λ°©λ¬Έ μμλ₯Ό μμ±
for order in permutations(range(len(dungeons))):
remaining_energy = k
visited_count = 0
# νμ¬ μμμ λ°λ₯Έ λμ λ°©λ¬Έ μλ
for dungeon_index in order:
min_energy, cost = dungeons[dungeon_index]
if remaining_energy >= min_energy:
visited_count += 1
remaining_energy -= cost
# λ°©λ¬Έν λμ μ μλ₯Ό κΈ°λ‘
answer = max(answer, visited_count)
return answer
μ μλμ μλμ½λ λλΆμ enumerate()
λ₯Ό μ μ©ν΄μ λ ν¨μ¨μ μΌλ‘ μ½λλ₯Ό 지 μ μλ€λ κ²λ μκ² λμμ΅λλ€π
μ΄λ² PRλ μκ³ νμ ¨μ΄μ!
π λ¬Έμ λ§ν¬
νλ‘κ·Έλλ¨Έμ€ μ½λ©ν μ€νΈ κ³ λμ Kit - μμ νμ(DFS) | νΌλ‘λ
λ¬Έμ μ€λͺ
μ΅μ νμ νΌλ‘λ
"μ λμ ννμ λ§μ³€μ λ μλͺ¨λλ "μλͺ¨ νΌλ‘λ
"κ° μλ€.μ΅μ νμ νΌλ‘λ
"λ ν΄λΉ λμ μ νννκΈ° μν΄ κ°μ§κ³ μμ΄μΌ νλ μ΅μνμ νΌλ‘λλ₯Ό λνλ΄λ©°,μλͺ¨ νΌλ‘λ
"λ λμ μ ννν ν μλͺ¨λλ νΌλ‘λλ₯Ό λνλΈλ€.μ νμ¬ν
βοΈ μμλ μκ°
1μκ° + @
β¨ μλ μ½λ
1. λ¬Έμ νμ΄
λ¬Έμ μμ μ£Όμ΄μ§ μ λ ₯λ€κ³Ό 쑰건μ λ°λΌ νμν νΌλ‘λμ νμ¬ νΌλ‘λλ₯Ό κ³μ λνκ³ λΉΌκ°λ©΄μ μμ νμμ νλ λ°©λ²μ λ μ¬λ Έμ΅λλ€.
쑰건μ λ§μ§ μλ€λ©΄ λ€λ‘ λ€μ λμκ°κ³ , μ‘°κ±΄μ΄ λ§λ€λ©΄ λ κΉκ² νμν©λλ€.
μ΄ λ, μ΄λ―Έ ν΄λ¦¬μ΄ν λμ μ λ€μ λ°©λ¬Έν μ μμ΅λλ€.
μ΄ κ³Όμ μμ μ΄μ λ Έλμ μνλ‘ λλμκ°κΈ° μν΄, λ°©λ¬Έ μ¬λΆ λ° λ°©λ¬Έ νμλ₯Ό μ΄μ λ ΈλκΉμ§ νμνμ λμ κ°μΌλ‘ 볡ꡬλ₯Ό ν΄μΌνκΈ° λλ¬Έμ visited λ°°μ΄μ μΆκ°λ‘ μ¬μ©νμ΅λλ€.
nonlocalμ μ¬μ©νλ©΄ ν¨μ λ΄λΆμμλ§ κ°μ κ°±μ ν μ μμ΅λλ€. (λΉμ μλ³μ)
μ΅μ’ μ μΌλ‘ μΉ΄μ΄ν λ μ΅λ νμκ° κ³μ°λμ΄ μΆλ ₯λ©λλ€.
μ½λλ₯Ό μ‘°κΈ λ ν¨μ¨μ μΌλ‘ μμ±ν μ μμκΉ κ³ λ―Όνλ€κ°
enumerate() λ₯Ό μ¬μ©νλ©΄ κ° λμ μ μΈλ±μ€μ κ°μ λμμ κ°μ Έμ¬ μ μμ΄μ
λ°λ‘ μ μ©μ νμμ΅λλ€.
λ³μ μ μΈμ λ°λ‘ ν΄μ£Όμ§ μμλ λ°λ³΅λ¬ΈμΌλ‘ λ°λ‘ μ μ©ν΄λ³Ό μ μμμ΅λλ€.
2. κ°μ λ μ½λ
π μλ‘κ² μκ²λ λ΄μ©