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

60-9kyo-hwang #213

Merged
merged 11 commits into from
Sep 19, 2024
Merged

60-9kyo-hwang #213

merged 11 commits into from
Sep 19, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

@9kyo-hwang 9kyo-hwang commented Aug 5, 2024

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

๊ณผ์ œ ์ง„ํ–‰ํ•˜๊ธฐ

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

1์‹œ๊ฐ„ ์กฐ๊ธˆ ๋„˜๊ฒŒ...?

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

1. ๋ฌธ์ œ

๊ณผ์ œ ์ˆ˜ํ–‰์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ [name, start, playtime] ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง„๋‹ค. name์€ ๊ณผ์ œ ์ด๋ฆ„, start๋Š” hh:mm ํ˜•ํƒœ์˜ ์‹œ์ž‘ ์‹œ๊ฐ ๋ฌธ์ž์—ด, playtime์€ ๊ณผ์ œ๋ฅผ ์™„๋ฃŒํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด๋‹ค.

์‹œ๊ฐ„ ์ˆœ์„œ์— ๋งž๊ฒŒ ๊ณผ์ œ๋ฅผ ์‹œ์ž‘ํ•˜๋Š”๋ฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทœ์น™์ด ์กด์žฌํ•œ๋‹ค.

  • ๊ณผ์ œ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๋„์ค‘์— ์ƒˆ๋กœ์šด ๊ณผ์ œ๋ฅผ ์‹œ์ž‘ํ•ด์•ผ ํ•˜๋Š” ์‹œ๊ฐ์ด ๋˜๋ฉด, ๊ธฐ์กด ๊ณผ์ œ๋Š” ์ค‘๋‹จํ•˜๊ณ  ์ƒˆ๋กœ์šด ๊ณผ์ œ๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.
  • ์ง„ํ–‰ ์ค‘์ด๋˜ ๊ณผ์ œ๋ฅผ ๋๋ƒˆ์„ ๋•Œ, ์ž ์‹œ ์ค‘๋‹จํ–ˆ๋˜ ๊ณผ์ œ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๊ณผ์ œ๋ฅผ ๋งˆ์ € ํ•œ๋‹ค. ๋‹จ ์ด ๋•Œ ์ƒˆ๋กœ์šด ๊ณผ์ œ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, ์ค‘๋‹จํ–ˆ๋˜ ๊ณผ์ œ๋ฅผ ํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๊ณผ์ œ๋ฅผ ๋จผ์ € ํ•œ๋‹ค.
  • ์ค‘๋‹จํ–ˆ๋˜ ๊ณผ์ œ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๋ฉด ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋ฉˆ์ถ˜ ๊ณผ์ œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

๊ณผ์ œ๋ฅผ ๋๋‚ธ ์ˆœ์„œ๋Œ€๋กœ ์ด๋ฆ„์„ ๋ฐฐ์—ด์— ๋‹ด์•„ returnํ•˜๋ผ.

2. ํ’€์ด

๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ๋กœ์ง์„ ์ฐจ๋ถ„ํžˆ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ๋‹ค.

class FPlan
{
public:
    string Name;
    int Start;
    int Playtime;
    
    FPlan() {}
    FPlan(const string& InName, const string& InStart, const string& InPlaytime)
    {
        Name = InName;
        Start = stoi(InStart.substr(0, 2)) * 60 + stoi(InStart.substr(3, 2));
        Playtime = stoi(InPlaytime);
    }
    
    const bool operator<(const FPlan& Rhs) const
    {
        return Start < Rhs.Start;
    }
};

๊ณผ์ œ ์ •๋ณด์— ๋Œ€ํ•œ ํด๋ž˜์Šค๋ฅผ ์ƒˆ๋กœ ์ •์˜ํ–ˆ๋‹ค.

  • start, playtime ์ •๋ณด๊ฐ€ ๋ฌธ์ž์—ด๋กœ ์ฃผ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— intํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•ด ์ €์žฅํ•œ๋‹ค. ์ด ๋•Œ ์‹œ๊ฐ ์ •๋ณด๋Š” ๋ถ„ ๋‹จ์œ„๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
  • ๋˜ํ•œ ๊ณผ์ œ ์ •๋ณด ๊ฐ„์—๋Š” ์‹œ์ž‘ ์‹œ๊ฐ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋ผ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, start ์ •๋ณด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋„๋ก < ์—ฐ์‚ฐ์ž๋ฅผ ์˜ค๋ฒ„๋กœ๋”ฉํ–ˆ๋‹ค(java์˜ compareTo() ๋ฉ”์„œ๋“œ, ํŒŒ์ด์ฌ์˜ labmda x: x[1]๊ณผ ๋™์ผํ•œ ์—ญํ• ).
vector<string> solution(vector<vector<string>> InPlans) 
{
    vector<FPlan> Plans;
    for(const vector<string>& Plan : InPlans)
    {
        Plans.push_back({Plan[0], Plan[1], Plan[2]});
    }
    
    sort(Plans.begin(), Plans.end());
    
    ...
}

๊ธฐ๋ณธ์ ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๊ณผ์ œ ์ •๋ณด๋“ค์„ ์ƒˆ๋กœ ์ •์˜ํ•œ FPlan ํด๋ž˜์Šค๋กœ ๋ณ€ํ™˜ํ•ด ์ €์žฅํ•œ ๋’ค, ์‹œ์ž‘ ์‹œ๊ฐ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์‹œํ‚จ๋‹ค.

vector<string> Result;  // ์™„๋ฃŒ๋œ ๊ณผ์ œ ์ด๋ฆ„๋“ค์„ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
vector<int> Paused;  // ์ผ์‹œ์ ์œผ๋กœ ์ค‘๋‹จํ•œ ๊ณผ์ œ๋“ค์˜ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•  ์Šคํƒ

for(int i = 0; i < Plans.size() - 1; ++i)
{
    ...
}

Result.emplace_back(Plans.back().Name);

์ €์žฅ๋œ ๊ณผ์ œ ์ •๋ณด๋ฅผ ์ˆœํšŒํ•œ๋‹ค. ๋‹จ ๋งจ ๋งˆ์ง€๋ง‰ ์ •๋ณด๋Š” ์ˆœํšŒํ•  ํ•„์š” ์—†์ด ๋งˆ์ง€๋ง‰์— ๋ฌด์กฐ๊ฑด ์™„๋ฃŒ๋˜๊ธฐ ๋•Œ๋ฌธ์—, Plans.size() - 1 ๋งŒํผ๋งŒ ์ˆœํšŒํ•œ๋‹ค.

int TimeDifference = Plans[i + 1].Start - Plans[i].Start;
if(Plans[i].Playtime > TimeDifference)
{
    Plans[i].Playtime -= TimeDifference;
    Paused.emplace_back(i);
    continue;
}

...

i + 1๋ฒˆ์งธ ์ •๋ณด์™€ i๋ฒˆ์งธ ์ •๋ณด์˜ ์‹œ์ž‘ ์‹œ๊ฐ์˜ ์ฐจ๋ฅผ ํ†ตํ•ด ์‹œ๊ฐ„ ์ฐจ TimeDifference๋ฅผ ๊ตฌํ•œ๋‹ค.
ํ•ด๋‹น ์‹œ๊ฐ„ ์ฐจ๊ฐ€ ํ˜„์žฌ i๋ฒˆ์งธ ๊ณผ์ œ ์†Œ์š” ์‹œ๊ฐ„๋ณด๋‹ค ์งง์„ ๊ฒฝ์šฐ, i๋ฒˆ์งธ ๊ณผ์ œ๋Š” TimeDifference๋งŒํผ๋งŒ ๊ณผ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  Paused์— ์ €์žฅ๋œ๋‹ค.

TimeDifference -= Plans[i].Playtime;
Plans[i].Playtime = 0;
Result.emplace_back(Plans[i].Name);

while(TimeDifference > 0 && !Paused.empty())
{
    int Index = Paused.back();
    if(Plans[Index].Playtime > TimeDifference)
    {
        Plans[Index].Playtime -= TimeDifference;
        break;
    }
    
    TimeDifference -= Plans[Index].Playtime;
    Plans[Index].Playtime = 0;
    Result.emplace_back(Plans[Index].Name);
    Paused.pop_back();
}

์•„๋‹ˆ๋ผ๋ฉด i๋ฒˆ์งธ ๊ณผ์ œ๋Š” ์™„๋ฃŒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ Result ๋ฆฌ์ŠคํŠธ์— ํ•ด๋‹น ๊ณผ์ œ์˜ ์ด๋ฆ„์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
๊ทธ ํ›„ ์ค‘๋‹จํ•œ ๊ณผ์ œ๋“ค์„ ํ™•์ธํ•œ๋‹ค.

  • ๋งŒ์•ฝ ์ค‘๋‹จํ•œ ๊ณผ์ œ๊ฐ€ ๋‚จ์€ ์‹œ๊ฐ„ ์ฐจ ์•ˆ์— ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ๋” ์ด์ƒ ํ™•์ธํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๊ณผ์ œ ์ •๋ณด๋ฅผ ํ™•์ธํ•œ๋‹ค.
  • ์•„๋‹ˆ๋ผ๋ฉด ์ค‘๋‹จํ•œ ๊ณผ์ œ๋ฅผ ์™„๋ฃŒํ•˜๊ณ  Stack์—์„œ ์ œ๊ฑฐํ•œ๋‹ค.
  • ์ด ๊ณผ์ •์—์„œ ๋‚จ์€ ์‹œ๊ฐ„์„ ๊ฐฑ์‹ ํ•œ๋‹ค

์—ฌ๋‹ด) ์ด ๋ถ€๋ถ„์—์„œ ์‚ฝ์งˆ์„ ์ข€ ํ•ด์„œ ํ…Œ์ผ€ 4~6๊ฐœ๋ฅผ ๊ณ„์† ํ‹€๋ ธ๋‹ค... ์ค‘๋‹จ๋œ ๊ณผ์ œ๋ฅผ 2๊ฐœ ์ด์ƒ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— while๋ฌธ์œผ๋กœ ํ™•์ธํ•˜๊ณ , ์ค‘๋‹จ๋œ ๊ณผ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉด์„œ ๋‚จ์€ ์‹œ๊ฐ„ ์ฐจ๊ฐ€ ์ค„์–ด๋“ค๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๊ฐฑ์‹ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

while(!Paused.empty())
{
    Result.emplace_back(Plans[Paused.back()].Name);
    Paused.pop_back();
}

์ด๋ ‡๊ฒŒ Plans ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ๊ณผ์ œ ์ •๋ณด๋ฅผ ๋ชจ๋‘ ํ™•์ธํ–ˆ๋‹ค๋ฉด, ๋งˆ์ง€๋ง‰์œผ๋กœ ์ค‘๋‹จ๋œ ๊ณผ์ œ๋“ค์ด ๋‚จ์•„์žˆ๋Š” ์ง€ ํ•œ ๋ฒˆ ๋” ํ™•์ธํ•ด ๋งˆ๋ฌด๋ฆฌํ•œ๋‹ค.

์ „์ฒด ์ฝ”๋“œ

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class FPlan
{
public:
    string Name;
    int Start;
    int Playtime;
    
    FPlan() {}
    FPlan(const string& InName, const string& InStart, const string& InPlaytime)
    {
        Name = InName;
        Start = stoi(InStart.substr(0, 2)) * 60 + stoi(InStart.substr(3, 2));
        Playtime = stoi(InPlaytime);
    }
    
    const bool operator<(const FPlan& Rhs) const
    {
        return Start < Rhs.Start;
    }
};

vector<string> solution(vector<vector<string>> InPlans) 
{
    vector<FPlan> Plans;
    for(const vector<string>& Plan : InPlans)
    {
        Plans.push_back({Plan[0], Plan[1], Plan[2]});
    }
    
    sort(Plans.begin(), Plans.end());
    
    vector<string> Result;
    vector<int> Paused;
    
    for(int i = 0; i < Plans.size() - 1; ++i)
    {
        int TimeDifference = Plans[i + 1].Start - Plans[i].Start;
        if(Plans[i].Playtime > TimeDifference)
        {
            Plans[i].Playtime -= TimeDifference;
            Paused.emplace_back(i);
            continue;
        }
        
        TimeDifference -= Plans[i].Playtime;
        Plans[i].Playtime = 0;
        Result.emplace_back(Plans[i].Name);
        
        while(TimeDifference > 0 && !Paused.empty())
        {
            int Index = Paused.back();
            if(Plans[Index].Playtime > TimeDifference)
            {
                Plans[Index].Playtime -= TimeDifference;
                break;
            }
            
            TimeDifference -= Plans[Index].Playtime;
            Plans[Index].Playtime = 0;
            Result.emplace_back(Plans[Index].Name);
            Paused.pop_back();
        }
    }
    
    Result.emplace_back(Plans.back().Name);
    
    while(!Paused.empty())
    {
        Result.emplace_back(Plans[Paused.back()].Name);
        Paused.pop_back();
    }
    
    return Result;
}

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

์‹œ๊ฐ„ ๋‹ค๋ฃจ๋Š” ๋ฌธ์ œ๋Š” ํ•ญ์ƒ ํ‘ธ๋Š” ๋ฐ ๊ณจ์น˜๊ฐ€ ์•„ํ”„๋‹ค...

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

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

์˜ˆ์ „์— ์ž๋ฐ”๋กœ ํ’€์—ˆ์„ ๋•Œ๋Š” ํด๋ž˜์Šค ๊ธฐ์ค€์œผ๋กœ ํ’€์—ˆ์—ˆ๋Š”๋ฐ,
ํŒŒ์ด์ฌ์œผ๋กœ ํ’€๋‹ค๋ณด๋‹ˆ ์ข€ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜์žˆ์—ˆ์Šต๋‹ˆ๋‹ค!

์ €๋„ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ƒˆ๋กœ ์ฃผ์–ด์ง„ ๊ณผ์ œ๋ฅผ ๊ฐฑ์‹ ํ•˜๊ณ ,
์Šคํƒ ํ•˜๋‚˜๋กœ ์ด์ „ ๊ณผ์ œ๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ท.

def solution(plans):
    stack = []
    result = []
    
    for plan in plans:
        hour, minute = map(int, plan[1].split(':'))
        start_time = hour * 60 + minute
        plan[1] = start_time
        plan[2] = int(plan[2])
        
    # ์‹œ์ž‘ ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ๊ณ„ํš ์ •๋ ฌ
    plans.sort(key=lambda x: x[1])
    
    for i in range(len(plans) - 1):
        current_plan = plans[i]
        next_plan = plans[i + 1]
        
        stack.append([current_plan[0], current_plan[2]])  
        time_gap = next_plan[1] - current_plan[1]  
        
        # ์Šคํƒ์„ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ ๋นˆ ์‹œ๊ฐ„์„ ์†Œ์ง„
        while stack and time_gap > 0:
            top_plan, remaining_time = stack[-1] 
            
            if remaining_time <= time_gap:
                stack.pop()
                time_gap -= remaining_time
                result.append(top_plan)  
                
            else:
                stack[-1][1] -= time_gap
                time_gap = 0

    result.append(plans[-1][0])
    
    while stack:
        result.append(stack.pop()[0])
    
    return result

@9kyo-hwang 9kyo-hwang merged commit 30b109d into main Sep 19, 2024
5 checks passed
@9kyo-hwang 9kyo-hwang deleted the 60-9kyo-hwang branch September 19, 2024 05:32
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