728x90
반응형
목차
문제
https://school.programmers.co.kr/learn/courses/30/lessons/340212
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
문제 구현 시 주의점
이분탐색으로 하면 쉽게 풀리나 숫자의 범위가 매우 크기 때문에 js로 풀 경우 테스트를 통과하지 못하니 주의하자
bigint를 써서 풀어야 한다.
코드 구현
let answer = 0n;
function cacul(diffs, times, level, limit) {
let process = 0n;
for (let idx = 0; idx < diffs.length; idx++) {
const diff = BigInt(diffs[idx]);
const time = BigInt(times[idx]);
const levelBig = BigInt(level);
if (levelBig < diff) {
if (idx === 0) {
process += (diff - levelBig + 1n) * time;
} else {
const prevTime = BigInt(times[idx - 1]);
process += (diff - levelBig) * (prevTime + time) + time;
}
} else {
process += time;
}
if(limit < process)return Math.Infinity;
}
return process;
}
function binarySearch(diffs, times, limit) {
let start = 1n;
let end = 100001n;
let final = 100001n;
const limitBig = BigInt(limit);
while (start <= end) {
let mid = (start + end) / 2n;
let ans = cacul(diffs, times, mid, limitBig);
if (ans <= limitBig) {
final = mid < final ? mid : final;
end = mid - 1n;
} else {
start = mid + 1n;
}
}
answer = final;
}
function solution(diffs, times, limit) {
binarySearch(diffs, times, limit);
return Number(answer);
}반응형
'PS > 프로그래머스' 카테고리의 다른 글
| [프로그래머스][그리디] n+1 카드게임 (4) | 2025.07.10 |
|---|---|
| [프로그래머스][백트래킹][이분탐색][조합] 주사위 고르기 (1) | 2025.07.04 |
| [프로그래머스][map] 충돌 위험 찾기 (0) | 2025.07.04 |
| [프로그래머스][bfs] 석유시추 (0) | 2025.07.02 |
| [프로그래머스][조합] 비밀코드해독 (0) | 2025.06.28 |