leetcode

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

1574.cpp (564B)


      1 class Solution {
      2   public:
      3     int findLengthOfShortestSubarray(const vector<int> &arr) const {
      4         const int n = size(arr);
      5         int i = 0, j = n - 1;
      6 
      7         while (i + 1 < n && arr[i] <= arr[i + 1])
      8             i++;
      9         if (i == n - 1) return 0;
     10 
     11         while (j > i && arr[j - 1] <= arr[j])
     12             j--;
     13 
     14         int res = min(n - i - 1, j);
     15         for (int k = 0; k <= i && j < n;) {
     16             if (arr[j] >= arr[k])
     17                 res = min(res, j - k++ - 1);
     18             else
     19                 j++;
     20         }
     21 
     22         return res;
     23     }
     24 };