From 0339aa7b1f2104d8316d27d9e9f6e61ac8d4e3dd Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Wed, 19 Aug 2026 22:11:01 -0400 Subject: [PATCH] feat(problems): add solutions for Search in Rotated Sorted Array and Koko Eating Bananas --- .../33.search-in-rotated-sorted-array.js | 87 +++++++++++++++++++ work/Medium/Array/875.koko-eating-bananas.js | 80 +++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 work/Medium/Array/33.search-in-rotated-sorted-array.js create mode 100644 work/Medium/Array/875.koko-eating-bananas.js diff --git a/work/Medium/Array/33.search-in-rotated-sorted-array.js b/work/Medium/Array/33.search-in-rotated-sorted-array.js new file mode 100644 index 0000000..f6b2a98 --- /dev/null +++ b/work/Medium/Array/33.search-in-rotated-sorted-array.js @@ -0,0 +1,87 @@ +/* + * 33. Search in Rotated Sorted Array + * Difficulty: Medium + * https://leetcode.com/problems/search-in-rotated-sorted-array/ + * + * ────────────────────────────────────────────────── + * + * There is an integer array nums sorted in ascending order (with + * distinct values). + * + * Prior to being passed to your function, nums is possibly left rotated + * at an unknown index k (1 <= k < nums.length) such that the resulting + * array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., + * nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be left + * rotated by 3 indices and become [4,5,6,7,0,1,2]. + * + * Given the array nums after the possible rotation and an integer + * target, return the index of target if it is in nums, or -1 if it is + * not in nums. + * + * You must write an algorithm with O(log n) runtime complexity. + * + * + * + * Example 1: + * + * Input: nums = [4,5,6,7,0,1,2], target = 0 + * Output: 4 + * + * Example 2: + * + * Input: nums = [4,5,6,7,0,1,2], target = 3 + * Output: -1 + * + * Example 3: + * + * Input: nums = [1], target = 0 + * Output: -1 + * + * + * + * Constraints: + * + * • 1 <= nums.length <= 5000 + * + * • -10^4 <= nums[i] <= 10^4 + * + * • All values of nums are unique. + * + * • nums is an ascending array that is possibly rotated. + * + * • -10^4 <= target <= 10^4 +*/ + +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var search = function(nums, target) { + let l = 0, r = nums.length - 1; + + while (l <= r){ + let mid = Math.floor((l + r)/2); + if (target === nums[mid]) { + return mid + } + + // Left sorted portion + if (nums[l] <= nums[mid]){ + if (target > nums[mid] || target < nums[l]){ + l = mid + 1; + }else{ + r = mid - 1; + } + } + // Right sorted portion + else{ + if(target < nums[mid] || target > nums[r]){ + r = mid - 1; + }else{ + l = mid + 1; + } + } + } + return -1; +}; diff --git a/work/Medium/Array/875.koko-eating-bananas.js b/work/Medium/Array/875.koko-eating-bananas.js new file mode 100644 index 0000000..ac1d662 --- /dev/null +++ b/work/Medium/Array/875.koko-eating-bananas.js @@ -0,0 +1,80 @@ +/* + * 875. Koko Eating Bananas + * Difficulty: Medium + * https://leetcode.com/problems/koko-eating-bananas/ + * + * ────────────────────────────────────────────────── + * + * Koko loves to eat bananas. There are n piles of bananas, the i^th + * pile has piles[i] bananas. The guards have gone and will come back in + * h hours. + * + * Koko can decide her bananas-per-hour eating speed of k. Each hour, + * she chooses some pile of bananas and eats k bananas from that pile. If + * the pile has less than k bananas, she eats all of them instead and + * will not eat any more bananas during this hour. + * + * Koko likes to eat slowly but still wants to finish eating all the + * bananas before the guards return. + * + * Return the minimum integer k such that she can eat all the bananas + * within h hours. + * + * + * + * Example 1: + * + * Input: piles = [3,6,7,11], h = 8 + * Output: 4 + * + * Example 2: + * + * Input: piles = [30,11,23,4,20], h = 5 + * Output: 30 + * + * Example 3: + * + * Input: piles = [30,11,23,4,20], h = 6 + * Output: 23 + * + * + * + * Constraints: + * + * • 1 <= piles.length <= 10^4 + * + * • piles.length <= h <= 10^9 + * + * • 1 <= piles[i] <= 10^9 +*/ + +/** + * @param {number[]} piles + * @param {number} h + * @return {number} + */ +var minEatingSpeed = function(piles, h) { + // Function to determine if K works for the H + function kWorks(k){ + let hours = 0; + for (let p of piles){ + hours += Math.ceil(p/k) + } + return hours <= h; + } + + // Setup Binary Search + let l = 1; + let r = Math.max(...piles); + + // Find the K value which works for H and consumes all bananas + while (l < r){ + const mid = Math.floor((l + r) / 2); + if (kWorks(mid)){ + r = mid; + }else{ + l = mid + 1; + } + } + return l; +};