/154. Find Minimum in Rotated Sorted Array II

154. Find Minimum in Rotated Sorted Array II

Hard
Arrays44.7% acceptance

Given an integer array rotated_sorted_array of length n, which is sorted in ascending order and then rotated between 1 and n times, and may contain duplicate values, return the minimum element in the array. The array is rotated such that the order is preserved except for the rotation. The goal is to minimize the number of operations.

Example 1

Input: [6,6,7,8,1,2,3,4,5]

Output: 1

Explanation: The minimum element is 1 after rotation.

Example 2

Input: [2,2,2,2,2,2,2,1,2]

Output: 1

Explanation: The minimum element is 1 after rotation.

Example 3

Input: [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,9]

Output: 9

Explanation: The minimum element is 9 after rotation.

Constraints

  • 1 <= len(rotated_sorted_array) <= 5000
  • -5000 <= rotated_sorted_array[i] <= 5000
  • rotated_sorted_array is sorted in ascending order and rotated between 1 and len(rotated_sorted_array) times
Python (current runtime)

Case 1

Input: [4,5,6,7,0,1,2,2]

Expected: 0

Case 2

Input: [3,3,3,3,3,3,3,3,3,1,2,3]

Expected: 1

Case 3

Input: [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]

Expected: 1

Case 4

Input: [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,4]

Expected: 4

Case 5

Input: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Expected: 0