-
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
68-9kyo-hwang #230
Open
9kyo-hwang
wants to merge
5
commits into
main
Choose a base branch
from
68-9kyo-hwang
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
68-9kyo-hwang #230
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mjj111
approved these changes
Oct 2, 2024
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.
μ΄μ λλ§μΉ¨ λ°λλ°λ νκ² νμμμ΅λλ€ γ
κ°μΈμ μΌλ‘λ μ μ²λΌ Set 2κ° μ¬μ©νλ κ²λ³΄λ€ κ΅ν©λ νμ΄κ° λ μ§κ΄μ μΈκ±° κ°μμπ€
import java.util.*;
class Solution {
public int solution(int coin, int[] cards) {
int N = cards.length;
Set<Integer> original = new HashSet();
Set<Integer> additional = new HashSet();
int index = N / 3;
for(int i = 0 ; i < index; ++i) {
original.add(cards[i]);
}
int target = N + 1;
int round = 0;
while(true){
round++;
if(index >= N){
break;
}
additional.add(cards[index]);
additional.add(cards[index+1]);
index += 2;
boolean flag = false;
for(int i : original){
if(original.contains(target - i)){
original.remove(i);
original.remove(target - i);
flag = true;
break;
}
}
if(!flag && coin > 0){
for(int i : original){
if(!additional.contains(target - i)) continue;
original.remove(i);
additional.remove(target - i);
--coin;
flag = true;
break;
}
}
if(!flag && coin > 1){
for(int i : additional){
if(!additional.contains(target - i)) continue;
additional.remove(i);
additional.remove(target - i);
coin -= 2;
flag = true;
break;
}
}
if(!flag) break;
}
return round;
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
π λ¬Έμ λ§ν¬
2024 KAKAO WINTER INTERNSHIP n + 1 μΉ΄λκ²μ
1 ~
n
μ¬μ΄ μκ° μ μΈ μΉ΄λκ° νλμ© μλ μΉ΄λ λμΉμ λμ coin
κ°λ₯Ό κ°μ§κ³ μλ€. λ€μκ³Ό κ°μ μμλ‘ κ²μμ μ§ννλ€.n/3
μ₯μ λ½μ λͺ¨λ κ°μ§λ€.n
μ 6μ λ°°μμ΄λ€.n+1
μ΄ λλλ‘ μΉ΄λ 2μ₯μ λ΄κ³ λ€μ λΌμ΄λλ₯Ό μ§νν μ μλ€. λ§μ½ μΉ΄λ 2μ₯μ λΌ μ μλ€λ©΄ κ²μμ μ’ λ£λλ€.μμ μ€λͺ
μλ₯Ό λ€μ΄
n
= 12,coin
= 4μ΄κ³ [3, 6, 7, 2, 1, 10, 5, 9, 8, 12, 11, 4] μμλλ‘ μΉ΄λλ₯Ό λ½λλ‘ μΉ΄λ λμΉκ° μ£Όμ΄μ§λ€.μ²μμ 3, 6, 7, 2 μΉ΄λ 4μ₯(=
n/3
)κ³Ό λμ 4κ°(=coin
)λ₯Ό κ°μ§κ³ μμνλ€. λ€μ λΌμ΄λλ‘ μ§ννκΈ° μν΄ λ΄μΌ ν μΉ΄λ λ μ₯μ μ ν μμ ν©μ 13(=n+1
)μ΄λ€. λ€μκ³Ό κ°μ λ°©λ²μΌλ‘ μ΅λ 5λΌμ΄λκΉμ§ λλ¬ν μ μλ€.μ²μμ κ°μ§ λμ μλ₯Ό λνλ΄λ μ μ
coin
κ³Ό μΉ΄λλ₯Ό λ½λ μμλλ‘ μΉ΄λμ μ ν μλ₯Ό λ΄μ 1μ°¨μ μ μ λ°°μ΄cards
κ° λ§€κ°λ³μλ‘ μ£Όμ΄μ§ λ, κ²μμμ λλ¬ κ°λ₯ν μ΅λ λΌμ΄λμ μλ₯Ό return νλλ‘ solution ν¨μλ₯Ό μμ±νλΌ.βοΈ μμλ μκ°
1μκ° λ°?
β¨ μλ μ½λ
λνμ€ κ²μμ μ΅κ·Όμ νμ΄μ κ·Έλ°κ°, μ΄ λ¬Έμ μ μ κ·Όλ²μ΄ μκ°λ³΄λ€λ κΈλ°© λ μ¬λλ€. μ΄ λ¬Έμ λ λ§μ°¬κ°μ§λ‘, μΉ΄λλ₯Ό λ½μ κ²κ³Όλ 무κ΄νκ² "μΉ΄λ μ¬μ© μμ μ λ€λ‘ λ―Έλ£° μ μλ€"λ κ²μ μΊμΉν΄μΌ νλ€.
λν μΉ΄λ μ¬μ© μμ μ λ€λ‘ 미룬λ€λ κ²μ μ΄λκ°μ μΉ΄λκ° μ μ₯λμ΄ μλ€λ κ²μΈλ°, κ±°κΈ°μ
n+1
μ«μλ₯Ό λ§λ€ μ μλ 2μ₯μ λΉ λ₯΄κ² μ°Ύμλ΄μΌ νλ―λ‘ Set μλ£κ΅¬μ‘°λ₯Ό μ¬μ©ν΄μΌ νλ μ λ ν¬μΈνΈλ€.λμ κ²½μ° Setμ <card μ«μ, κ΅νμ νμν coin κ°μ> μμΌλ‘ μ§μ΄λ£λ μμΌλ‘ coin μλͺ¨ κ°μλ₯Ό νμΈν μ μκ² νλ€.
μ μ²λ¦¬ κ³Όμ μ΄λ€. μ΄λ μ£Όμ΄μ§λ cards λ°°μ΄μ λ€μ§λ μ΄μ λ μΉ΄λλ₯Ό 2μ₯μ© λ½μ λ stackμ²λΌ pop_back()μ νΈμΆν΄μ ν¨μ¨μ μΌλ‘ λ½μ μ μλλ‘ νκΈ° μν¨μ΄λ€.
μΉ΄λλ₯Ό μμ§ν Setμ΄λ€. μ²μμ
n / 3
μ₯μ κ°κ³ μμνλ―λ‘, μ¬κΈ°μλ λμ μλͺ¨ κ°μλ₯Ό 0μΌλ‘ μ€μ ν΄μ Setμ μ½μ νλ€. Comparatorλ λ€μμ μ€λͺ νκ² λ€.μ΄μ λΌμ΄λλ₯Ό μμνλ€. λ μ΄μ μ£Όμ΄μ§ μΉ΄λκ° μμ λκΉμ§ λΌμ΄λλ₯Ό λ°λ³΅νλ©°, 2μ₯μ© μΉ΄λλ₯Ό λ½μ Setμ μ½μ νλ€. μ΄μ λΆν° λ½μ μΉ΄λλ₯Ό μ¬μ©νκΈ° μν΄μ λ°λμ λμ μ μλͺ¨ν΄μΌ νλ―λ‘ λμ μλͺ¨ κ°μλ₯Ό 1λ‘ μΈν ν΄μ μ½μ νλ€.
μ΄μ 보μ ν λ±μ μννλ©°
n+1
μ λ§λ€ μ μλ μΉ΄λ μ‘°ν©μ΄ μλ μ§ νμΈνλ€.FindCombination = true
λ‘ μ€μ λλ©° λ± μνλ₯Ό μ€λ¨νλ€.λ±μ μ‘΄μ¬νλ λͺ¨λ μΉ΄λλ₯Ό μννλ©°
n+1
, μ¦RequiredSum
μ λ§λ€ μ μλTargetCard
λ₯Ό μ°Ύλλ€.Card
μTargetCard
κ° λμΌν μ«μκ° λμ¬ μλ μλλ°, λ¬Έμ μμ μ£Όμ΄μ§ μΉ΄λλ λͺ¨λ μ«μκ° λ€λ₯΄λ€.μ¬λ°λ₯Έ
TargetCard
κ° κ³μ°λμλ€λ©΄ νμ¬ Deckμ μ΄ μΉ΄λκ° μ‘΄μ¬νλ μ§ κ²μ¬νλ€.TargetCoin
μ μ°μ νλ€.λ±μ μΉ΄λκ° μ‘΄μ¬νλ κ²μ΄ νμΈλλ€λ©΄, λ§μ§λ§μΌλ‘ μ΄ μ‘°ν©μ μΈ μ μλ μ§ νμΈνλ€.
FindCombination = true
λ‘ μ€μ νλ€.μ¬κΈ°μ Setμ ComparatorλΌλ λ³λμ λΉκ΅ κΈ°μ€μ΄ νμνλ°,
n+1
μ λ§λλ μΉ΄λ μ‘°ν©μ΄ λ°λμ 1κ°μ§λΌλ 보μ₯μ μλ€. λμ μ 0κ° μλͺ¨νλ μ‘°ν©, 1κ° μλͺ¨νλ μ‘°ν©, 2κ° μλͺ¨νλ μ‘°ν©μ΄ μμ μ μλ€.λ°λ³΅λ¬Έμ λΉ μ Έλμλλ°λ
FindCombination == false
μΈ κ²½μ°,n+1
μ λ§λλ μΉ΄λ 2μ₯μ΄ λ±μ μ‘΄μ¬νμ§ μλλ€λ λ»μ΄λ―λ‘ λΌμ΄λ μ§νμ μ€λ¨νλ€.return Round;
μ 체 λ°λ³΅λ¬Έμ΄ μ’ λ£λλ©΄ μ΄μ κΉμ§ μ§νν λΌμ΄λ μκ° κΈ°λ‘λΌμμΌλ―λ‘ μ΄λ₯Ό λ°ννλ€.
μ 체 μ½λ
π μλ‘κ² μκ²λ λ΄μ©
μ¬μ€ λλ μ€λ³΅λ μ½λλ₯Ό μμΈλ €κ³ Setμ Comparator μ μνκ³ μ’ λ³΅μ‘νκ² νμ΄νλλ°, λ€λ₯Έ λΆλ€ νμ΄λ₯Ό 보면 κ·Έλ₯ μ΄κΈ°μ λ½μ μΉ΄λ λͺ©λ‘ Setκ³Ό λΌμ΄λ μ§ν μ€μ λ½μ μΉ΄λ Set 2κ°λ₯Ό μ¬μ©νλ€. μ΄κ² μ’ λ μ§κ΄μ μΌ μλ...?
μ§λ μ£Όμλ λ°λΉ΄μ΄μ PRμ λͺ»μΌμ΅λλ€ μ£μ‘ν©λλ€ π’