Skip to content

Commit

Permalink
#7 22.11.08 > 결정 알고리즘2 복습
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed Nov 8, 2022
1 parent 7ca3ce7 commit 07d84a7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/inf/sort,greedy/12_rerere.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";

function count(arr, dist) {
let count = 1;
let latestPosition = arr[0]; // 가장 최근에 들어간 말의 위치
for (let x of arr) {
if (x - latestPosition >= dist) {
count++;
dist = x;
}
}
return count;
}

function solution(c, arr) {
let answer = 0;
// 1. 마구간 정렬하기
arr.sort((a, b) => a - b);

let minDistance = arr[1] - arr[0];
let maxDistance = arr[arr.length - 1];

while (minDistance <= maxDistance) {
let distance = Math.floor((minDistance + maxDistance) / 2);
if (count(arr, distance) >= c) {
answer = distance;
minDistance = distance + 1;
} else {
maxDistance = distance - 1;
}
}
return answer;
}

let arr = [1, 2, 8, 4, 9];
console.log(solution(3, arr));
2 changes: 1 addition & 1 deletion src/inf/sort,greedy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
| 9 || 22.11.08😥 |
| 10 || 22.11.07 |
| 11 | X | 22.11.08😥 |
| 12 | X | 22.11.07😥 |
| 12 | X | 22.11.08😥 |

0 comments on commit 07d84a7

Please sign in to comment.