-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
16-kangrae-jo #59
Conversation
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.
์ฒ์์ ์ผ๋ฐ์ ์ธ ํ์์ ์ฌ์ฉํ์ ๋ ํ ์คํธ ์ผ์ด์ค๊ฐ 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> ×, 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; |
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.
์ด์งํ์์ ํตํด์ level์ ์ฐพ์๊ฐ๋ end๋ level์ ์ต๋๊ฐ์ธ 100000์ด๋ผ๊ณ ์๊ฐํฉ๋๋น
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.
์ง๊ธ ์๊ฐํด๋ณด๋ ์ด์งํ์์ด ๊ฐ๋ฅํ ์ด์ ๋ if(time > limit)
์์ ํผ์ฆ์ ์๊ฐ๋ด์ ํ์ ์๋๋ผ๋ ์ต์ level์ ๊ตฌํ๊ธฐ ์ํด e = m-1
์ ํตํด ๋ ์์ level์ ํตํด ํผ์ฆ์ ํ ์ ์๋์ง ๊ณ์ ํ์ธํ๋ ๋ก์ง ๋๋ฌธ์ด๊ตฐ์!
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.
์ด์งํ์์ ํตํด์ level์ ์ฐพ์๊ฐ๋ end๋ level์ ์ต๋๊ฐ์ธ 100000์ด๋ผ๊ณ ์๊ฐํฉ๋๋น
์ค ๊ทธ๋ ๋ค์! ์๊ฐ limit
๋ณ์์ ์๋ฏธ๋ฅผ ํผ์ฉํด๋ฒ๋ ธ์๋๋ด์ ๊ฐ์ฌํฉ๋๋ค~
์ง๊ธ ์๊ฐํด๋ณด๋ ์ด์งํ์์ด ๊ฐ๋ฅํ ์ด์ ๋
if(time > limit)
์์ ํผ์ฆ์ ์๊ฐ๋ด์ ํ์ ์๋๋ผ๋ ์ต์ level์ ๊ตฌํ๊ธฐ ์ํดe = m-1
์ ํตํด ๋ ์์ level์ ํตํด ํผ์ฆ์ ํ ์ ์๋์ง ๊ณ์ ํ์ธํ๋ ๋ก์ง ๋๋ฌธ์ด๊ตฐ์!
์ ํํ ์ดํด๋ ์๋์ง๋ง ๊ทธ๋ฐ ๋๋์ ๋ง์ต๋๋ค. ์ํ๊ณผ ํํ์ ๊ณ์ ์กฐ์ ํ๋ฉฐ ์ต์ level์ ์ฐพ๋ ์๋ฆฌ์ ๋๋ค!
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.
์ผ๋จ ๊ฒฐ๋ก ์ ํ๋ ธ์ต๋๋ค..
์๊ฐ๋๋๋๋ก ์ผ๋จ ๋ก์ง์ ๋ง๊ฒ 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
๐ ๋ฌธ์ ๋งํฌ
[ํผ์ฆ ๊ฒ์ ์ฑ๋ฆฐ์ง]
โ๏ธ ์์๋ ์๊ฐ
48m
โจ ์๋ ์ฝ๋
PCCP ๊ธฐ์ถ๋ฌธ์ 2๋ฒ์ ๋๋ค.
๋ฌธ์ ๋ฅผ ์ฝ์ด๋ณด์๋ฉด
๊ตฌํ
+ํ์ ๋ฐฉ๋ฒ ์ค ํ๋
๋ผ๋ ์๊ฐ์ด ๋ค ๊ฒ ์ ๋๋ค.์ ๋ ์ ์ ํ
level
์ด ๋์ฌ ๋ ๊น์งํผ์ฆ์ ํธ๋
๋ก์ง์ ๋ฐ๋ณตํ๋ ๊ทธ๋ฐ ์๋ฃจ์ ์ด ๋ ์ฌ๋์ต๋๋ค.์ฌ๊ธฐ์
ํผ์ฆ์ ํธ๋
์๊ตฌํ
์ด๋ผ๊ณ ์๊ฐ์ด ๋๊ณ ,์ ์ ํ
level
์ ์ฐพ๋๊ฑดํ์
์ด๋ผ๊ณ ์๊ฐ ๋๋ค์.๊ตฌํ์.... ๊ฐ์์ ๋ชซ์ ๋๋ค...
๋ฌธ์ ๋ฅผ ์ฝ์ด๋ณด๋ฉด ์ด๋ฐ ๋ก์ง์ ๋ ์ฌ๋ฆด ์ ์์ด์ผ ํ ๊ฒ ๊ฐ๋ค์!
(์๋ง ๋ ๊ฐ๊ฒฐํ๊ฑฐ๋ ํ๋์ ๋ณด๊ธฐ ์ฌ์ด ๊ตฌํ๋ฐฉ๋ฒ์ด ์๊ฒ ์ฃ ?)
์ด์ ์ง์ง ๋ฌธ์ ๋
ํ์
์ ๋๋ค. ์ ์ ํlevel
์ฐพ๊ธฐ๋ฐฉ๋ฒ์ ์ฌ๋ฌ๊ฐ์ง๊ฐ ์์ ์ ์์ด์.์ ๋ ์ฒ์์๋ 1๋ฒ ๋ฐฉ์์ผ๋ก ๋น ๋ฅด๊ฒ ๊ตฌํํด๋ดค์ต๋๋ค. (์ฒ์์๋ ์ด๋ถํ์์ด ๋ ์ค๋ฅด์ง ์์์ต๋๋คใ )
์๋๋ ๋ค๋ฅผ๊น ์๊ฐ์ด๊ณผ, ๋ช ๋ช ์ผ์ด์ค ์์๋ ์ค๋ต๊น์ง ๋๋๋ผ๊ตฌ์.
ํ์์ ๋ฌธ์ ๊ฐ ์๋ค๋ ๊ฒ์ ์๊ฒ๋์๊ณ , ์ด๋ถํ์์ผ๋ก ์งํํด๋ณด์์์ต๋๋ค.
๊ทธ๋ฐ๋ฐ... ์ด๋ถํ์์ ๊ตฌํํ์ง ๋ชปํ๊ฒ ๋๋ผ๊ณ ์.....
์ ํํ ๋งํ๋ฉด ์ด๋ถํ์ ์์ฒด๋ ๊ตฌํํ๋๋ฐ ์ฌ์ ํ ๋ช๋ช ์ผ์ด์ค์์๋ ์ค๋ต์ด์์ต๋๋ค.
๊ทธ๋์ ๋ค๋ฅธ์ฌ๋์ ํ์ด ํ ๋ฒ ๋ดค์ต๋๋ค.
์ญ์ ์ด๋ถํ์ ์ด์๊ณ , ๋ฑํธ ๊ฐ์ ๊ฒ์ ๋ฌธ์ ๊ฐ ์์์ต๋๋ค!
๋์๋ฅผ ๋น๊ตํ๋ ์ผ๋ถ ์ฝ๋์ ๋ฑํธ๊ฐ ์๊ณ ์๊ณ ์ ์ฐจ์ด๊ฐ ๋ฌ์๋๋ผ๊ณ ์.
๊ทธ๊ฒ์ ๊ณ ์น๋ ์ ๋ต์ด์์ต๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
ํ์ํ ๋ฒ์๊ฐ ๋์ ๊ฒฝ์ฐ์๋
BinarySearh
๋ฅผ ํ์ฉํ์!BinarySearh
๋ฅผ ํ์ฉํ ๋๋break
ํ๋ ์กฐ๊ฑด,start = mid +1
ํ๋ ์กฐ๊ฑด,end = mid -1
ํ๋ ์กฐ๊ฑด์ ์ ์ ํ์!