All files / leetCode 0153.ts

100% Statements 14/14
100% Branches 8/8
100% Functions 1/1
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  5x 1x 4x 4x   3x 3x   3x 5x 1x 4x 4x 2x 2x     2x    
export default function findMin(nums: number[]): number {
  if (nums.length < 2)
    return nums[0]
  if (nums.length === 2)
    return Math.min(nums[0], nums[1])
 
  let lo = 0
  let hi = nums.length - 1
 
  while (lo + 1 < hi) {
    if (nums[lo] < nums[hi])
      return nums[lo]
    const mid = lo + ((hi - lo) >> 1)
    if (nums[lo] < nums[mid])
      lo = mid
    else hi = mid
  }
 
  return Math.min(nums[lo], nums[hi])
}