2470. 두 용액 - node.js / javascript

문제

내 코드

let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");

const N = Number(input.shift());
const solution = input
  .shift()
  .split(" ")
  .map(Number)
  .sort((a, b) => a - b);

let left = 0;
let right = N - 1;
let tempSum = Number.MAX_SAFE_INTEGER;
let answer = "";

while (left < right) {
  let sum = solution[left] + solution[right];

  if (tempSum > Math.abs(sum)) {
    tempSum = Math.abs(sum);
    answer = [solution[left], solution[right]];
  }

  if (sum < 0) {
    left++;
  } else {
    right--;
  }
}

console.log(answer.sort((a, b) => a - b).join(" "));

깃허브 링크

https://github.com/highjoon/JS_Algorithm/blob/master/BOJ/%ED%88%AC%ED%8F%AC%EC%9D%B8%ED%84%B0/2470.js

좋은 웹페이지 즐겨찾기