My O(log(n)) Java solution (similar with the solution of the problem that the array items are unique)


  • -2
    U

    A quite similar solution with the previous problem. only add more 1 or 3 lines to overcome the same elements.

    public class Solution {
    public int findMin(int[] num) {
    int low = 0;
    int high = num.length - 1;
    int min = Integer.MAX_VALUE;

        while (low <= high) {
            int mid = low + (high - low)/2;
            min = Math.min(min, num[mid]);
            if (mid + 1 < num.length) {
                min = Math.min(min, num[mid + 1]); // only this line differ with the other problem
            }
            
            if (num[high] < num[mid]) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return min;
    }
    

    }


  • 0
    K

    input:{3,3,3,3,3,3,3,3,1,3}, output:3 error.


  • 0
    S

    Thanks for your post. However it would be better to share solution with correct code format and elaborated thoughts. Please read the Discuss FAQ for more info. Take a look at good sharing example

    Pay attention to "Writing code? Select all code then click on the {} button to preserve code formatting.” above text editor.


  • 0
    S

    @kmind, thanks for this catch! @ulyx, we have updated our test case. This solution should get wrong answer now.


  • 0
    U

    yes... maybe it is accepted only because the test case are not enough...


Log in to reply
 

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