class Solution {
public:
typedef long long ll;
long long minimumDifference(vector<int>& nums) {
int n=nums.size();
ll s1[n],s2[n];
memset(s1,0,sizeof s1),memset(s2,0,sizeof s2);
priority_queue<ll>q1;
priority_queue<ll,vector<ll>,greater<ll>>q2;
ll sum=0;
for(int i=0;i<n/3;i++){
q1.push(nums[i]);
sum+=nums[i];
s1[i]=sum;
}
for(int i=n/3;i<n/3*2;i++){
q1.push(nums[i]);
sum-=q1.top();
q1.pop();
sum+=nums[i];
s1[i]=sum;
}
sum=0;
for(int i=n-1;i>=n/3*2;i--){
q2.push(nums[i]);
sum+=nums[i];
s2[i]=sum;
}
for(int i=n/3*2-1;i>=n/3;i--){
q2.push(nums[i]);
sum-=q2.top();
q2.pop();
sum+=nums[i];
s2[i]=sum;
}
ll res=LONG_MAX;
for(int i=n/3-1;i<n/3*2;i++){
res = min(res, s1[i]-s2[i+1]);
}
return res;
}
};
网友评论