LeetCode 915. 分割数组
维护数组的值,是一道思维题
class Solution {
public:
int partitionDisjoint(vector<int>& nums) {
if(nums.size() == 0){
return 0;
}
int ans = 1;
int max = nums[0];
int LeftMax = nums[0];
for(int i = 0;i<nums.size();i++){
max = max > nums[i] ? max : nums[i];
if(nums[i] < LeftMax){ // 代表右边存在小于左边最大值的数
LeftMax = max; // 更新当前左边的最大值
ans = i+1;
}
}
return ans;
}
};