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

70-9kyo-hwang #233

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

70-9kyo-hwang #233

wants to merge 6 commits into from

Conversation

9kyo-hwang
Copy link
Collaborator

@9kyo-hwang 9kyo-hwang commented Nov 2, 2024

πŸ”— 문제 링크

2021 KAKAO BLIND RECRUITMENT 맀좜 ν•˜λ½ μ΅œμ†Œν™”

"μΉ΄μΉ΄μ˜€μƒμ‚¬"λŠ” λ‹€μŒκ³Ό 같은 쑰직 ꡬ성도λ₯Ό κ°–λŠ”λ‹€.
image

원 μ•ˆμ— 적힌 두 개의 μˆ«μžλŠ” μ§μ›μ˜ 정보λ₯Ό μ˜λ―Έν•œλ‹€. μ™Όμͺ½ μˆ«μžλŠ” μ§μ›λ²ˆν˜Έμ΄λ©° 직원을 식별할 수 μžˆλ„λ‘ 1λ²ˆλΆ€ν„° μˆœμ„œλŒ€λ‘œ λ°œκΈ‰λ˜λŠ” 일련번호이며, 였λ₯Έμͺ½ μˆ«μžλŠ” ν•΄λ‹Ή μ§μ›μ˜ ν•˜λ£¨ν‰κ·  λ§€μΆœμ•‘μ„ λ‚˜νƒ€λ‚Έλ‹€. μœ„ κ·Έλ¦Όμ—μ„œ 1번 직원은 14원을, 9번 직원은 28μ›μ˜ ν•˜λ£¨ν‰κ·  λ§€μΆœμ•‘μ„ κΈ°λ‘ν•˜κ³  μžˆλ‹€.

ν™”μ‚΄ν‘œκ°€ μ‹œμž‘λ˜λŠ” μͺ½μ˜ 직원은 νŒ€μž₯, ν™”μ‚΄ν‘œλ₯Ό λ°›λŠ” μͺ½μ˜ 직원은 νŒ€μ›μ„ μ˜λ―Έν•œλ‹€.

  • μ§μ›λ²ˆν˜Έ 1λ²ˆμ€ νšŒμ‚¬μ˜ CEO둜 κ³ μ •λ˜μ–΄ 있으며, CEOλŠ” 항상 νŒ€μž₯이고 νŒ€μ›μΌ 수 μ—†μ–΄ ν™”μ‚΄ν‘œλ₯Ό λ°›λŠ” μͺ½μ΄ 될 수 μ—†λ‹€.
  • CEOλ₯Ό μ œμ™Έν•œ λ‚˜λ¨Έμ§€ λͺ¨λ“  직원듀은 λ‹€λ₯Έ λˆ„κ΅°κ°€λ‘œλΆ€ν„° μ •ν™•νžˆ 1개의 ν™”μ‚΄ν‘œλ₯Ό λ°›λŠ”λ‹€.

ν•œ 직원은 μ΅œλŒ€ 2개의 νŒ€μ— μ†Œμ†λ  수 μžˆμ§€λ§Œ, νŒ€μž₯을 κ²Έμž„ν•˜κ±°λ‚˜ 두 개의 νŒ€μ—μ„œ νŒ€μ›μ΄ 될 μˆ˜λŠ” μ—†λ‹€.

  • 10번 직원은 DνŒ€μ˜ νŒ€μž₯μ΄λ©΄μ„œ λ™μ‹œμ— 5번 직원이 νŒ€μž₯으둜 μžˆλŠ” CνŒ€μ— μ†ν•œ νŒ€μ›μ΄λ‹€.
  • 5번, 9번, 10번 직원은 λ°›λŠ” μͺ½μ˜ ν™”μ‚΄ν‘œμ™€ μ‹œμž‘ν•˜λŠ” ν™”μ‚΄ν‘œκ°€ λͺ¨λ‘ μžˆμœΌλ―€λ‘œ νŒ€μž₯인 λ™μ‹œμ— νŒ€μ›μ΄λ‹€.
  • 2번, 3번, 4번, 6번, 7번, 8번 직원은 μ‹œμž‘ν•˜λŠ” ν™”μ‚΄ν‘œκ°€ μ—†κ³  λ°›λŠ” μͺ½μ˜ ν™”μ‚΄ν‘œλ§Œ μžˆμœΌλ―€λ‘œ νŒ€μž₯이 μ•„λ‹ˆλ©° 였직 νŒ€μ›μ΄λ‹€.
  • 1번 직원인 CEOλŠ” λ°›λŠ” μͺ½μ˜ ν™”μ‚΄ν‘œκ°€ μ—†κ³  μ‹œμž‘ν•˜λŠ” ν™”μ‚΄ν‘œλ§Œ 있으며 항상 νŒ€μ›μ΄ μ•„λ‹Œ νŒ€μž₯이닀.

그림의 μ‘°μ§λ„μ—λŠ” A, B, C, D 총 4개의 νŒ€μ΄ μ‘΄μž¬ν•˜λ©°, 각각 1번, 9번, 5번, 10번 직원이 νŒ€μž₯ μ§μœ„λ₯Ό λ‹΄λ‹Ήν•œλ‹€.

μ΄λŸ¬ν•œ κ΅¬μ„±μ—μ„œ, ν•˜λ£¨ μΌμ •μœΌλ‘œ μ›Œν¬μˆμ„ κ³„νšν•˜κ³  μžˆλ‹€. 단, λͺ¨λ“  직원을 μ°Έμ„μ‹œν‚¬ 수 μ—†μ–΄ μ•„λž˜μ™€ 같은 κΈ°μ€€μœΌλ‘œ μ›Œν¬μˆμ— 참석할 직원듀을 μ„ λ°œν•˜λ €κ³  ν•œλ‹€.

  1. μ›Œν¬μˆμ—μ„œ κ΅μœ‘λ°›μ€ λ‚΄μš©μ€ μ „ μ§μ›λ“€μ—κ²Œ κ³΅μœ λ˜μ–΄μ•Ό ν•˜λ―€λ‘œ λͺ¨λ“  νŒ€μ€ μ΅œμ†Œ 1λͺ… μ΄μƒμ˜ 직원을 μ›Œν¬μˆμ— μ°Έμ„μ‹œμΌœμ•Ό ν•œλ‹€.
  2. μ›Œν¬μˆ κΈ°κ°„ λ™μ•ˆ, νšŒμ‚¬μ˜ 맀좜 손싀을 μ΅œμ†Œν™”ν•˜λŠ” 것이 μ€‘μš”ν•˜λ―€λ‘œ μ›Œν¬μˆμ— μ°Έμ„ν•˜λŠ” μ§μ›λ“€μ˜ ν•˜λ£¨ν‰κ·  λ§€μΆœμ•‘μ˜ 합이 μ΅œμ†Œκ°€ λ˜μ–΄μ•Ό ν•œλ‹€.

μœ„ μ‘°μ§λ„μ—μ„œλŠ” νšŒμƒ‰μœΌλ‘œ 칠해진 1, 7, 10번 직원을 μ›Œν¬μˆμ— μ°Έμ„μ‹œν‚€λ©΄ λͺ¨λ“  νŒ€μ—μ„œ μ΅œμ†Œ ν•œ λͺ… μ΄μƒμ˜ 직원을 μ°Έμ„μ‹œν‚¨ 것이 되며, ν•΄λ‹Ή μ§μ›λ“€μ˜ ν•˜λ£¨ν‰κ·  λ§€μΆœμ•‘μ˜ 합은 44(14+13+17)원이닀. 10번 직원은 CνŒ€κ³Ό DνŒ€ λͺ¨λ‘μ— 속해 μžˆμœΌλ―€λ‘œ, 두 νŒ€μ—μ„œ λͺ¨λ‘ μ°Έμ„ν•œ κ²ƒμœΌλ‘œ μΈμ •λœλ‹€.

λͺ¨λ“  νŒ€μ—μ„œ μ΅œμ†Œ ν•œ λͺ… 이상 μ›Œν¬μˆμ— μ°Έμ„ν•˜λ©΄μ„œ, μ°Έμ„ν•˜λŠ” μ§μ›λ“€μ˜ ν•˜λ£¨ν‰κ·  λ§€μΆœμ•‘μ˜ 합을 μ΅œμ†Œλ‘œ ν•˜λ €κ³  ν•  λ•Œ, μ΅œμ†Œν™”λœ λ§€μΆœμ•‘μ˜ 합을 κ΅¬ν•΄μ„œ return ν•˜λ„λ‘ solution ν•¨μˆ˜λ₯Ό μ™„μ„±ν•˜λΌ.

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

2μ‹œκ°„ ν•˜κ³  GGμ³μ„œ ν•΄μ„€ λ΄„...

✨ μˆ˜λ„ μ½”λ“œ

쑰직도 그림을 보면 μ•Œ 수 μžˆλ“―, 트리 ν˜•νƒœλ‘œ 관계λ₯Ό ν‘œν˜„ν•  수 μžˆλ‹€. CEO, 즉 1번 λ…Έλ“œκ°€ Head둜 μ‘΄μž¬ν•˜κ³  νŒ€μ›λ“€μ΄ μ„œλΈŒ 트리둜 ν‘œν˜„λœλ‹€.

vector<vector<int>> Tree;
int solution(vector<int> InSales, vector<vector<int>> InLinks) 
{
    Tree.resize(InSales.size() + 1);  // Sales λ°°μ—΄ 길이가 직원 수(N), 1λ²ˆλΆ€ν„° μ‹œμž‘ν•˜λ―€λ‘œ N + 1
    for(const vector<int>& Link : InLinks)
    {
        int a = Link[0], b = Link[1];
        Tree[a].emplace_back(b);
    }

    ...
}

우리의 λͺ©ν‘œλŠ” λͺ¨λ“  νŒ€μ—μ„œ μ΅œμ†Œ ν•œ λͺ…μ˜ νŒ€μ›μ„ μ›Œν¬μˆμ— μ°Έμ„μ‹œν‚€λ˜, "맀좜 손해λ₯Ό μ΅œμ†Œλ‘œ" λ§Œλ“€μ–΄μ•Ό ν•œλ‹€. λ‹€λ§Œ 맀좜 손해가 μ΅œμ†Œκ°€ λ˜λŠ” νŒ€μ› 쑰합이 λ°”λ‘œ μ•Œ 수 μžˆλŠ” 방법은 μ—†μœΌλ―€λ‘œ, λͺ¨λ“  경우λ₯Ό 확인해봐야 ν•œλ‹€.
λ‹€λ§Œ 직원이 μ΅œλŒ€ 300,000λͺ…κΉŒμ§€ μ‘΄μž¬ν•  수 있기 λ•Œλ¬Έμ— λ‹¨μˆœνžˆ λͺ¨λ“  경우λ₯Ό ν™•μΈν•˜κΈ°μ—λŠ” μ‹œκ°„μ΄ λ„ˆλ¬΄ 였래 κ±Έλ¦°λ‹€. 그렇기에 각 κ²½μš°μ— λ”°λ₯Έ 맀좜 손싀을 μΊμ‹±ν•΄λ‘λŠ” λ“± λ³„λ„μ˜ μ‘°μΉ˜κ°€ ν•„μš”ν•˜λ‹€. 이 점을 κΈ°μ–΅ν•˜μž.

μš°μ„  각 νŒ€μ›μ΄ μ›Œν¬μˆμ— μ°Έμ„ν•˜λƒ, 참석 μ•ˆν•˜λƒμ— λ”°λ₯Έ 맀좜 손싀을 λ‚˜νƒ€λ‚΄λ³΄μž.

  • νŒ€μž₯이 μ•„λ‹Œ νŒ€μ›, 즉 Leaf Node듀은 참석 μ•ˆν•˜λ©΄ 손싀이 μ—†κ³  참석 ν•˜λ©΄ 본인의 맀좜만큼 손싀이 λ°œμƒν•œλ‹€.
  • νŒ€μž₯ μž…μž₯μ—μ„œλŠ” μ•„λž˜ μ‘°μ§μ—μ„œ κ³„μ‚°λœ μ΅œμ„ μ˜ κ²°κ³Ό + λ‚˜μ˜ 참석 μœ λ¬΄μ— λ”°λ₯Έ μΆ”κ°€ μ†μ‹€λ‘œ κ²½μš°κ°€ λ‚˜λ‰œλ‹€.

μœ„μ™€ 같은 계산 방식을 생각해보면, DFSλ₯Ό μ‚¬μš©ν•˜μ—¬ 말단 μ§μ›μ—μ„œ μ‹œμž‘ν•΄ μ΅œμ’…μ μœΌλ‘œ CEO에 λ„λ‹¬ν•΄μ„œ μ΅œμ„ μ˜ κ²°κ³Όλ₯Ό μ–»μ–΄λ‚΄λŠ” μ‹μœΌλ‘œ κ³„μ‚°ν•˜λŠ” 방식이 합리적이닀.

pair<int, int> DFS(const vector<int>& InLosses, const int Head = 1);

DFS ν•¨μˆ˜λ₯Ό μœ„μ™€ 같이 μ •μ˜ν•œλ‹€. 2개의 int 값을 λ°˜ν™˜ν•˜λŠ”λ°, (Head 직원이 참석 μ•ˆν–ˆμ„ λ•Œμ˜ μ΅œμ†Œ 맀좜 손싀, Head 직원이 μ°Έμ„ν–ˆμ„ λ•Œμ˜ μ΅œμ†Œ 맀좜 손싀)을 λ°˜ν™˜ν•œλ‹€. μΈμžλŠ” 손싀, 즉 각 νŒ€μ›μ˜ 맀좜 λ°°μ—΄κ³Ό ν˜„μž¬ νŒ€μ›(νŒ€μž₯) 번호λ₯Ό μ˜λ―Έν•œλ‹€. 졜초 μ‹œμž‘μ€ CEO이닀.

if(Tree[Head].empty())
{
    return {0, LossIfHeadParticipation};
}

ν˜„μž¬ Head νŒ€μ›μ˜ ν•˜λΆ€ 쑰직이 없을 경우, 즉 Leaf Node인 경우 μœ„μ—μ„œ μ–˜κΈ°ν•œ κ²ƒμ²˜λŸΌ (참석 μ•ˆν•˜λ©΄ 손싀 0, μ°Έμ„ν•˜λ©΄ 본인 맀좜만큼 손싀)을 λ°˜ν™˜ν•œλ‹€.

int TotalLosses = 0, MinLossDifference = 1e9;
bool bMemberParticipation = false;
for(const int Member : Tree[Head])
{
    const auto& [LossIfNotParticipation, LossIfParticipation] = DFS(InLosses, Member);
    TotalLosses += min(LossIfNotParticipation, LossIfParticipation);
    ...
}

Leaf Nodeκ°€ μ•„λ‹ˆλΌλ©΄ 자기 λ°‘μœΌλ‘œ νŒ€μ›λ“€μ΄ μ‘΄μž¬ν•  ν…Œλ‹ˆ 각각을 μˆœνšŒν•˜λ©° DFSλ₯Ό μž¬κ·€ ν˜ΈμΆœν•΄ (참석 μ•ˆν–ˆμ„ λ•Œμ˜ 손싀, 참석 ν–ˆμ„ λ•Œμ˜ 손싀)을 각각 λ°˜ν™˜λ°›λŠ”λ‹€. 그리고 λ‘˜ 쀑 손싀이 적은 μͺ½μ˜ κ°’μœΌλ‘œ 전체 손싀을 κΈ°λ‘ν•œλ‹€.

for(const int Member : Tree[Head])
{
    ...
    if(LossIfNotParticipation >= LossIfParticipation)
    {
        bMemberParticipation = true;
    }

    MinLossDifference = min(MinLossDifference, LossIfParticipation - LossIfNotParticipation);
}

이 뢀뢄을 μ£Όμ˜ν•΄μ•Ό ν•œλ‹€. 일반적으둜 μƒκ°ν•˜λ©΄ "참석을 μ•ˆν•˜λ©΄ μ•ˆ ν•  수둝 맀좜 손싀은 적어진닀"κ°€ λ§žμ§€λ§Œ, 일뢀 κ²½μš°λŠ” 참석을 μ•ˆν•˜λŠ” 게 더 큰 맀좜 손싀이 일어날 수 μžˆλ‹€. 그럴 경우 νŒ€μ›μ„ λ°˜λ“œμ‹œ μ°Έμ„μ‹œμΌœμ•Ό ν•œλ‹€. MinLossDifferenceλŠ” μ•„λž˜μ—μ„œ μ„€λͺ…ν•œλ‹€.

if(bMemberParticipation)
{
    return {TotalLosses, LossIfHeadParticipation + TotalLosses};
}
...

ν•˜μœ„ νŒ€μ›λ“€μ˜ κ²°κ³Όλ₯Ό λͺ¨λ‘ ν™•μΈν–ˆλ‹€λ©΄ μ΅œμ’…μ μœΌλ‘œ νŒ€μž₯의 참석 μœ λ¬΄μ— λ”°λ₯Έ λΉ„μš©μ„ μΆ”κ°€λ‘œ 계산해 μ΅œμ’… 손싀을 λ°˜ν™˜ν•΄μ£Όλ©΄ λœλ‹€.
λ§Œμ•½ μœ„ forλ¬Έμ—μ„œ "μ°Έμ„μ‹œμΌœμ•Ό 맀좜 손싀이 덜 ν•œ" νŒ€μ›μ΄ λ°œκ²¬λμ„ 경우, κ·Έ νŒ€μ€ μ΅œμ†Œ 1λͺ… 참석이 보μž₯λ˜λ―€λ‘œ λ°˜ν™˜ν•˜λŠ” 맀좜 손싀도 λ”± νŒ€μž₯의 맀좜 만큼 더해진닀. 즉 (νŒ€μž₯이 참석 μ•ˆν•˜λ©΄ νŒ€μ›λ“€μ˜ 손싀, νŒ€μž₯이 μ°Έμ„ν•˜λ©΄ νŒ€μ›λ“€μ˜ 손싀 + νŒ€μž₯ 맀좜)이 λ°˜ν™˜λœλ‹€.

반면 "λͺ¨λ“  νŒ€μ›μ΄ μ›Œν¬μˆμ— μ°Έμ„ν•˜μ§€ μ•ŠλŠ” 것이 맀좜 손싀이 덜 λ°œμƒν•œλ‹€"라고 ν•˜λ©΄ κ³ λ €ν•΄μ•Ό ν•  사항이 λŠ˜μ–΄λ‚œλ‹€.

  • νŒ€μž₯이 μ›Œν¬μˆμ— μ°Έμ„ν•œλ‹€λ©΄ ν•΄λ‹Ή νŒ€μ— μ΅œμ†Œ 1λͺ… 참석이 보μž₯λ˜λŠ” κ²ƒμ΄λ―€λ‘œ λ°‘μ—μ„œ κ³„μ‚°λœ 손싀 + νŒ€μž₯ λ§€μΆœμ„ λ°˜ν™˜ν•˜λ©΄ λœλ‹€.
  • νŒ€μž₯μ‘°μ°¨ 참석을 μ•ˆν•˜λŠ” κ²½μš°λŠ” "νŒ€μ› 쀑 ν•œ λͺ…을 κ°•μ œλ‘œ 참석"μ‹œμΌœμ•Ό ν•˜λŠ”λ°, 이 경우 μ–΄λ–€ νŒ€μ›μ„ μ°Έμ„μ‹œμΌœμ•Ό ν•˜λŠ” 지에 λŒ€ν•΄ κ³ λ―Όν•΄μ•Ό ν•œλ‹€.

λͺ¨λ“  νŒ€μ›μ΄ 참석 μ•ˆν•˜λŠ” 게 손싀이 κ°€μž₯ 적고, νŒ€μž₯λ§ˆμ € 참석을 μ•ˆν•œλ‹€λ©΄ 손싀을 κ°μˆ˜ν•˜κ³  νŒ€μ› 1λͺ…을 κ°•μ œλ‘œ μ°Έμ—¬μ‹œμΌœμ•Ό ν•œλ‹€. 이 λ•Œ μ–΄λ–€ νŒ€μ›μ„ μ°Έμ„μ‹œμΌœμ•Ό ν•˜λŠ” 지가 문제인데, μ—¬κΈ°μ„œ MinLossDifferenceλ₯Ό λ‹€μ‹œ ν™•μΈν•œλ‹€.

MinLossDifference = min(MinLossDifference, LossIfParticipation - LossIfNotParticipation);

말 κ·ΈλŒ€λ‘œ 참석 ν–ˆμ„ λ•Œμ˜ 손싀과 참석 μ•ˆ ν–ˆμ„ λ•Œμ˜ μ†μ‹€μ˜ μ°¨ 쀑 κ°€μž₯ μž‘μ€ 값을 κΈ°λ‘ν•œλ‹€. 이 값이 μ—¬κΈ°μ„œ μ‚¬μš©λ˜λŠ”λ°, μ–΄μ©” 수 없이 νŒ€μ› ν•œ λͺ…을 μ°Έμ„μ‹œμΌœμ•Ό ν•œλ‹€λ©΄ "참석 ν•˜λ“  μ•ˆ ν•˜λ“  맀좜 손싀 차이가 λ³„λ‘œ μ—†λŠ”" 직원을 μ°Έμ„μ‹œν‚€λŠ” 것이 κ°€μž₯ μ’‹κΈ° λ•Œλ¬Έμ΄λ‹€.
μ •ν™•νžˆλŠ”, "참석 μ•ˆ ν–ˆμ„ λ•Œμ˜ 손싀을 λΉΌκ³  참석 ν–ˆμ„ λ•Œμ˜ 손싀을 λ”ν–ˆμ„ λ•Œ κ·Έ 값이 κ°€μž₯ μž‘μ€ νŒ€μ›"을 μ°ΎλŠ” 것이닀.

  • ν˜„μž¬ TotalLossesμ—λŠ” min(LossIfNotParticipation, LossIfParticipation) 값이 λˆ„μ λ˜κ³  μžˆλŠ”λ°, μš°λ¦¬κ°€ ν™•μΈν•˜λŠ” κ²½μš°λŠ” LossIfNotParticipation κ°’λ§Œ λˆ„μ λœ κ²½μš°μ΄λ‹€.
  • 즉 1λͺ…을 μ°Έμ„μ‹œμΌœμ•Ό ν•œλ‹€λ©΄ TotalLossesμ—μ„œ ν•΄λ‹Ή νŒ€μ›μ˜ LossIfNotParticipation 값이 빠지고 LossIfParticipation 값이 더해져야 ν•œλ‹€.
  • λ‹€μ‹œ 말해 TotalLosses - LossIfNotParticipation + LossIfParticipation 과정이 λ°œμƒν•˜λŠ” κ²ƒμ΄λ―€λ‘œ, λ”ν•΄μ§€λŠ” LossIfParticipation 값이 κ°€μž₯ μž‘μ€ νŒ€μ›μ„ μ°Ύμ•„μ•Ό ν•˜λŠ” 것이닀.
  • κ·ΈλŸ¬λ―€λ‘œ LossIfParticipation - LossIfNotParticipation이 κ°€μž₯ μž‘μ€ νŒ€μ›μ„ μ°Ύμ•„ MinLossDifference둜 κΈ°λ‘ν•˜λŠ” 것이닀.

μ°Έμ„μ‹œν‚¬ νŒ€μ›μ„ μ°Ύμ•˜λ‹€λ©΄, νŒ€μž₯이 μ°Έμ„ν•˜μ§€ μ•Šμ•˜μ„ λ•Œμ˜ λ°˜ν™˜κ°’μœΌλ‘œ TotalLosses + MinLossDifferenceλ₯Ό λ°˜ν™˜ν•œλ‹€.

...
else
{
    return {TotalLosses + MinLossDifference, LossIfHeadParticipation + TotalLosses};
}

μ΅œμ’…μ μœΌλ‘œ solution ν•¨μˆ˜μ—μ„œ 졜초둜 DFS ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œμ˜ λ°˜ν™˜κ°’μ€ [CEOκ°€ μ°Έμ„ν•˜μ§€ μ•Šμ•˜μ„ λ•Œμ˜ μ΅œμ†Œ 손싀, CEOκ°€ μ°Έμ„ν–ˆμ„ λ•Œμ˜ μ΅œμ†Œ 손싀]이 λœλ‹€. 이 λ‘˜ 쀑 μž‘μ€ 값을 λ°˜ν™˜ν•˜λ©΄ λœλ‹€.

const auto& [LossIfNotParticipation, LossIfParticipation] = DFS(InSales);
return min(LossIfNotParticipation, LossIfParticipation);
전체 μ½”λ“œ

#include <string>
#include <vector>

using namespace std;

vector<vector<int>> Tree;
pair<int, int> DFS(const vector<int>& InLosses, const int Head = 1)
{
    const int LossIfHeadParticipation = InLosses[Head - 1];
    
    if(Tree[Head].empty())
    {
        return {0, LossIfHeadParticipation};
    }

    int TotalLosses = 0, MinLossDifference = 1e9;
    bool bMemberParticipation = false;
    
    for(const int Member : Tree[Head])
    {
        const auto& [LossIfNotParticipation, LossIfParticipation] = DFS(InLosses, Member);
        TotalLosses += min(LossIfNotParticipation, LossIfParticipation);

        if(LossIfNotParticipation >= LossIfParticipation)
        {
            bMemberParticipation = true;
        }

        MinLossDifference = min(MinLossDifference, LossIfParticipation - LossIfNotParticipation);
    }

    if(bMemberParticipation)
    {
        return {TotalLosses, LossIfHeadParticipation + TotalLosses};
    }
    else
    {
        return {TotalLosses + MinLossDifference, LossIfHeadParticipation + TotalLosses};
    }
}

int solution(vector<int> InSales, vector<vector<int>> InLinks) 
{
    Tree.resize(InSales.size() + 1);
    for(const vector<int>& Link : InLinks)
    {
        int a = Link[0], b = Link[1];
        Tree[a].emplace_back(b);
    }

    const auto& [LossIfNotParticipation, LossIfParticipation] = DFS(InSales);
    return min(LossIfNotParticipation, LossIfParticipation);
}

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

쀄 κΈ€λ‘œλ§Œ μ λ‹€λ³΄λ‹ˆ μ„€λͺ…이 λ„ˆλ¬΄ 길어진 것 κ°™λ‹€.
ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ AIκ°€ 문제 μΆ”μ²œν•΄μ£ΌκΈΈλž˜ 트라이 ν–ˆλ‹€κ°€ μ‹œμ›ν•˜κ²Œ νŒ¨λ°°ν•΄λ²„λ ΈλŠ”λ°, Level 4 λ¬Έμ œμ˜€λ„€... γ…‹γ…‹

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

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

될듯 말듯 됐닀가 μ•ˆλλ‹€κ°€...

μ–΄μ°Œμ €μ°Œ μž¬κ·€λ₯Ό μ¨μ„œ ν•˜λŠ”κ²Œ μ μ ˆν•œκ±΄ μ•Œμ•˜λŠ”λ°...
κ²°κ΅­ μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€πŸ₯²

λ°˜ν™˜κ°’μ„ νŒ€μž₯이 μžˆλƒ 없냐 2개둜 λ„˜κ²¨μ£ΌλŠ”κ±Έ λ³΄λ‹ˆκΉŒ λ­”κ°€ μ–΄λ–»κ²Œ ν•˜λ©΄ ν•΄κ²°ν•˜λ©΄ λ˜λŠ”μ§€ λΉ‘ μ΄ν•΄ν–ˆμŠ΅λ‹ˆλ‹€.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants