<Just For Fun> C++ 4ms one-liner;


  • 0
    F
    class Solution {
        int doFindMin(vector<int>& nums, int l, int r)
        {
            return !((l+r>>1)+1 <r && nums[l+r>>1] >= nums[(l+r>>1) + 1]) && !((l+r>>1)-1 >= l && nums[l+r>>1] >= nums[(l+r>>1)-1]) ? nums[l+r>>1] : nums[l+r>>1] > nums[r-1] ? doFindMin(nums, (l+r>>1)+1, r) : doFindMin(nums, l, (l+r>>1));
        }
    public:
        int findMin(vector<int>& nums) {
            return doFindMin(nums, 0, nums.size());
        }
    };
    

    I know this looks horrible, but, one-liner, just for fun.
    Here is the readable one:

    class Solution {
        int doFindMin(vector<int>& nums, int l, int r)
        {
            int m = (l + r) / 2;
            if (!(m+1 < r && nums[m] >= nums[m + 1]) && !(m-1 >= l && nums[m] >= nums[m - 1]))
                return nums[m];
            if (nums[m] > nums[r-1])
                return doFindMin(nums, m+1, r);
            return doFindMin(nums, l, m);
        }
    public:
        int findMin(vector<int>& nums) {
            return doFindMin(nums, 0, nums.size());
        }
    };

Log in to reply
 

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