[LeetCode] 875. Koko Eating Bananas, Medium
·
CodingTest/LeetCode
1. 문제정수 배열 piles가 주어질 때, 1시간에 임의의 값 k만큼 각 요소를 삭제할 수 있다고 하자.정수 h시간 전에 모든 요소를 삭제할 수 있는 최소 k를 구하라.2. 해결function getTotalHours(piles: number[], k: number) : number { let total = 0; for(let pile of piles) { total += Math.ceil(pile / k); } return total;}function minEatingSpeed(piles: number[], h: number): number { let left = 1, right = Math.max(...piles); while(left 이진 탐색 문제에서..
[LeetCode] 162. Find Peak Element, Medium
·
CodingTest/LeetCode
1. 문제정수 배열 nums가 주어질 때 자신의 왼쪽, 오른쪽 요소보다 큰 요소를 peak라 한다.이 peak는 여러 개 있을 수 있고, 그 중 아무 요소의 인덱스를 반환하라.2. 해결function findPeakElement(nums: number[]): number { let left = 0, right = nums.length - 1; while (left 0 ? nums[mid - 1] : -Infinity; const rightVal = mid leftVal && nums[mid] > rightVal) { return mid; } else if (nums[mid] 애초에 O(log n) 시간 복잡도로 탐색하라고 했으므로 이진 탐색을 사..
[LeetCode] 2300. Successful Pairs of Spells and Potions, Medium
·
CodingTest/LeetCode
1. 문제정수 배열 spells와 potions가 주어질 때, 아래 조건을 만족하는 배열을 반환하라.각 spell * 각 potion이 success를 초과하는 개수를 담는다.spell 별로 시행해서 배열에 담는다.2. 해결function successfulPairs(spells: number[], potions: number[], success: number): number[] { const ans = []; potions.sort((a, b) => a - b); for(let spell of spells) { let left = 0, right = potions.length - 1; let validIndex = potions.length; ..
[LeetCode] 374. Guess Number Higher or Lower, Easy
·
CodingTest/LeetCode
1. 문제정수 n이 주어질 때, 임의의 선택된 값을 찾아라.사전 정의된 guess() 함수는 인자로 전달된 값에 따라 아래 결과를 반환한다.선택된 값보다 큰 경우, -1선택된 값보다 작은 경우, 1선택된 값을 찾은 경우, 02. 해결/** * Forward declaration of guess API. * @param {number} num your guess * @return -1 if num is higher than the picked number * 1 if num is lower than the picked number * otherwise return 0 * var guess = function(num) {} */function guess..
[LeetCode] 2462. Total Cost to Hire K Workers, Medium
·
CodingTest/LeetCode
1. 문제각 사람 별 고용 비용을 담은 costs 배열이 주어지고, 사람을 고용하는 횟수 k와 선택할 수 있는 사람 수 candidates가 주어질 때, 최소 비용으로 사람을 고용하라.candidates는 costs 배열의 앞과 뒤에서 동시에 선택하여, 최소 비용인 사람을 선택해 고용한다.2. 해결class MinHeap { private heap: number[]; constructor() { this.heap = []; } getLeftChildIndex = (parentIndex) => parentIndex * 2 + 1; getRightChildIndex = (parentIndex) => parentIndex * 2 + 2; getParentIndex ..
[LeetCode] 2542. Maximum Subsequence Score, Medium
·
CodingTest/LeetCode
1. 문제정수 배열 nums1과 nums2가 주어질 때, k개의 인덱스를 선택해서 nums1의 합 * nums2의 최소값이 최대가 되는 값을 구하라.2. 해결class MinHeap { private heap: number[]; constructor() { this.heap = []; } getLeftChildIndex = (parentIndex) => parentIndex * 2 + 1; getRightChildIndex = (parentIndex) => parentIndex * 2 + 2; getParentIndex = (childIndex) => Math.floor((childIndex - 1) /2); swap = (i: number, j: num..
[LeetCode] 2336. Smallest Number in Infinite Set, Medium
·
CodingTest/LeetCode
1. 문제조건을 만족하는 SmallestInfiniteSet 클래스를 구현하라.생성 시 모든 양수를 담아 초기화 한다.popSmallest() : 가장 작은 수를 반환한다.addBack(int num): 입력 받은 정수가 없는 경우, 해당 값을 삽입한다.2. 해결class MinHeap { private heap: number[]; constructor() { this.heap = []; } getLeftChildIndex = (parentIndex) => parentIndex * 2 + 1; getRightChildIndex = (parentIndex) => parentIndex * 2 + 2; getParentIndex = (childIndex) => Ma..
[LeetCode] 215. Kth Largest Element in an Array, Medium
·
CodingTest/LeetCode
1. 문제정수 배열 nums와 정수 k가 주어질 때, 배열 내 k번째 큰 값을 구하라.2. 해결class MinHeap { private heap: number[]; constructor() { this.heap = []; } getLeftChildIndex = (parentIndex) => parentIndex * 2 + 1; getRightChildIndex = (parentIndex) => parentIndex * 2 + 2; getParentIndex = (childIndex) => Math.floor((childIndex - 1) /2); swap = (i: number, j: number) => { [this.heap[i], thi..
[LeetCode] 994. Rotting Oranges, Medium
·
CodingTest/LeetCode
1. 문제아래와 같은 정보로 2차원 배열이 주어질 때, 전체 오렌지가 상하는데 걸리는 최소 시간을 구하라.0: 빈 칸.1: 신선한 오렌지2: 썩은 오렌지썩은 오렌지 4방향의 위치한 신선한 오렌지는 1분 후 썩는다.2. 해결const dx = [-1, 1, 0, 0];const dy = [0, 0, -1, 1];function orangesRotting(grid: number[][]): number { const n = grid.length; const m = grid[0].length; let fresh = 0; const queue = []; const visited = Array.from({length: n}, () => Array(m).fill(false)); for..
[LeetCode] 1926. Nearest Exit from Entrance in Maze, Medium
·
CodingTest/LeetCode
1. 문제미로 정보가 .과 +로 주어질 때, .은 통로 + 벽으로 막혀있다. entrance 정보로 시작 위치가 주어질 때, 미로를 탈출할 수 있는 최소 이동 회수를 반환하라.entrance 좌표는 탈출구로 간주하지 않는다.2. 해결const dx = [-1, 1, 0, 0];const dy = [0, 0, -1, 1];function nearestExit(maze: string[][], entrance: number[]): number { const n = maze.length; const queue: number[][] = [[...entrance, 0]]; const visited = Array.from({ length: n }, () => Array(maze[0].length).f..