Puzzled about the Recommended Solution (Find Minimum in Rotated Sorted Array II)


  • 2
    G
    public int findMin(int[] A) {
       int L = 0, R = A.length - 1;
       while (L < R && A[L] >= A[R]) {
          int M = (L + R) / 2;
          if (A[M] > A[R]) {
             L = M + 1;
          } else if (A[M] < A[L]) {
             R = M;
          } else {   // A[L] == A[M] == A[R]
             L = L + 1;
          }
       }
       return A[L];
    }
    

    this is the recommended solution the system give.For this case [2,1,4,3]( rotate from [4,3,2,1] ), its result is 2,but in fact, 1 is the correct answer. Here, what is the meaning of "rotate" ,[2,1,4,3] is not a valid input?


  • 0
    M

    2, 1, 4, 3 is not a valid input array.
    The input has to be a sorted array (that is then rotated)


  • 0
    J

    @MessyHack This solution is only for numbers in ascending order. But the puzzle doesn't give any notice about it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.