当前位置:网站首页>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;
}
};边栏推荐
- Observation configuring wmic
- sin(a+b)=sina*cosb+sinb*cosa的推导过程
- Acwing2013. three lines
- Grouped uitableview has 20px of extra padding at the bottom
- 2022 AI trend 8 forecast!
- Navicat防止新建查询误删
- BGP - basic concept
- Cs5092 5V USB input boost two section lithium battery charging management IC, SOT23-6 miniature package
- What does cardinality mean in set
- cos(a-b)=cosa*cosb+sina*sinb的推导过程
猜你喜欢

Cs4344/ht5010 stereo d/a digital to analog converter

SAP QM executes the transaction code qp01, and the system reports an error -material type food is not defined for task list type Q-

Microsoft issued a document to celebrate Net 20th anniversary!

Understand what MSS is

2022 AI trend 8 forecast!

What is VLAN

Derivation of COS (a-b) =cosa*cosb+sina*sinb

Cs8683 (120W mono class D power amplifier IC)
![[short time average zero crossing rate] short time average zero crossing rate of speech signal based on MATLAB [including Matlab source code 1721]](/img/4a/304f262c1c08800aa95f9e2d537e4d.jpg)
[short time average zero crossing rate] short time average zero crossing rate of speech signal based on MATLAB [including Matlab source code 1721]

Viewing Chinese science and technology from the Winter Olympics (V): the Internet of things
随机推荐
Report on strategic suggestions on investment direction and Prospect of global and Chinese marine biological industry (2022 Edition)
Metauniverse in 2022: robbing people, burning money and breaking through the experience boundary
Global and China chemical mechanical polishing abrasive materials market demand outlook and investment scale forecast report 2022 Edition
In a single-page app, what is the right way to deal with wrong URLs (404 errors)?
Acwing / 2004. Mauvaise écriture
Methods for obtaining some information of equipment
How do I check swift if two arrays contain the same elements, regardless of the order in which they appear?
Explain @builder usage
Comparison test of mono 120W high power class D power amplifier chip cs8683-tpa3116
The "&" character will destroy the data stored in the web The "&" character breaks passwords that are stored in the web config
Cs4344/ht5010 stereo d/a digital to analog converter
Global and Chinese kaolin market operation scale and investment development proposal report 2022
Asemi fast recovery diode us1m parameters, us1m recovery time, us1m voltage drop
使用OpenGL绘制shp文件
Understand what MSS is
3dmax软件的制作木桶过程:三步流程
Sophomores majoring in mechanics build a manipulator by hand -- full of compromise
[core content and derivation] the mystery of human memory system may be just like this
ASP. Net core - Safety of asynclocal in asp NET Core
Face++ realizes face detection by flow