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

16-kangrae-jo #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

16-kangrae-jo #59

wants to merge 1 commit into from

Conversation

kangrae-jo
Copy link
Collaborator

@kangrae-jo kangrae-jo commented Jan 14, 2025

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

[ํผ์ฆ ๊ฒŒ์ž„ ์ฑŒ๋ฆฐ์ง€]

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

48m

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

PCCP ๊ธฐ์ถœ๋ฌธ์ œ 2๋ฒˆ์ž…๋‹ˆ๋‹ค.

๋ฌธ์ œ๋ฅผ ์ฝ์–ด๋ณด์‹œ๋ฉด ๊ตฌํ˜„ + ํƒ์ƒ‰ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜ ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค ๊ฒƒ ์ž…๋‹ˆ๋‹ค.
์ €๋Š” ์ ์ ˆํ•œ level์ด ๋‚˜์˜ฌ ๋•Œ ๊นŒ์ง€ ํผ์ฆ์„ ํ‘ธ๋Š”๋กœ์ง์„ ๋ฐ˜๋ณตํ•˜๋Š” ๊ทธ๋Ÿฐ ์†”๋ฃจ์…˜์ด ๋– ์˜ฌ๋ž์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ํผ์ฆ์„ ํ‘ธ๋Š” ์€ ๊ตฌํ˜„ ์ด๋ผ๊ณ  ์ƒ๊ฐ์ด ๋˜๊ณ ,
์ ์ ˆํ•œ level์„ ์ฐพ๋Š”๊ฑด ํƒ์ƒ‰์ด๋ผ๊ณ  ์ƒ๊ฐ ๋˜๋„ค์š”.

๊ตฌํ˜„์€.... ๊ฐ์ž์˜ ๋ชซ์ž…๋‹ˆ๋‹ค...
๋ฌธ์ œ๋ฅผ ์ฝ์–ด๋ณด๋ฉด ์ด๋Ÿฐ ๋กœ์ง์„ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์–ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋„ค์š”!

long long prev = level - diffs[i];
if (prev < 0) time += -prev * (times[i-1] + times[i]);
time += times[i];

(์•„๋งˆ ๋” ๊ฐ„๊ฒฐํ•˜๊ฑฐ๋‚˜ ํ•œ๋ˆˆ์— ๋ณด๊ธฐ ์‰ฌ์šด ๊ตฌํ˜„๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ์ฃ ?)

์ด์ œ ์ง„์งœ ๋ฌธ์ œ๋Š” ํƒ์ƒ‰์ž…๋‹ˆ๋‹ค. ์ ์ ˆํ•œ level ์ฐพ๊ธฐ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์–ด์š”.

1. level์„ 1๋ถ€ํ„ฐ limit๊นŒ์ง€ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ, time์ด ์ฒ˜์Œ limit๋ฅผ ๋„˜์–ด์„ค ๋•Œ์˜ level๊ฐ’ ์ด ์ •๋‹ต
2. level์„ ์ด๋ถ„ ํƒ์ƒ‰ํ•˜๋ฉฐ start > end ๊ฐ€ ๋˜์—ˆ์„ ๋•Œ์˜ level๊ฐ’ ์ด ์ •๋‹ต
.
.
.

์ €๋Š” ์ฒ˜์Œ์—๋Š” 1๋ฒˆ ๋ฐฉ์‹์œผ๋กœ ๋น ๋ฅด๊ฒŒ ๊ตฌํ˜„ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. (์ฒ˜์Œ์—๋Š” ์ด๋ถ„ํƒ์ƒ‰์ด ๋– ์˜ค๋ฅด์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹คใ…œ)
์•„๋‹ˆ๋‚˜ ๋‹ค๋ฅผ๊นŒ ์‹œ๊ฐ„์ดˆ๊ณผ, ๋ช‡ ๋ช‡ ์ผ€์ด์Šค ์—์„œ๋Š” ์˜ค๋‹ต๊นŒ์ง€ ๋‚˜๋”๋ผ๊ตฌ์š”.

ํƒ์ƒ‰์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๊ณ , ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ์ง„ํ–‰ํ•ด๋ณด์•˜์—ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ... ์ด๋ถ„ํƒ์ƒ‰์„ ๊ตฌํ˜„ํ•˜์ง€ ๋ชปํ•˜๊ฒ ๋”๋ผ๊ณ ์š”.....

์ •ํ™•ํžˆ ๋งํ•˜๋ฉด ์ด๋ถ„ํƒ์ƒ‰ ์ž์ฒด๋Š” ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ ์—ฌ์ „ํžˆ ๋ช‡๋ช‡ ์ผ€์ด์Šค์—์„œ๋Š” ์˜ค๋‹ต์ด์—ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด ํ•œ ๋ฒˆ ๋ดค์Šต๋‹ˆ๋‹ค.

์—ญ์‹œ ์ด๋ถ„ํƒ์ƒ‰ ์ด์—ˆ๊ณ , ๋“ฑํ˜ธ ๊ฐ™์€ ๊ฒƒ์— ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค!
๋Œ€์†Œ๋ฅผ ๋น„๊ตํ•˜๋Š” ์ผ๋ถ€ ์ฝ”๋“œ์— ๋“ฑํ˜ธ๊ฐ€ ์žˆ๊ณ  ์—†๊ณ ์˜ ์ฐจ์ด๊ฐ€ ๋‚ฌ์—ˆ๋”๋ผ๊ณ ์š”.
๊ทธ๊ฒƒ์„ ๊ณ ์น˜๋‹ˆ ์ •๋‹ต์ด์—ˆ์Šต๋‹ˆ๋‹ค.

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

ํƒ์ƒ‰ํ•  ๋ฒ”์œ„๊ฐ€ ๋„“์„ ๊ฒฝ์šฐ์—๋Š” BinarySearh๋ฅผ ํ™œ์šฉํ•˜์ž!
BinarySearh๋ฅผ ํ™œ์šฉํ•  ๋•Œ๋Š” breakํ•˜๋Š” ์กฐ๊ฑด, start = mid +1ํ•˜๋Š” ์กฐ๊ฑด, end = mid -1ํ•˜๋Š” ์กฐ๊ฑด์„ ์ž˜ ์ •ํ•˜์ž!

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

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

์ฒ˜์Œ์— ์ผ๋ฐ˜์ ์ธ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ 4๊ฐœ๋งŒ ์‹คํŒจํ•ด์„œ ๋ญ๊ฐ€ ๋ฌธ์ œ์ง€ ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ๊ฐ•๋ž˜๋‹˜์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ์ด์ง„ํƒ์ƒ‰์ธ ๊ฑธ ๊นจ๋‹ซ๊ณ  ๋ฐ”๋กœ ๋ฐ”๊ฟจ์Šต๋‹ˆ๋‹ค.

๋ฐ”๊พธ๋ฉด์„œ ๋“ค์—ˆ๋˜ ์˜๋ฌธ์€ ์–ด๋–ป๊ฒŒ ๋‹ต์„ ์ฐพ์ง€? ์˜€์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ์ ‘๊ทผํ•  ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ์ฐพ๋Š” level ๊ฐ’์ด ๋‹ต์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๋†’์€ level์€ ๋ชจ๋“  ํผ์ฆ์„ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•˜๊ณ  ์ œํ•œ ์‹œ๊ฐ„๋‚ด์— ๋ฐ˜๋“œ์‹œ ํ’€ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— level์„ ๊ฐ€์žฅ ๋จผ์ € ์ฐพ๋Š”๊ฒŒ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค์Œ์˜ ์ฝ”๋“œ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค.

์ฒ˜์Œ ์ฝ”๋“œ
//
// Created by ๊น€๊ท ํ˜ธ on 2025. 1. 18..
//
#include <string>
#include <vector>
#include <iostream>
#include <climits>

using namespace std;

long long measure(const vector<int> &diffs, const vector<int> &times, int level, long long limit) {
    int size = diffs.size();
    long long total = 0;
    for (int i = 0; i < size; i++) {
        if (level >= diffs[i]) total += times[i];
        else total += (times[i - 1] + times[i]) * (diffs[i] - level) + times[i];
        if (total > limit || total < 0) return -1; // overflow
    }
    return total;
}

// ์ œํ•œ์‹œ๊ฐ„ ๋‚ด์— ํผ์ฆ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์ˆ™๋ จ๋„์˜ ์ตœ์†Ÿ๊ฐ’
// 1๋ถ€ํ„ฐ level์„ ์˜ฌ๋ ค๊ฐ€๋ฉด์„œ ํผ์ฆ์ด ์‹œ๊ฐ„๋‚ด์— ํ•ด๊ฒฐ๋˜๋Š” ์ˆ™๋ จ๋„๋ฅผ ์ฐพ์œผ๋ฉด ๊ทธ๊ฒƒ์ด ์ตœ์†Ÿ๊ฐ’์ด ๋จ
int solution(vector<int> diffs, vector<int> times, long long limit) {
    int level;
    for (level = 1; level <= 100000; level++) {
        long long time = measure(diffs, times, level, limit);
        if (time <= limit && time != -1) break;
    }
    return level;
}

๊ทธ๋ž˜์„œ ์ด์ง„ ํƒ์ƒ‰์— ๋Œ€ํ•œ ์˜๋ฌธ์„ ํ’ˆ์—ˆ์ง€๋งŒ.. ์ผ๋‹จ ๋ฌธ์ œ๋Š” ํ’€๋ ธ์Šต๋‹ˆ๋‹ค. ์™œ ๋‹ต์ด ์ฐพ์•„์ง€๋Š”์ง€๋Š” ์ข€ ๋” ์ƒ๊ฐํ•ด๋ด์•ผ๊ฒ ์–ด์š”

code
#include <string>
#include <vector>

using namespace std;

long long measure(const vector<int>& diffs, const vector<int>& times, int level, long long limit) {
    int size = diffs.size();
    long long total = 0;
    for (int i=0; i<size; i++) {
        if (level >= diffs[i]) total += times[i];
        else total += (times[i-1] + times[i]) * (diffs[i]-level) + times[i];
        if(total < 0) return -1; // overflow
    }
    return total;
}

int solution(vector<int> diffs, vector<int> times, long long limit) {
    int m;
    int s = 1, e = 100000;
    while(s<=e) {
        m = (s+e)/2;
        long long time = measure(diffs, times, m, limit);
        if(time > limit) s = m+1;
        else e = m-1;
    }
    return s;
}


int solution(vector<int> diffs, vector<int> times, long long limit) {
long long start = 0;
long long end = limit;
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด์ง„ํƒ์ƒ‰์„ ํ†ตํ•ด์„œ level์„ ์ฐพ์•„๊ฐ€๋‹ˆ end๋Š” level์˜ ์ตœ๋Œ“๊ฐ’์ธ 100000์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹น

Copy link
Collaborator

Choose a reason for hiding this comment

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

์ง€๊ธˆ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ์ด์ง„ํƒ์ƒ‰์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” if(time > limit)์—์„œ ํผ์ฆ์„ ์‹œ๊ฐ„๋‚ด์— ํ’€์ˆ˜ ์žˆ๋”๋ผ๋„ ์ตœ์†Œ level์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด e = m-1์„ ํ†ตํ•ด ๋” ์ž‘์€ level์„ ํ†ตํ•ด ํผ์ฆ์„ ํ’€ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ณ„์† ํ™•์ธํ•˜๋Š” ๋กœ์ง ๋•Œ๋ฌธ์ด๊ตฐ์š”!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์ด์ง„ํƒ์ƒ‰์„ ํ†ตํ•ด์„œ level์„ ์ฐพ์•„๊ฐ€๋‹ˆ end๋Š” level์˜ ์ตœ๋Œ“๊ฐ’์ธ 100000์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹น

์˜ค ๊ทธ๋ ‡๋„ค์š”! ์ˆœ๊ฐ„ limit ๋ณ€์ˆ˜์˜ ์˜๋ฏธ๋ฅผ ํ˜ผ์šฉํ•ด๋ฒ„๋ ธ์—ˆ๋‚˜๋ด์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~

์ง€๊ธˆ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ์ด์ง„ํƒ์ƒ‰์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” if(time > limit)์—์„œ ํผ์ฆ์„ ์‹œ๊ฐ„๋‚ด์— ํ’€์ˆ˜ ์žˆ๋”๋ผ๋„ ์ตœ์†Œ level์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด e = m-1์„ ํ†ตํ•ด ๋” ์ž‘์€ level์„ ํ†ตํ•ด ํผ์ฆ์„ ํ’€ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ณ„์† ํ™•์ธํ•˜๋Š” ๋กœ์ง ๋•Œ๋ฌธ์ด๊ตฐ์š”!

์ •ํ™•ํžˆ ์ดํ•ด๋Š” ์•ˆ๋˜์ง€๋งŒ ๊ทธ๋Ÿฐ ๋Š๋‚Œ์€ ๋งž์Šต๋‹ˆ๋‹ค. ์ƒํ•œ๊ณผ ํ•˜ํ•œ์„ ๊ณ„์† ์กฐ์ ˆํ•˜๋ฉฐ ์ตœ์†Œ level์„ ์ฐพ๋Š” ์›๋ฆฌ์ž…๋‹ˆ๋‹ค!

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

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

์ผ๋‹จ ๊ฒฐ๋ก ์€ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค..
์ƒ๊ฐ๋‚˜๋Š”๋Œ€๋กœ ์ผ๋‹จ ๋กœ์ง์— ๋งž๊ฒŒ diffs์ค‘ ์ตœ๊ณ  ๋‚œ์ด๋„๋ฅผ level๋กœ ์žก๊ณ  ์ ์ฐจ ํ•˜๋‚˜์”ฉ ๋‚ด๋ ค๊ฐ€๋ฉฐ ๋‚œ์ด๋„๊ฐ€ ๋†’์•„์งˆ์ˆ˜๋ก ์‹œ๊ฐ„์ด ๋Š˜์–ด๋‚˜ limit์„ ์ดˆ๊ณผํ•˜๊ฒŒ ๋ ๋•Œ, ์ง์ „์˜ level์„ ๋ฐ˜ํ™˜ํ•ด ๋‹ต์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

4๊ฐœ์งœ๋ฆฌ ์ •๋‹ต์€ ์ „๋ถ€ ํ†ต๊ณผํ–ˆ์ง€๋งŒ, ์ œ์ถœํ›„ ์ฒด์ ์—์„œ๋Š” ์ ˆ๋ฐ˜์˜ ๋‹ต๋งŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚˜๋จธ์ง€๋Š” ์ „๋ถ€ ์‹œ๊ฐ„์ดˆ๊ณผ์˜€์Šต๋‹ˆ๋‹ค.

๋ช‡๋ฒˆ์งธPR์ธ์ง€๋Š” ๊ธฐ์–ต์ด ์ž˜ ๋‚˜์ง€ ์•Š๋Š”๋ฐ, ์ œ๊ฐ€ ์˜ฌ๋ ธ๋˜ ๋ฌธ์ œ๊ฐ™์Šต๋‹ˆ๋‹ค. (์„œ์šธ ์˜ฌ๋ผ๊ฐ€๊ธฐ ์ง์ „์— ์˜ฌ๋ ธ๋˜ PR ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋•Œ๋„ ์ด๋Ÿฐ๋ฌธ์ œ์˜€๋Š”๋ฐ ์ด๋ถ„ํƒ์ƒ‰์„ ํ™œ์šฉํ•ด ํ’€์—ˆ๋˜ ๊ธฐ์–ต์ด ๋‚ฉ๋‹ˆ๋‹ค.

์ฒ˜์Œ์—๋Š” level์„ 1๊ณผ max(diffs)๋กœ ์žก์Šต๋‹ˆ๋‹ค.
์ดํ›„ ์ ˆ๋ฐ˜์˜ ๊ฐ’์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ์–ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์™ผ์ชฝ์˜ ์ ˆ๋ฐ˜, ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์˜ค๋ฅธ์ชฝ์˜ ์ ˆ๋ฐ˜์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๋ฉฐ level์„ ์ฐพ๋Š”๊ฒƒ์ž…๋‹ˆ๋‹ค.

mid = (left + right) // 2
๋งŒ์•ฝ limit์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค๋ฉด?
level์ด ๋” ๋‚ด๋ ค๊ฐ€๋„ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ right๋Š” mid-1์ด ๋ฉ๋‹ˆ๋‹ค.(์ค‘์•™๊ฐ’๋ณด๋‹ค ์ž‘์€์ˆ˜๋กœ ์ „์ฒด๋ฅผ ์ค„์ž„)

def solution(diffs, times, limit):
    high_level = max(diffs)
    level = high_level
    min_level = 0
    timetable = []
    current = 0
    
    for i in range(len(times)):
        if i == 0:
            timetable.append(times[i])
        else:
            timetable.append(times[i-1] + times[i])
        
    while level >= 0:
        total_time = 0
        for i in range(len(diffs)):
            if level >= diffs[i]:
                total_time += times[i]
            else:
                total_time += timetable[i] * (diffs[i] - level) + times[i]

        if total_time <= limit:
            min_level = level
        else:
            break
            
        level -= 1
                    
    answer = min_level
    return answer

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.

3 participants