Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created October 25, 2018 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/906064f24f55e7ad159cd45b4fc64049 to your computer and use it in GitHub Desktop.
Save fpdjsns/906064f24f55e7ad159cd45b4fc64049 to your computer and use it in GitHub Desktop.
[leetcode] 915. Partition Array into Disjoint Intervals : https://leetcode.com/problems/partition-array-into-disjoint-intervals/
class Solution {
public:
int partitionDisjoint(vector<int>& A) {
int size = A.size();
vector<int> minArr(size);
minArr[size-1] = A[size-1];
for(int i=size-2;i>=0;i--){
minArr[i] = min(A[i], minArr[i+1]);
}
int maxNum=A[0];
int ans = 0;
while(ans+1 < size && maxNum > minArr[ans+1]){
ans++;
maxNum = max(maxNum, A[ans]);
}
return ans + 1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment