当前位置:网站首页>Leetcode 2163. Minimum difference of sum after element deletion
Leetcode 2163. Minimum difference of sum after element deletion
2022-06-25 06:38:00 【Python's path to becoming a God】
I'll give you a subscript from 0 The starting array of integers nums , It contains 3 * n Elements .
You can start your nums Delete in just n Elements , The rest 2 * n The elements will be divided into two Same size Part of .
front n The first element belongs to the first part , Their sum is recorded as sumfirst .
Back n The first element belongs to the second part , Their sum is recorded as sumsecond .
The sum of two parts Difference value Write it down as sumfirst - sumsecond .
For example ,sumfirst = 3 And sumsecond = 2 , Their difference is 1 .
Let's take another example ,sumfirst = 2 And sumsecond = 3 , Their difference is -1 .
Please go back and delete n After elements , The remaining two parts and The minimum value of the difference How much is the .
Example 1:
Input :nums = [3,1,2]
Output :-1
explain :nums Yes 3 Elements , therefore n = 1 .
So we need to nums Delete in 1 Elements , And divide the remaining elements into two parts .
- If we delete nums[0] = 3 , The array becomes [1,2] . The difference between the sum of the two parts is 1 - 2 = -1 .
- If we delete nums[1] = 1 , The array becomes [3,2] . The difference between the sum of the two parts is 3 - 2 = 1 .
- If we delete nums[2] = 2 , The array becomes [3,1] . The difference between the sum of the two parts is 3 - 1 = 2 .
The minimum difference between the sum of two parts is min(-1,1,2) = -1 .
Example 2:
Input :nums = [7,9,5,8,1,3]
Output :1
explain :n = 2 . So we need to delete 2 Elements , And divide the remaining elements into 2 part .
If we delete the element nums[2] = 5 and nums[3] = 8 , The remaining elements are [7,9,1,3] . The difference between and is (7+9) - (1+3) = 12 .
In order to get the minimum difference , We should delete nums[1] = 9 and nums[4] = 1 , The remaining elements are [7,5,8,3] . The difference between and is (7+5) - (8+3) = 1 .
Observation can be seen , The best answer is 1 .
Tips :
nums.length == 3 * n
1 <= n <= 105
1 <= nums[i] <= 105
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/minimum-difference-in-sums-after-removal-of-elements
solution :
Priority queue + The prefix and . Read the prompt and then write it out , How to solve this problem sumfisrt-sumsecond The difference is the smallest , It should be sumfirst As small as possible ,sumsecond As big as possible , about sumfirst[i] Before presentation i Of the elements n Minimum value of sum of numbers , about sumsecond[i] similar , It's just from the back to the front , For maximum . We can use the priority queue to find n The maximum number .
class Solution {
public:
long long minimumDifference(vector<int>& nums) {
using ll = long long;
priority_queue<ll, vector<ll>, less<ll>> pq;// The operation of the priority queue is similar to that of the stack
int n3 = nums.size();
vector<ll> lSum(n3);
int n = n3 / 3;
ll sum = 0;
for (int i = 0; i < 2 * n; ++i)
{
if (i < n)
{
sum += nums[i];
pq.push(nums[i]);
}
else
{
if (nums[i] < pq.top())
{
sum -= pq.top();
pq.pop();
pq.push(nums[i]);
sum += nums[i];
}
}
lSum[i] = sum;
}
priority_queue<ll, vector<ll>, greater<ll>> pq2;
sum = 0;
ll ans = 1e18;
for (int i = n3 - 1; i >= n; --i)
{
if (i >= 2 * n)
{
sum += nums[i];
pq2.push(nums[i]);
}
else
{
if(nums[i] > pq2.top())
{
sum -= pq2.top();
pq2.pop();
pq2.push(nums[i]);
sum += nums[i];
}
}
if( i <= 2 * n)
ans = min(ans, lSum[i - 1] - sum);
}
return ans;
}
};边栏推荐
- Ht8513 single lithium battery power supply with built-in Dynamic Synchronous Boost 5W mono audio power amplifier IC solution
- Derivation of COS (a+b) =cosa*cosb-sina*sinb
- We cannot activate inspection type for article master in transaction code MM41?
- Research Report on investment share and application prospect of 1,3-propanediol (PDO) industry in the world and China 2022
- Coffee script unmatched outent error
- Is it safe to open a stock account on the Internet in Beijing?
- Period to string [repeat] - period to string [duplicate]
- Meta universe is over, Web 3.0 will be fooled again?
- ACWING/2004. 错字
- China rehabilitation hospital industry operation benefit analysis and operation situation investigation report 2022
猜你喜欢

After unplugging the network cable, does the original TCP connection still exist?

3dmax软件的制作木桶过程:三步流程

VMware virtual machine prompt: the virtual device ide1:0 cannot be connected because there is no corresponding device on the host.

DNS domain name system

JS to determine whether an element exists in the array (four methods)

Exercise: completion

レレ / 蕾蕾

We cannot activate inspection type for article master in transaction code MM41?

From file system to distributed file system

アルマ / 炼金妹
随机推荐
The "&" character will destroy the data stored in the web The "&" character breaks passwords that are stored in the web config
Ht7180 3.7V L 12v/2a built in MOS high current boost IC solution
Derivation of sin (a-b) =sina*cosb-sinb*cosa
Zhinai's database
Cs4344/ht5010 stereo d/a digital to analog converter
TCP BBR as rate based
3dmax软件的制作木桶过程:三步流程
百度地图——入门教程
Wan Yin revealed that he was rejected by MIT in this way: "the department doesn't like you". He confronted the principal and realized
PHP and WMI – explore windows with PHP
You can see the classification of SQL injection. SQL injection point /sql injection type /sql injection has several /sql injection point classifications
Personal blog system graduation project opening report
原子Alpha开发板--SD卡和emmc烧录工具
joda.time获取日期总结
HCIP Day 16
Kotlin reflection -- Notes
DNS domain name system
Derivation of COS (a-b) =cosa*cosb+sina*sinb
sin(a-b)=sina*cosb-sinb*cosa的推导过程
Monitoring access: how to grant minimum WMI access to the monitoring service account