11286 절대값 힙

송민지·2023년 5월 9일
0

알고리즘

목록 보기
14/22

const input = require("fs")
  .readFileSync("11286.txt")
  .toString()
  .trim()
  .split("\n");

const arr = [];
const answer = [];

for (let i = 1; i < input.length; i++) {
  arr.push(input[i]);
  if (Math.abs(input[i]) === 0) arr.pop(Math.min(input[i]));
  answer.push(arr.pop(Math.min(input[i])));
}
console.log(arr);
console.log(answer);

[]
[
  '1',       '-1',      undefined,
  undefined, undefined, '1',
  '1',       '-1',      '-1',
  '2',       '-2',      undefined,
  undefined, undefined, undefined,
  undefined, undefined, undefined
]

arr[i]번째가 0 일때 arr의 절대값이 큰 수대로 answer에 넣는건 성공했지만, 절대값이 없을때 0을 넣는것은 실패했다.

조금만 더 하면 될거같은데 잘 안된다 흑..

profile
기록하는 일상

0개의 댓글