leetcode

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

0915.cpp (504B)


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