diff --git a/Solution/875. Koko Eating Bananas/875. Koko Eating Bananas.py b/Solution/875. Koko Eating Bananas/875. Koko Eating Bananas.py new file mode 100644 index 0000000..ffa7923 --- /dev/null +++ b/Solution/875. Koko Eating Bananas/875. Koko Eating Bananas.py @@ -0,0 +1,6 @@ +class Solution: + def minEatingSpeed(self, piles: List[int], h: int) -> int: + def check(k: int) -> bool: + return sum((x + k - 1) // k for x in piles) <= h + + return 1 + bisect_left(range(1, max(piles) + 1), True, key=check) \ No newline at end of file diff --git a/Solution/875. Koko Eating Bananas/readme.md b/Solution/875. Koko Eating Bananas/readme.md new file mode 100644 index 0000000..013dbeb --- /dev/null +++ b/Solution/875. Koko Eating Bananas/readme.md @@ -0,0 +1,105 @@ + + + +# [33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array) + + +--- +- **comments**: true +- **difficulty**: Medium +- **tags**: + - Array + - Binary Search +--- + +## Description + + + +

There is an integer array nums sorted in ascending order (with distinct values), which is rotated at an unknown pivot index k (0 <= 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 rotated at pivot index 3 and become [4,5,6,7,0,1,2].

+ +

Given the array nums after the 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.

+ + + +## Solutions + + + +### Solution 1: Binary Search + +Since the array is rotated, we can't directly apply the standard binary search. But we can still use binary search with some modifications by identifying which half of the array is properly sorted. + +At each step: + +- Check if the left half is sorted. +- If it is, check whether the target is within the left half. +- If not, search the right half. +- Repeat similarly if the right half is sorted. + +This approach ensures that with each iteration, we eliminate half the array, maintaining the logarithmic complexity. + +Time Complexity: $O(\log n)$ +Space Complexity: $O(1)$ + + + +#### Python3 + +```python +class Solution: + def search(self, nums: List[int], target: int) -> int: + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] == target: + return mid + if nums[left] <= nums[mid]: + if nums[left] <= target < nums[mid]: + right = mid - 1 + else: + left = mid + 1 + else: + if nums[mid] < target <= nums[right]: + left = mid + 1 + else: + right = mid - 1 + return -1 +``` + +#### Java + +```java +class Solution { + public int search(int[] nums, int target) { + int l = 0, r = nums.length - 1; + while (l <= r) { + int mid = (l + r) / 2; + if (nums[mid] == target) return mid; + if (nums[l] <= nums[mid]) { + if (nums[l] <= target && target < nums[mid]) { + r = mid - 1; + } else { + l = mid + 1; + } + } else { + if (nums[mid] < target && target <= nums[r]) { + l = mid + 1; + } else { + r = mid - 1; + } + } + } + return -1; + } +} +``` + + + + + + +```