Binary Search

Find Peak Element

January 14, 2024
medium
Binary Search

Problem # Given an array nums of integers which is initially increasing and then decreasing, find a peak element. A peak element is an element which is greater than its neighbors. Assume that nums[-1] and nums[n] (elements outside the array) are negative infinity. Example: Input: nums = [1, 3, 20, 4, 1, 0] Output: 20 Solution Approach # The solution involves using a modified binary search. Since the array is initially increasing and then decreasing, a peak element can be found where the trend changes from increasing to decreasing. ...

Maximum Profit in Job Scheduling

January 8, 2024
medium
dynamic programming, Binary Search

Problem # You are given n jobs where each job consists of a start time, end time, and a profit. You can choose to perform these jobs such that no two jobs overlap. Find the maximum profit you can achieve by scheduling these jobs. Example: # Input: jobs = [(1, 3, 50), (2, 5, 20), (4, 6, 70), (6, 7, 30)] Output: 120 Explanation: The optimal job schedule is to do the job from 1 to 3 and then the job from 4 to 6. ...

Find the Minimum in a Rotated Sorted Array

January 4, 2024
medium
Binary Search

Problem # Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). Find the minimum element. The array may contain duplicates. Example: Input: [3,4,5,1,2] Output: 1 Solution # Algorithm: Modified Binary Search Solution Approach:** # - The idea is to use binary search. However, due to the rotation, a standard binary search won't work directly. - The strategy is to find the inflection point where the order of the array changes. ...