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

36-9kyo-hwang #138

Merged
merged 3 commits into from
Mar 24, 2024
Merged

36-9kyo-hwang #138

merged 3 commits into from
Mar 24, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

@9kyo-hwang 9kyo-hwang commented Mar 18, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

2023 ํ˜„๋Œ€๋ชจ๋น„์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฒฝ์ง„๋Œ€ํšŒ ์ƒ๋‹ด์› ์ธ์›

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„ + ์•ˆ๋– ์˜ฌ๋ผ์„œ ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ ์ฐธ๊ณ ...

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œ ์„ค๋ช…

1~k๋ฒˆ์œผ๋กœ ๋ถ„๋ฅ˜๋˜๋Š” ์ƒ๋‹ด ์œ ํ˜•์ด ์กด์žฌํ•˜๋Š”๋ฐ, ๊ฐ ๋ฉ˜ํ† ๋Š” k๊ฐœ์˜ ์ƒ๋‹ด ์œ ํ˜• ์ค‘ ํ•˜๋‚˜๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.
n๋ช…์˜ ๋ฉ˜ํ† ๋ฅผ k๊ฐœ์˜ ์ƒ๋‹ด ์œ ํ˜•์— ์ ์ ˆํžˆ ๋ฐฐ์ •ํ•ด์„œ ์ฐธ๊ฐ€์ž๋“ค์˜ ์ƒ๋‹ด ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ์ตœ์†Œํ™” ํ•˜๊ณ ์ž ํ•œ๋‹ค.
๊ฐ ์ƒ๋‹ด ์œ ํ˜• ๋ณ„๋กœ ๋ฉ˜ํ† ๊ฐ€ ์ตœ์†Œํ•œ 1๋ช… ์ด์ƒ ๋˜๋„๋ก ์ ์ ˆํžˆ ๋ฐฐ์ •ํ•˜์—ฌ, ์ฐธ๊ฐ€์ž๋“ค์ด ์ƒ๋‹ด์„ ๋ฐ›๊ธฐ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ ์‹œ๊ฐ„์„ ๋ชจ๋‘ ํ•ฉํ•œ ๊ฐ’์˜ ์ตœ์†Ÿ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜๋ผ.

2. ์„ค๋ช…

5๋ช…์˜ ๋ฉ˜ํ† ์— 3๊ฐœ์˜ ์ƒ๋‹ด ์œ ํ˜•์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.
๊ฐ ์œ ํ˜• ๋ณ„๋กœ ์ตœ์†Œํ•œ 1๋ช…์€ ์กด์žฌํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ, ๋ฉ˜ํ† ๋Š” (1, 1, 3), (1, 3, 1), ..., (1, 2, 2), (2, 1, 2), ... ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
์œ ํ˜• ๊ฐœ์ˆ˜๋Š” $1 \le k \le 5$์ด๊ณ , ๋ฉ˜ํ†  ์ˆ˜๋Š” $k \le n \le 20$์œผ๋กœ ์ž‘์•„์„œ ์ด ๋ถ€๋ถ„์€ ์ค‘๋ณต ์กฐํ•ฉ์„ ์ด์šฉํ•ด ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋„ ๋  ๊ฒƒ ๊ฐ™๋‹ค.

mento_assignment_combs = [
    prod for prod in product(range(1, n + 1), repeat=k) if sum(prod) == n  # [1, 2, ..., n] ๋ฆฌ์ŠคํŠธ์—์„œ k๊ฐœ๋ฅผ ๋ฝ‘๋Š”๋ฐ ๋ฝ‘์€ ์ˆซ์ž๋“ค์˜ ํ•ฉ์ด n์ธ ๊ฒฝ์šฐ๋งŒ ์ทจํ•œ๋‹ค.
]

์ด๋Ÿฐ ์‹์œผ๋กœ ์ค‘๋ณต์กฐํ•ฉ(product)์„ ์ด์šฉํ•ด (1, 1, 3), (1, 2, 2), ... ๊ณผ ๊ฐ™์€ ๋ฐฐ์ • ์กฐํ•ฉ์ด mento_assignment_combs์— ์ €์žฅ๋œ๋‹ค.

์ด์ œ ์ด ๋ฉ˜ํ†  ๋ฐฐ์ • ์กฐํ•ฉ์„ ์ด์šฉํ•ด, ๊ฐ ์กฐํ•ฉ๋งˆ๋‹ค ๋Œ€๊ธฐ์‹œ๊ฐ„์„ ๊ตฌํ•ด๋ณด์ž.

ans = float('inf')
for mento_assignment_comb in mento_assignment_combs:
    waiting_cur_comb = sum(
        get_consultation_waiting(category, num_mento)
        for category, num_mento in enumerate(mento_assignment_comb)
    )
    ans = min(ans, waiting_cur_comb)

mento_assignment_combs์•ˆ์— ๋“  ์กฐํ•ฉ์„ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด get_consultation_waiting() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค.
์ด ํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ ์œ ํ˜•(category)๊ณผ ํ•ด๋‹น ์œ ํ˜•์˜ ๋ฉ˜ํ†  ์ˆ˜(num_mento)๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ํ•ด๋‹น ์œ ํ˜•์˜ ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

๊ฐ ์œ ํ˜• ๋ณ„ ๋ฉ˜ํ†  ์ˆ˜๋ฅผ ์ง€์ •ํ•ด์คฌ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ์–ด๋–ค ์‹์œผ๋กœ ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์—ฌ๊ธฐ์„œ ์šฐ์„ ์ˆœ์œ„ํ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

์šฐ์„ ์ˆœ์œ„ํ๋Š” ๋ฉ˜ํ† ์˜ ์ƒ๋‹ด ๊ฐ€๋Šฅ ์‹œ๊ฐ„์„ ๋‹ด๊ณ  ์žˆ๋‹ค. ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์“ฐ๋Š” ์ด์œ ๋Š” ์—ฌ๋Ÿฌ ๋ช…์˜ ๋ฉ˜ํ† ๊ฐ€ ์žˆ์„ ๋•Œ, ์ƒ๋‹ด ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ๋น ๋ฅธ ๋ฉ˜ํ†  ์‹œ๊ฐ„์„ ๋ฐ”๋กœ ๋ฝ‘์•„์“ฐ๊ธฐ ์œ„ํ•ด์„œ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋ฉ˜ํ†  ์ˆ˜๋งŒํผ ์šฐ์„ ์ˆœ์œ„ ํ์˜ ์›์†Œ๊ฐ€ ์กด์žฌํ•˜๋„๋ก ์„ค์ •ํ•œ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์ฝ”๋“œ๋กœ ๋ณด์ž.

def get_consultation_waiting(category, num_mento):
    mento_ticks = [0] * num_mento
    heapify(mento_ticks)
    waiting = 0
    ...

๋ฉ˜ํ†  ์ˆ˜๋งŒํผ 0 ๊ฐœ์ˆ˜๋ฅผ ์ง‘์–ด๋„ฃ์–ด heap์œผ๋กœ ๊ตฌ์„ฑํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 2๋ช…์ด๋ฉด [0, 0]์œผ๋กœ ํž™์„ ๊ตฌ์„ฑํ•œ๋‹ค.
์ด๋Ÿฐ ์‹์œผ๋กœ ๋ฉ˜ํ†  ์ˆ˜๋งŒํผ 0์„ ์ง‘์–ด๋„ฃ์œผ๋ฉด, ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ "๋น„์–ด์žˆ๋Š” ๋ฉ˜ํ† ๋กœ ๋จผ์ € ๋‹ค๊ฐ€๊ฐ€์„œ ์ƒ๋‹ด์„ ํ•˜๋Š”" ํšจ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

์ด์ œ ๋ฉ˜ํ† ๋“ค๋„ ์ค€๋น„๋˜์—ˆ์œผ๋‹ˆ, ์ฐธ๊ฐ€์ž๋“ค์„ ์ฐจ๋ก€๋Œ€๋กœ ์ƒ๋‹ดํ•ด๋ณด์ž.

consultations = defaultdict(list)
for a, b, c in reqs:
    consultations[c - 1].append((a, b))

์šฐ์„  ๊ฐ ์œ ํ˜• ๋ณ„๋กœ ์ฐธ๊ฐ€์ž๋“ค์„ ๋ถ„๋ฆฌํ•ด์„œ ์ €์žฅํ•ด์ค€๋‹ค.

for tick, consultation in consultations[category]:
    fastest_mento = heappop(mento_ticks)
    ....

ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ์— ์žˆ๋Š” ์ฐธ๊ฐ€์ž ์ •๋ณด๋“ค์„ ์ฐจ๋ก€๋Œ€๋กœ ๊บผ๋‚ด ํ™•์ธํ•œ๋‹ค. tick์€ ์ƒ๋‹ด ์‹œ์ž‘ ์‹œ๊ฐ, consultation์€ ์†Œ์š” ์ƒ๋‹ด ์‹œ๊ฐ„์„ ์˜๋ฏธํ•œ๋‹ค.
์šฐ์„  ํ˜„์žฌ ๊ฐ€์žฅ ๋นจ๋ฆฌ ์ƒ๋‹ดํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ˜ํ†  ํ•œ ๋ช…์„ ๊บผ๋‚ธ๋‹ค. ์ตœ์ดˆ์˜ ๊ฒฝ์šฐ 0 ์‹œ๊ฐ„์œผ๋กœ ๋“ค์–ด๊ฐ€์žˆ๋‹ค.

if fastest_mento > tick:
    waiting += fastest_mento - tick
    heappush(mento_ticks, fastest_mento + consultation)
else:
    heappush(mento_ticks, tick + consultation)

์ƒํ™ฉ์€ 2๊ฐ€์ง€๋‹ค. 1. ๋ฉ˜ํ† ๋ฅผ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ 2. ์ง€๊ธˆ ๋ฐ”๋กœ ์ƒ๋‹ด์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
1์˜ ๊ฒฝ์šฐ (๊ฐ€์žฅ ๋น ๋ฅธ ๋ฉ˜ํ†  ์‹œ๊ฐ„ - ์›ํ•˜๋Š” ์ƒ๋‹ด ์‹œ๊ฐ„)๋งŒํผ ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด ๋ฐœ์ƒํ•ด ์ด๋ฅผ ๊ธฐ๋กํ•ด์ฃผ๊ณ , ํž™์—๋Š” (๋ฉ˜ํ† ๊ฐ€ ์ƒ๋‹ด ๊ฐ€๋Šฅํ•œ ์‹œ๊ฐ„ + ํ˜„์žฌ ์ฐธ๊ฐ€์ž๊ฐ€ ์›ํ•˜๋Š” ์ƒ๋‹ด ์‹œ๊ฐ„)์„ ๋„ฃ์–ด์ค€๋‹ค.
2์˜ ๊ฒฝ์šฐ๋Š” ๋ฉ˜ํ† ๊ฐ€ ์ง„์ž‘์— ์•ž์„  ์ƒ๋‹ด์„ ๋๋‚ธ ์ƒํƒœ(fastest_mento < tick)๊ฑฐ๋‚˜, ๋ฐฉ๊ธˆ ๋ฉ˜ํ† ๊ฐ€ ์•ž์„  ์ƒ๋‹ด์„ ๋ง‰ ๋๋‚ธ ๊ฒฝ์šฐ(fastest_mento == tick)์ด๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ธฐ๋กํ•  ํ•„์š”๋Š” ์—†๊ณ , ํž™์—๋Š” (์ฐธ๊ฐ€์ž์˜ ์ƒ๋‹ด ์‹œ๊ฐ + ์ฐธ๊ฐ€์ž์˜ ์ƒ๋‹ด ์†Œ์š” ์‹œ๊ฐ„)๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

return waiting

์œ„ ๊ณผ์ •์ด ์™„๋ฃŒ๋˜๋ฉด ๊ธฐ๋กํ•œ ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

ans = float('inf')
for mento_assignment_comb in mento_assignment_combs:
    waiting_cur_comb = sum(
        get_consultation_waiting(category, num_mento)
        for category, num_mento in enumerate(mento_assignment_comb)
    )
    ans = min(ans, waiting_cur_comb)
return ans

๋‹ค์‹œ ์ด ์ฝ”๋“œ๋กœ ๋Œ์•„์™€์„œ, get_consultation_waiting() ํ•จ์ˆ˜๋ฅผ ๊ฐ ์œ ํ˜• ๋ณ„๋กœ ํ˜ธ์ถœํ•ด ๋”ํ•œ waiting_cur_comb ๊ฐ’์ด "ํ˜„์žฌ ๋ฉ˜ํ†  ๋ฐฐ์ • ์กฐํ•ฉ"์—์„œ์˜ ๋Œ€๊ธฐ์‹œ๊ฐ„์ด๋‹ค. ์ด๋ฅผ ๋ชจ๋“  ์กฐํ•ฉ์— ๋Œ€ํ•ด ๊ฒ€์‚ฌํ•ด, ์ตœ์†Œ๊ฐ’์„ ans์— ๊ธฐ๋กํ•œ ๋’ค ๋ฐ˜ํ™˜ํ•˜๋ฉด ์™„์„ฑ์ด๋‹ค.

3. ์ „์ฒด ์ฝ”๋“œ

from collections import defaultdict
from itertools import product
from heapq import heapify, heappop, heappush

def solution(k, n, reqs):
    # Generating all possible mentor assignment combinations
    mento_assignment_combs = [
        prod for prod in product(range(1, n + 1), repeat=k) if sum(prod) == n
    ]
    
    # Organizing consultations by category
    consultations = defaultdict(list)
    for a, b, c in reqs:
        consultations[c - 1].append((a, b))

    def get_consultation_waiting(category, num_mento):
        # Initialize heap with zeros
        mento_ticks = [0] * num_mento
        heapify(mento_ticks)
        waiting = 0
        
        for tick, consultation in consultations[category]:
            fastest_mento = heappop(mento_ticks)
            
            # Adjust waiting time based on consultation time and mentor availability
            if fastest_mento > tick:
                waiting += fastest_mento - tick
                heappush(mento_ticks, fastest_mento + consultation)
            else:
                heappush(mento_ticks, tick + consultation)
                
        return waiting

    # Calculate waiting time for each mentor assignment combination
    ans = float('inf')
    for mento_assignment_comb in mento_assignment_combs:
        waiting_cur_comb = sum(
            get_consultation_waiting(category, num_mento)
            for category, num_mento in enumerate(mento_assignment_comb)
        )
        ans = min(ans, waiting_cur_comb)
    return ans
#include <string>
#include <vector>
#include <queue>
#include <numeric>
#include <algorithm>

using namespace std;

vector<vector<int>> MentoAssignmentCombos;
vector<int> MentoNums;
vector<vector<pair<int, int>>> ConsultationInfo;

void GetMentoAssignmentCombos(vector<int> MentoCombo, int RemainCategory, int RemainMentoNum)
{
    if(RemainCategory == 0)
    {
        if(RemainMentoNum == 0)
        {
            MentoAssignmentCombos.emplace_back(MentoCombo);
        }
        return;
    }
    
    for(const int NumMento : MentoNums)
    {
        MentoCombo.emplace_back(NumMento);
        GetMentoAssignmentCombos(MentoCombo, RemainCategory - 1, RemainMentoNum - NumMento);
        MentoCombo.pop_back();
    }
}

int GetConsultationWaiting(const int Category, const int NumMento)
{
    priority_queue<int, vector<int>, greater<>> PQ;
    for(int i = 0; i < NumMento; ++i)
    {
        PQ.emplace(0);
    }
    
    int WaitingTime = 0;
    for(const auto& [Tick, Consultation] : ConsultationInfo[Category])
    {
        const int FastestMentoTick = PQ.top();
        PQ.pop();
        
        if(FastestMentoTick > Tick)
        {
            WaitingTime += FastestMentoTick - Tick;
            PQ.emplace(FastestMentoTick + Consultation);
        }
        else
        {
            PQ.emplace(Tick + Consultation);
        }
    }
    
    return WaitingTime;
}

int solution(int k, int n, vector<vector<int>> Reqs) 
{
    MentoNums.assign(n, 0);
    iota(MentoNums.begin(), MentoNums.end(), 1);
    GetMentoAssignmentCombos({}, k, n);
    
    ConsultationInfo.assign(k, {});
    for(const auto& Req : Reqs)
    {
        ConsultationInfo[Req[2] - 1].emplace_back(Req[0], Req[1]);
    }
    
    int Answer = 1e9;
    for(const auto& MentoAssignmentCombo : MentoAssignmentCombos)
    {
        int WaitingCurCombo = 0;
        for(int i = 0; i < k; ++i)
        {
            WaitingCurCombo += GetConsultationWaiting(i, MentoAssignmentCombo[i]);
        }
        Answer = min(Answer, WaitingCurCombo);
    }
    return Answer;
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Ha... SRT์—์„œ ๊ณ„์† ๊ณ ๋ฏผํ•ด๋ดค๋Š”๋ฐ ๊ฐ์ด ์ฐธ ์•ˆ์žกํ˜€์„œ ๊ฒฐ๊ตญ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด๋ฒ„๋ ธ๋‹ค... ์™ค์ผ€ ์•ˆ๋ณด์ด๋Š” ๊ฑธ๊นŒ?

ํ˜ธ์šฐ๋™์˜ ๊ฐœ๋ฐœ ์ผ์ง€ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Level 3 ์ƒ๋‹ด์› ์ธ์›

@tgyuuAn
Copy link
Member

tgyuuAn commented Mar 19, 2024

๋‹น์‹  ์™œ ์ด๋Ÿฐ ๊ฑธ ํ’€์–ด์„œ ๊ธฐ๋ฅผ ์ฃฝ์ด๊ณ  ๊ทธ๋ž˜์š”?!

1 similar comment
@LJEDD2
Copy link

LJEDD2 commented Mar 20, 2024

๋‹น์‹  ์™œ ์ด๋Ÿฐ ๊ฑธ ํ’€์–ด์„œ ๊ธฐ๋ฅผ ์ฃฝ์ด๊ณ  ๊ทธ๋ž˜์š”?!

Copy link
Collaborator

@Dolchae Dolchae left a comment

Choose a reason for hiding this comment

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

๋ฌธ์ œ๊ฐ€ ์ •๋ง ์–ด๋ ต๋„ค์š”..๐Ÿ˜‚ ์ƒ๋‹ด ์‹œ๊ฐ„์„ ์šฐ์„ ์ˆœ์œ„ ํ๋กœ ํ•ด๊ฒฐํ•˜๋ฉด์„œ ์ดˆ๊ธฐ๊ฐ’์„ ๋ฏธ๋ฆฌ 0์œผ๋กœ ์ฑ„์›Œ๋†“๋Š” ์•„์ด๋””์–ด ์ž˜ ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค๐Ÿ‘ ์ˆ˜๊ณ ํ•˜์…จ์–ด์š”๐Ÿ˜Š

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.

์šฐ์œผ์•„ใ…ใ…‡์™• ํ’€์—ˆ๋‹ค์•„ใ… ๐Ÿซ 

์ €๋Š” ์•ฝ๊ฐ„ ๋‹ค๋ฅด๊ฒŒ... ์กฐํ•ฉ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ •๋ ฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค.

ํฌ๊ฒŒ 4๋‹จ๊ณ„๋กœ ๋‚˜๋ˆ„์–ด ํ’€์ดํ–ˆ์Šต๋‹ˆ๋‹ค.

  1. ์ƒ๋‹ด ์œ ํ˜•๋ณ„๋กœ ๋ถ„๋ฆฌํ•œ๋‹ค.
  2. ์ƒ๋‹ด ์œ ํ˜•๋ณ„๋กœ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ง์› ์ˆ˜๋ฅผ ๋Š˜๋ ค์„œ ๊ฐ๊ฐ ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ๊ธฐ์–ตํ•œ๋‹ค.
  3. ์‚ฌ๋žŒ์ด ์ถ”๊ฐ€๋˜๋ฉด์„œ ์–ผ๋งˆ๋‚˜ ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€๋ฅผ ๊ตฌํ•œ๋‹ค.
    (4๋ฒˆ ๊ณ„์‚ฐํ•  ๋•Œ ๋ˆ„์ ๋œ ๋ถ€๋ถ„ ์ œ์™ธํ•˜๊ธฐ ์œ„ํ•œ ๊ณผ์ •)
  4. ๋ชจ๋“  ์œ ํ˜•์—์„œ ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด ์—†์„ ๋•Œ๋ณด๋‹ค ๋ช‡ ๋ช… ๋ถ€์กฑํ•œ์ง€ ๊ตฌํ•˜๊ณ , ๊ทธ ๋ฒ”์œ„๋งŒํผ ๋Œ€๊ธฐ ์‹œ๊ฐ„ ์ดํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

ํ•œ ๋‹จ๊ณ„์”ฉ ์•Œ์•„๋ณด๊ธฐ

[ 1๋‹จ๊ณ„ ] ์ƒ๋‹ด ์œ ํ˜•๋ณ„ ๋ถ„๋ฆฌ
reqs_type = [[] for _ in range(k+1)]

for start, care, care_type in reqs:
    reqs_type[care_type].append((start, care))

์œ ํ˜•์˜ ์ข…๋ฅ˜๋งŒํผ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค. ( +1 >> 0 ์ œ์™ธํ•˜๊ธฐ ์œ„ํ•จ )

์ƒ๋‹ด ์œ ํ˜•์— ๋”ฐ๋ผ ์ƒ๋‹ด ์‹œ์ž‘ ์‹œ๊ฐ„๊ณผ ์ƒ๋‹ด ์‹œ๊ฐ„์„ ๋‹ค์‹œ ๋„ฃ์–ด์ค€๋‹ค.


[ 2๋‹จ๊ณ„ ] ์œ ํ˜•๋ณ„ ์ง์› ์ˆ˜์— ๋”ฐ๋ฅธ ๋Œ€๊ธฐ ์‹œ๊ฐ„ ๊ตฌํ•˜๊ธฐ

์œ ํ˜•๋ณ„ ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด๊ณ  ์ง์› ์ˆ˜์— ๋”ฐ๋ผ ๊ฑธ๋ฆฌ๋Š” ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ๊ตฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

์ง์› ์ˆ˜์˜ ๊ฒฝ์šฐ ์ตœ๋Œ€ ๊ฐ€๋Šฅํ•œ ์ง์› ์ˆ˜๊นŒ์ง€ ๋Š˜๋ฆฌ๋ฉด ๋œ๋‹ค.
( ๋‹ค๋งŒ ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด 0์ด ๋˜๋ฉด ์˜๋ฏธ ์—†์–ด์„œ ์ข…๋ฃŒ )

์ƒ๋‹ด์›์ด ๋‹ค์Œ ๊ณ ๊ฐ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์‹œ์ ์„ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐฑ์‹ ํ•˜๋ฉด์„œ
๊ตฌํ•˜๋ฉด ๋œ๋‹ค.

for customer_list in reqs_type[1:]:
    
    if customer_list == []:
        continue
    
    care_cnt = 1
    delay = 0
    delay_per_care = []
    
    while care_cnt <= max_care:
        
        # ์ƒ๋‹ด์› ์ˆ˜๋งŒํผ 0์„ ๋งŒ๋“ค์–ด์„œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ํ†ตํ•ด ๊ตฌํ•œ๋‹ค.
        next_care = [0 for delay_per_care in range(care_cnt)]
        heapify(next_care)
        
        delay = 0
        
       # ๋จผ์ € ์ƒ๋‹ด์ด ๋๋‚˜๋Š” ์‹œ๊ฐ„์„ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•˜์—ฌ pop
       # ๋งˆ์ง€๋ง‰ ๊ณ ๊ฐ๊นŒ์ง€ ๋Œ๋ฉด์„œ ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ๋”ํ•œ๋‹ค.
       # ๋๋‚˜๋Š” ์‹œ๊ฐ„์„ ๋‹ค์‹œ push
        for start, care in customer_list:
            nc = heappop(next_care)
            delay += max(0, nc-start)
            heappush(next_care, max(nc, start)+care)
        
        if delay == 0:
            break
        
        delay_per_care.append(delay)
        care_cnt += 1

[ 3๋‹จ๊ณ„ ] ๊ฒน์น˜๋Š” ๋Œ€๊ธฐ ์‹œ๊ฐ„ ๋นผ์ฃผ๊ธฐ

ํ•ด๋‹น ๋ฐ˜๋ณต๋ฌธ์€ 2๋‹จ๊ณ„์˜ ๋ฐ˜๋ณต๋ฌธ ์•ˆ์— ์žˆ์Šต๋‹ˆ๋‹ค!
( ์ „์ฒด ์ฝ”๋“œ๋ณด๋ฉด ๋ฌด์Šจ ๋ง์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ์–ด์š”!)

    for i in range(len(delay_per_care)):
        delay_meno.append(delay_per_care[i]
                        -(0 if i+1 == len(delay_per_care) else delay_per_care[i+1]))
                        
        delay_cnt += 1

ํ•œ ์œ ํ˜•์—์„œ i๋ช…์˜ ์‚ฌ๋žŒ์ด ์žˆ์„ ๋•Œ์™€ i+1๋ช…์˜ ์‚ฌ๋žŒ์ด ์žˆ์„ ๋•Œ, ์–ผ๋งˆ๋‚˜ ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด ์ค„์—ˆ๋Š”์ง€๋ฅผ delay_meno(๋Œ€๊ธฐ์‹œ๊ฐ„ ๋ฉ”๋ชจ)์— ๋„ฃ์–ด์ค€๋‹ค.

๋งŒ์•ฝ i๊ฐ€ ๋งจ ๋์— ๋„๋‹ฌํ–ˆ๋‹ค๋ฉด ๋งจ ๋ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

delay_cnt๋Š” ์ด ๋“ค์–ด๊ฐ„ ์ƒ๋‹ด์›์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— 1์”ฉ ๋”ํ•ด์ค€๋‹ค.

4๋‹จ๊ณ„๋ฅผ ์œ„ํ•œ ๊ณผ์ •์ด๋ผ๊ณ  ๋ณด๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.
์‚ฌ๋žŒ์ด ์ถ”๊ฐ€๋˜๋ฉด์„œ ์ด์ „๋ณด๋‹ค ์–ผ๋งˆ๋‚˜ ์‹œ๊ฐ„์ด ์ค„์—ˆ๋Š”์ง€ ๊ฐ’์ด ๋ชจ์ด๊ฒŒ๋˜๊ณ ...
์ด ๊ฐ’๋“ค ํ•˜๋‚˜ํ•˜๋‚˜๋Š” ๊ฒฐ๊ตญ ์‚ฌ๋žŒ์ด ํ•œ ๋ช… ์—†์„ ๋•Œ๋งˆ๋‹ค ์ƒ๊ธฐ๋Š” ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.


[ 4๋‹จ๊ณ„ ] ์‚ฌ๋žŒ์ด ๋ถ€์กฑํ•œ ๋งŒํผ ์ถ”๊ฐ€๋กœ ๊ฑธ๋ฆฌ๋Š” ๋Œ€๊ธฐ ์‹œ๊ฐ„์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ

๋Œ€๊ธฐ ์‹œ๊ฐ„์ด 0์ด ๋˜์ง€ ์•Š๋Š” ์ด์ƒ ์œ ํ˜•์—์„œ ๋งค๋ฒˆ ์ตœ๋Œ€ํ•œ์œผ๋กœ ์ƒ๋‹ด์›์„ ๋„ฃ์—ˆ๊ธฐ ๋•Œ๋ฌธ์—
์ฃผ์–ด์ง„ ์ƒ๋‹ด์›๋ณด๋‹ค ๋” ๋งŽ์€ ์ƒ๋‹ด์›์ด ๊ณ„์‚ฐ์— ๋“ค์–ด๊ฐ”๋‹ค.

์ฃผ์–ด์ง„ ์ƒ๋‹ด์› ์ˆ˜๋ฅผ ๋งž์ถ”๋ฉด์„œ
๊ทธ๋งŒํผ ๋Š˜์–ด๋‚˜๋Š” ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์ƒ๋‹ด์›์ด ํ•œ ๋ช… ๋Š˜์–ด๋‚˜๋ฉด, ์ด์ „์— ๋น„ํ•ด ์—„์ฒญ๋‚˜๊ฒŒ ๋งŽ์€ ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
( ์—ฌ๋Ÿฌ ๋ฒˆ ์ค‘์ฒฉํ•œ ๋ถ€๋ถ„์„ ๋งŽ์ด ์—†์•จ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ค„์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. )
๋ฐ˜๋ฉด์— ์ƒ๋‹ด์›์ด ์ ์ฐจ ๋Š˜์–ด๋‚ ์ˆ˜๋ก ์ด์ „์— ๋น„ํ•ด ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด ์ค„์–ด๋“ ๋‹ค.

์‰ฝ๊ฒŒ ๋งํ•˜์ž๋ฉด, ์ƒ๋‹ด์› ์ˆ˜๋ฅผ ํ•œ ๋ช… ๋นผ๋ฉด, ๊ฐ€์žฅ ์ž‘์€ ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ๋”ํ•˜๋ฉด ๋œ๋‹ค.
( ์–ด๋Š ์œ ํ˜•์ผ์ง€ ์ƒ๊ด€์—†์ด ๋Œ€๊ธฐ ์‹œ๊ฐ„๋งŒ ๋”ฐ์ง€๊ธฐ์— ๊ฐ€๋Šฅํ•œ ์ผ์ด๋‹ค. )

delay_meno.sort()
print(sum(delay_meno[:delay_cnt-(n-k)]))

๊ทธ๋ž˜์„œ ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ์ •๋ ฌํ•˜๊ณ ,

๊ตฌํ•œ ๋Œ€๊ธฐ ์‹œ๊ฐ„์˜ ์ˆ˜์—์„œ ( delay_cnt )
์—ฌ์œ  ์ƒ๋‹ด์› ์ˆ˜๋ฅผ ๋นผ์ฃผ๋ฉด ( n-k : ๊ฐ ์œ ํ˜•์— 1๋ช…์”ฉ ๋ฌด์กฐ๊ฑด ๋ฐฐ์น˜ํ•˜๊ณ  ๋‚จ์€ ์ƒ๋‹ด์› )
๋นผ์•ผํ•˜๋Š” ์ƒ๋‹ด์› ์ˆ˜๊ฐ€ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์ˆ˜๋งŒํผ ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.


์ „์ฒด ์ฝ”๋“œ
from heapq import *

def solution(k, n, reqs):
    reqs_type = [[] for _ in range(k+1)]
    
    for start, care, care_type in reqs:
        reqs_type[care_type].append((start, care))
        
    max_care = 1 + n - k

    delay_meno = []
    delay_cnt = 0

    for customer_list in reqs_type[1:]:

        if customer_list == []:
            continue

        care_cnt = 1
        delay = 0
        delay_per_care = []

        while care_cnt <= max_care:

            next_care = [0 for delay_per_care in range(care_cnt)]
            heapify(next_care)

            delay = 0

            for start, care in customer_list:
                nc = heappop(next_care)
                delay += max(0, nc-start)
                heappush(next_care, max(nc, start)+care)

            if delay == 0:
                break

            delay_per_care.append(delay)
            care_cnt += 1
        
        cnt = len(delay_per_care)
        for i in range(cnt):
            delay_meno.append(delay_per_care[i]-(0 if i+1 == cnt else delay_per_care[i+1]))
            delay_cnt += 1

    delay_meno.sort()
    return sum(delay_meno[:delay_cnt-(n-k)])

delay_meno... memo ์˜คํƒ„๋ฐ ํ•ด ๋œจ๊ธฐ ์ง์ „์— ๊ฑฐ์˜ ๋‹ค ์ผ์„ ๋•Œ ๋ด์„œ... ๊ณ ์น˜๊ธฐ ๊ท€์ฐฎ... ํž˜๋“ค์–ด์„œ ๋‚ด๋ฒ„๋ ค๋‘๊ฒ ์Šต๋‹ˆ๋‹น... ๋„ˆ๊ทธ๋Ÿฌ์ด ๋ด์ฃผ์„ธ์šฅ ๐Ÿซ 

Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

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

ํ’€์–ด๋ณผ๊นŒ ํ–ˆ์œผ๋‚˜.. ์—ญ์‹œ ์–ด๋ ต๋„ค์š”...๐Ÿ˜‚
์ƒ๋‹ด ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ์ตœ์†Œํ™”ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ์ธ๋ฐ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๋– ์˜ฌ๋ฆฌ์ง€ ๋ชปํ–ˆ๋„ค์š”..
๊ทธ๋ฆฌ๊ณ  ๋‘ ๊ฐ€์ง€ ์ƒํ™ฉ์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ƒ๊ฐํ•˜๋Š” ๋ถ€๋ถ„๋„ ์ €๋Š” ์ƒ๊ฐ์ง€ ๋ชปํ–ˆ๋Š”๋ฐ ์—ญ์‹œ..๐Ÿ‘

@9kyo-hwang 9kyo-hwang merged commit 3754123 into main Mar 24, 2024
@9kyo-hwang 9kyo-hwang deleted the 36-9kyo-hwang branch March 24, 2024 08:57
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.

6 participants