Skip to content

Commit

Permalink
#19 23.08.11 > 5 > 용액
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed Aug 11, 2023
1 parent 237f629 commit b77e411
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/bj/gold/5/2467.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict";

/**
* [이분탐색 문제]
* - 시간 복잡도: O(log N)
*/

const [N, ...capacity] = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split(/\s/)
.map((v) => +v);

const solution = (N, capacity) => {
let [left, right] = [0, N - 1];
let diff = Math.abs(capacity[left] + capacity[right]);
let [sol1, sol2] = [capacity[left], capacity[right]];

while (left < right) {
let sumValue = capacity[left] + capacity[right];
if (Math.abs(sumValue) < diff) {
diff = Math.abs(sumValue);
sol1 = capacity[left];
sol2 = capacity[right];
}

if (sumValue === 0) break; // 0이 되었다는 것은 0에 가장 근접한 것이므로 바로 나오면 된다.
else if (sumValue > 0) right--;
else left++;
}
return `${sol1} ${sol2}`;
};

console.log(solution(N, capacity));
1 change: 1 addition & 0 deletions src/bj/gold/5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
| 23 | 2493 | [](./2493.js) | 23.06.28 | [23.07.11](./replay/2493.js) |
| 24 | 6198 | [옥상 정원 꾸미기](./6198.js) | 23.06.29 | [23.07.11](./replay/6198.js) |
| 25 | 1240 | [노드 사이의 거리](./1240.js) | 23.08.10 | X |
| 26 | 2467 | [용액](./2467.js) | 23.08.11 ||

0 comments on commit b77e411

Please sign in to comment.