Confusion with a 3 lines C++ solution


  • 0

    I think it's quite hard to achieve an O(logn) solution as duplicates are allowed. So I just try a very naive solution which is O(n) and it is accepted with 8ms. Perhaps the OJ does not include some corner cases which will make this solution fail. But I am quite confused why this "worst" solution can run that fast according to the submission... :/

    class Solution {
    public:
        int findMin(vector<int>& nums) {
            for (int i = 0; i < nums.size() - 1; ++i) 
                if (nums[i] > nums[i+1]) return nums[i+1];
            return nums[0];
        }
    };
    

Log in to reply
 

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