diff --git a/.vscode/input.code-snippets b/.vscode/input.code-snippets new file mode 100644 index 0000000..b89362c --- /dev/null +++ b/.vscode/input.code-snippets @@ -0,0 +1,15 @@ +{ + "ReadFileSnippet": { + "prefix": "rdipt", + "body": [ + "const fs = require(\"fs\");", + "const input = fs", + " .readFileSync(process.platform === \"linux\" ? \"/dev/stdin\" : \"${1:../input.txt}\")", + " .toString()", + " .trim()", + " .split(\"\\n\");" + ], + "description": "Read file input with fs", + "scope": "javascript,typescript" + } +} diff --git a/Jiho/Day10/Leetcode_853_Car Fleet.js b/Jiho/Day10/Leetcode_853_Car Fleet.js new file mode 100644 index 0000000..c4f371d --- /dev/null +++ b/Jiho/Day10/Leetcode_853_Car Fleet.js @@ -0,0 +1,30 @@ +var carFleet = function (target, position, speed) { + const len = position.length; + // position, speed 데이터 합치기 + let data = []; + for (let i = 0; i < len; i++) { + data.push([position[i], speed[i]]); + } + data.sort((a, b) => a[0] - b[0]); + console.log(data); + + let max_time = (target - data[len - 1][0]) / data[len - 1][1]; + + let answer = 0; + let pos = [data[len - 1][0]]; + for (let i = len - 1; i >= 0; i--) { + const time = (target - data[i][0]) / data[i][1]; + if (time <= max_time) { + continue; + } else if (time > max_time) { + answer += 1; + pos = []; + max_time = time; + } + pos.push(data[i][0]); + } + if (pos.length !== 0) answer += 1; + return answer; +}; + +console.log(carFleet(100, [0, 2, 4], [4, 2, 1])); diff --git a/Jiho/Day10/Leetcode_875_Koko Eating Bananas.js b/Jiho/Day10/Leetcode_875_Koko Eating Bananas.js new file mode 100644 index 0000000..ecce61c --- /dev/null +++ b/Jiho/Day10/Leetcode_875_Koko Eating Bananas.js @@ -0,0 +1,24 @@ +var minEatingSpeed = function (piles, h) { + let [left, right] = [1, Math.max(...piles)]; + + let min = -1; // speed + + while (left <= right) { + // #1. mid spped 값 계산 + const mid = Math.floor((left + right) / 2); + let numOfHours = 0; + for (const pile of piles) { + numOfHours += Math.ceil(pile / mid); + } + // #2. 검사 + if (h >= numOfHours) { + min = mid; + right = mid - 1; + } else { + left = mid + 1; + } + } + return min; +}; + +console.log(minEatingSpeed([3, 6, 7, 11], 8)); diff --git "a/Jiho/Day10/\353\260\261\354\244\200_1920_\354\210\230 \354\260\276\352\270\260.js" "b/Jiho/Day10/\353\260\261\354\244\200_1920_\354\210\230 \354\260\276\352\270\260.js" new file mode 100644 index 0000000..6f35d9f --- /dev/null +++ "b/Jiho/Day10/\353\260\261\354\244\200_1920_\354\210\230 \354\260\276\352\270\260.js" @@ -0,0 +1,42 @@ +const fs = require("fs"); +const input = fs + .readFileSync(process.platform === "linux" ? "/dev/stdin" : "../input.txt") + .toString() + .trim() + .split("\n"); + +const judgeExistence = (N, nums, M, targets) => { + // #1. 오름차순 정렬 + nums.sort((a, b) => a - b); + for (const target of targets) { + // #2. Index 초기화 + let [left, right] = [0, N - 1]; + let exists = false; + while (left <= right) { + const mid = Math.floor((left + right) / 2); + // #3. 경우의 수 처리 + if (nums[mid] < target) { + left = mid + 1; + } else if (nums[mid] > target) { + right = mid - 1; + } else { + exists = true; + break; + } + } + if (exists) { + console.log(1); + } else { + console.log(0); + } + } +}; + +const [N, nums, M, targets] = input; + +judgeExistence( + parseInt(N), + nums.split(" ").map(Number), + parseInt(M), + targets.split(" ").map(Number) +);