Skip to content

Commit

Permalink
5.9: greedy, sort(센서)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed May 9, 2024
1 parent d9dc89b commit ff53f4c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions baekjoon/2212.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 센서 : 그리디, 정렬
const input = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n");
const n = Number(input[0]);
const k = Number(input[1]);
const coord = input[2]
.split(" ")
.map((v) => Number(v))
.sort((a, b) => a - b);
const diff = [];
let answer = 0;

for (let i = 0; i < n - 1; i++) {
const [f, s] = [coord[i], coord[i + 1]];
diff.push([Math.abs(s - f), [i, i + 1]]);
}

const sorted = diff.sort((a, b) => b[0] - a[0]);
const splitIndex = sorted
.slice(0, k - 1)
.map((v) => v[1][0])
.sort((a, b) => a - b);

let start = 0;
let idx = 0;
let count = 0;
for (let i = 0; i < n; i++) {
if (count < k - 1 && i === splitIndex[idx]) {
answer += Math.abs(coord[splitIndex[idx]] - coord[start]);
idx++;
count++;
start = i + 1;
}
}
answer += Math.abs(coord[n - 1] - coord[start]);

console.log(answer);

0 comments on commit ff53f4c

Please sign in to comment.