leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0581.cpp (480B)


      1 class Solution {
      2   public:
      3     int findUnsortedSubarray(const vector<int> &nums) const {
      4         const int n = size(nums);
      5         int mini = nums[n - 1], maxi = nums[0];
      6         int low = -1, high = -2;
      7 
      8         for (int i = 1; i < n; i++) {
      9             mini = min(mini, nums[n - i - 1]);
     10             maxi = max(maxi, nums[i]);
     11 
     12             if (nums[i] < maxi) high = i;
     13             if (nums[n - i - 1] > mini) low = n - i - 1;
     14         }
     15 
     16         return high - low + 1;
     17     }
     18 };