Skip to content

Commit 4ea153a

Browse files
authoredAug 25, 2023
feat: add solutions to lc problem: No.2163 (doocs#1504)
No.2163.Minimum Difference in Sums After Removal of Elements
1 parent a12260e commit 4ea153a

File tree

10 files changed

+586
-7
lines changed

10 files changed

+586
-7
lines changed
 

‎solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README.md

+205-2
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,231 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:优先队列(大小根堆)+ 前后缀和 + 枚举分割点**
66+
67+
题目实际上等价于在 $nums$ 中找到一个分割点,将数组分成左右两部分,在前一部分中选取最小的 $n$ 个元素,在后一部分中选取最大的 $n$ 个元素,使得两部分和的差值最小。
68+
69+
我们可以用一个大根堆维护前缀中最小的 $n$ 个元素,用一个小根堆维护后缀中最大的 $n$ 个元素。我们定义 $pre[i]$ 表示在数组 $nums$ 的前 $i$ 个元素中选择最小的 $n$ 个元素的和,定义 $suf[i]$ 表示从数组第 $i$ 个元素到最后一个元素中选择最大的 $n$ 个元素的和。在维护大小根堆的过程中,更新 $pre[i]$ 和 $suf[i]$ 的值。
70+
71+
最后,我们在 $i \in [n, 2n]$ 的范围内枚举分割点,计算 $pre[i] - suf[i + 1]$ 的值,取最小值即可。
72+
73+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
74+
6575
<!-- tabs:start -->
6676

6777
### **Python3**
6878

6979
<!-- 这里可写当前语言的特殊实现逻辑 -->
7080

7181
```python
72-
82+
class Solution:
83+
def minimumDifference(self, nums: List[int]) -> int:
84+
m = len(nums)
85+
n = m // 3
86+
87+
s = 0
88+
pre = [0] * (m + 1)
89+
q1 = []
90+
for i, x in enumerate(nums[: n * 2], 1):
91+
s += x
92+
heappush(q1, -x)
93+
if len(q1) > n:
94+
s -= -heappop(q1)
95+
pre[i] = s
96+
97+
s = 0
98+
suf = [0] * (m + 1)
99+
q2 = []
100+
for i in range(m, n, -1):
101+
x = nums[i - 1]
102+
s += x
103+
heappush(q2, x)
104+
if len(q2) > n:
105+
s -= heappop(q2)
106+
suf[i] = s
107+
108+
return min(pre[i] - suf[i + 1] for i in range(n, n * 2 + 1))
73109
```
74110

75111
### **Java**
76112

77113
<!-- 这里可写当前语言的特殊实现逻辑 -->
78114

79115
```java
116+
class Solution {
117+
public long minimumDifference(int[] nums) {
118+
int m = nums.length;
119+
int n = m / 3;
120+
long s = 0;
121+
long[] pre = new long[m + 1];
122+
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
123+
for (int i = 1; i <= n * 2; ++i) {
124+
int x = nums[i - 1];
125+
s += x;
126+
pq.offer(x);
127+
if (pq.size() > n) {
128+
s -= pq.poll();
129+
}
130+
pre[i] = s;
131+
}
132+
s = 0;
133+
long[] suf = new long[m + 1];
134+
pq = new PriorityQueue<>();
135+
for (int i = m; i > n; --i) {
136+
int x = nums[i - 1];
137+
s += x;
138+
pq.offer(x);
139+
if (pq.size() > n) {
140+
s -= pq.poll();
141+
}
142+
suf[i] = s;
143+
}
144+
long ans = 1L << 60;
145+
for (int i = n; i <= n * 2; ++i) {
146+
ans = Math.min(ans, pre[i] - suf[i + 1]);
147+
}
148+
return ans;
149+
}
150+
}
151+
```
80152

153+
### **C++**
154+
155+
```cpp
156+
class Solution {
157+
public:
158+
long long minimumDifference(vector<int>& nums) {
159+
int m = nums.size();
160+
int n = m / 3;
161+
162+
using ll = long long;
163+
ll s = 0;
164+
ll pre[m + 1];
165+
priority_queue<int> q1;
166+
for (int i = 1; i <= n * 2; ++i) {
167+
int x = nums[i - 1];
168+
s += x;
169+
q1.push(x);
170+
if (q1.size() > n) {
171+
s -= q1.top();
172+
q1.pop();
173+
}
174+
pre[i] = s;
175+
}
176+
s = 0;
177+
ll suf[m + 1];
178+
priority_queue<int, vector<int>, greater<int>> q2;
179+
for (int i = m; i > n; --i) {
180+
int x = nums[i - 1];
181+
s += x;
182+
q2.push(x);
183+
if (q2.size() > n) {
184+
s -= q2.top();
185+
q2.pop();
186+
}
187+
suf[i] = s;
188+
}
189+
ll ans = 1e18;
190+
for (int i = n; i <= n * 2; ++i) {
191+
ans = min(ans, pre[i] - suf[i + 1]);
192+
}
193+
return ans;
194+
}
195+
};
196+
```
197+
198+
### **Go**
199+
200+
```go
201+
func minimumDifference(nums []int) int64 {
202+
m := len(nums)
203+
n := m / 3
204+
s := 0
205+
pre := make([]int, m+1)
206+
q1 := hp{}
207+
for i := 1; i <= n*2; i++ {
208+
x := nums[i-1]
209+
s += x
210+
heap.Push(&q1, -x)
211+
if q1.Len() > n {
212+
s -= -heap.Pop(&q1).(int)
213+
}
214+
pre[i] = s
215+
}
216+
s = 0
217+
suf := make([]int, m+1)
218+
q2 := hp{}
219+
for i := m; i > n; i-- {
220+
x := nums[i-1]
221+
s += x
222+
heap.Push(&q2, x)
223+
if q2.Len() > n {
224+
s -= heap.Pop(&q2).(int)
225+
}
226+
suf[i] = s
227+
}
228+
ans := int64(1e18)
229+
for i := n; i <= n*2; i++ {
230+
ans = min(ans, int64(pre[i]-suf[i+1]))
231+
}
232+
return ans
233+
}
234+
235+
func min(a, b int64) int64 {
236+
if a < b {
237+
return a
238+
}
239+
return b
240+
}
241+
242+
type hp struct{ sort.IntSlice }
243+
244+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
245+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
246+
func (h *hp) Pop() interface{} {
247+
a := h.IntSlice
248+
v := a[len(a)-1]
249+
h.IntSlice = a[:len(a)-1]
250+
return v
251+
}
81252
```
82253

83254
### **TypeScript**
84255

85256
```ts
86-
257+
function minimumDifference(nums: number[]): number {
258+
const m = nums.length;
259+
const n = Math.floor(m / 3);
260+
let s = 0;
261+
const pre: number[] = Array(m + 1);
262+
const q1 = new MaxPriorityQueue();
263+
for (let i = 1; i <= n * 2; ++i) {
264+
const x = nums[i - 1];
265+
s += x;
266+
q1.enqueue(x, x);
267+
if (q1.size() > n) {
268+
s -= q1.dequeue().element;
269+
}
270+
pre[i] = s;
271+
}
272+
s = 0;
273+
const suf: number[] = Array(m + 1);
274+
const q2 = new MinPriorityQueue();
275+
for (let i = m; i > n; --i) {
276+
const x = nums[i - 1];
277+
s += x;
278+
q2.enqueue(x, x);
279+
if (q2.size() > n) {
280+
s -= q2.dequeue().element;
281+
}
282+
suf[i] = s;
283+
}
284+
let ans = Number.MAX_SAFE_INTEGER;
285+
for (let i = n; i <= n * 2; ++i) {
286+
ans = Math.min(ans, pre[i] - suf[i + 1]);
287+
}
288+
return ans;
289+
}
87290
```
88291

89292
### **...**

‎solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README_EN.md

+194-1
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,212 @@ It can be shown that it is not possible to obtain a difference smaller than 1.
6363
### **Python3**
6464

6565
```python
66+
class Solution:
67+
def minimumDifference(self, nums: List[int]) -> int:
68+
m = len(nums)
69+
n = m // 3
6670

71+
s = 0
72+
pre = [0] * (m + 1)
73+
q1 = []
74+
for i, x in enumerate(nums[: n * 2], 1):
75+
s += x
76+
heappush(q1, -x)
77+
if len(q1) > n:
78+
s -= -heappop(q1)
79+
pre[i] = s
80+
81+
s = 0
82+
suf = [0] * (m + 1)
83+
q2 = []
84+
for i in range(m, n, -1):
85+
x = nums[i - 1]
86+
s += x
87+
heappush(q2, x)
88+
if len(q2) > n:
89+
s -= heappop(q2)
90+
suf[i] = s
91+
92+
return min(pre[i] - suf[i + 1] for i in range(n, n * 2 + 1))
6793
```
6894

6995
### **Java**
7096

7197
```java
98+
class Solution {
99+
public long minimumDifference(int[] nums) {
100+
int m = nums.length;
101+
int n = m / 3;
102+
long s = 0;
103+
long[] pre = new long[m + 1];
104+
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
105+
for (int i = 1; i <= n * 2; ++i) {
106+
int x = nums[i - 1];
107+
s += x;
108+
pq.offer(x);
109+
if (pq.size() > n) {
110+
s -= pq.poll();
111+
}
112+
pre[i] = s;
113+
}
114+
s = 0;
115+
long[] suf = new long[m + 1];
116+
pq = new PriorityQueue<>();
117+
for (int i = m; i > n; --i) {
118+
int x = nums[i - 1];
119+
s += x;
120+
pq.offer(x);
121+
if (pq.size() > n) {
122+
s -= pq.poll();
123+
}
124+
suf[i] = s;
125+
}
126+
long ans = 1L << 60;
127+
for (int i = n; i <= n * 2; ++i) {
128+
ans = Math.min(ans, pre[i] - suf[i + 1]);
129+
}
130+
return ans;
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
long long minimumDifference(vector<int>& nums) {
141+
int m = nums.size();
142+
int n = m / 3;
143+
144+
using ll = long long;
145+
ll s = 0;
146+
ll pre[m + 1];
147+
priority_queue<int> q1;
148+
for (int i = 1; i <= n * 2; ++i) {
149+
int x = nums[i - 1];
150+
s += x;
151+
q1.push(x);
152+
if (q1.size() > n) {
153+
s -= q1.top();
154+
q1.pop();
155+
}
156+
pre[i] = s;
157+
}
158+
s = 0;
159+
ll suf[m + 1];
160+
priority_queue<int, vector<int>, greater<int>> q2;
161+
for (int i = m; i > n; --i) {
162+
int x = nums[i - 1];
163+
s += x;
164+
q2.push(x);
165+
if (q2.size() > n) {
166+
s -= q2.top();
167+
q2.pop();
168+
}
169+
suf[i] = s;
170+
}
171+
ll ans = 1e18;
172+
for (int i = n; i <= n * 2; ++i) {
173+
ans = min(ans, pre[i] - suf[i + 1]);
174+
}
175+
return ans;
176+
}
177+
};
178+
```
179+
180+
### **Go**
181+
182+
```go
183+
func minimumDifference(nums []int) int64 {
184+
m := len(nums)
185+
n := m / 3
186+
s := 0
187+
pre := make([]int, m+1)
188+
q1 := hp{}
189+
for i := 1; i <= n*2; i++ {
190+
x := nums[i-1]
191+
s += x
192+
heap.Push(&q1, -x)
193+
if q1.Len() > n {
194+
s -= -heap.Pop(&q1).(int)
195+
}
196+
pre[i] = s
197+
}
198+
s = 0
199+
suf := make([]int, m+1)
200+
q2 := hp{}
201+
for i := m; i > n; i-- {
202+
x := nums[i-1]
203+
s += x
204+
heap.Push(&q2, x)
205+
if q2.Len() > n {
206+
s -= heap.Pop(&q2).(int)
207+
}
208+
suf[i] = s
209+
}
210+
ans := int64(1e18)
211+
for i := n; i <= n*2; i++ {
212+
ans = min(ans, int64(pre[i]-suf[i+1]))
213+
}
214+
return ans
215+
}
72216

217+
func min(a, b int64) int64 {
218+
if a < b {
219+
return a
220+
}
221+
return b
222+
}
223+
224+
type hp struct{ sort.IntSlice }
225+
226+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
227+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
228+
func (h *hp) Pop() interface{} {
229+
a := h.IntSlice
230+
v := a[len(a)-1]
231+
h.IntSlice = a[:len(a)-1]
232+
return v
233+
}
73234
```
74235

75236
### **TypeScript**
76237

77238
```ts
78-
239+
function minimumDifference(nums: number[]): number {
240+
const m = nums.length;
241+
const n = Math.floor(m / 3);
242+
let s = 0;
243+
const pre: number[] = Array(m + 1);
244+
const q1 = new MaxPriorityQueue();
245+
for (let i = 1; i <= n * 2; ++i) {
246+
const x = nums[i - 1];
247+
s += x;
248+
q1.enqueue(x, x);
249+
if (q1.size() > n) {
250+
s -= q1.dequeue().element;
251+
}
252+
pre[i] = s;
253+
}
254+
s = 0;
255+
const suf: number[] = Array(m + 1);
256+
const q2 = new MinPriorityQueue();
257+
for (let i = m; i > n; --i) {
258+
const x = nums[i - 1];
259+
s += x;
260+
q2.enqueue(x, x);
261+
if (q2.size() > n) {
262+
s -= q2.dequeue().element;
263+
}
264+
suf[i] = s;
265+
}
266+
let ans = Number.MAX_SAFE_INTEGER;
267+
for (let i = n; i <= n * 2; ++i) {
268+
ans = Math.min(ans, pre[i] - suf[i + 1]);
269+
}
270+
return ans;
271+
}
79272
```
80273

81274
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
long long minimumDifference(vector<int>& nums) {
4+
int m = nums.size();
5+
int n = m / 3;
6+
7+
using ll = long long;
8+
ll s = 0;
9+
ll pre[m + 1];
10+
priority_queue<int> q1;
11+
for (int i = 1; i <= n * 2; ++i) {
12+
int x = nums[i - 1];
13+
s += x;
14+
q1.push(x);
15+
if (q1.size() > n) {
16+
s -= q1.top();
17+
q1.pop();
18+
}
19+
pre[i] = s;
20+
}
21+
s = 0;
22+
ll suf[m + 1];
23+
priority_queue<int, vector<int>, greater<int>> q2;
24+
for (int i = m; i > n; --i) {
25+
int x = nums[i - 1];
26+
s += x;
27+
q2.push(x);
28+
if (q2.size() > n) {
29+
s -= q2.top();
30+
q2.pop();
31+
}
32+
suf[i] = s;
33+
}
34+
ll ans = 1e18;
35+
for (int i = n; i <= n * 2; ++i) {
36+
ans = min(ans, pre[i] - suf[i + 1]);
37+
}
38+
return ans;
39+
}
40+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
func minimumDifference(nums []int) int64 {
2+
m := len(nums)
3+
n := m / 3
4+
s := 0
5+
pre := make([]int, m+1)
6+
q1 := hp{}
7+
for i := 1; i <= n*2; i++ {
8+
x := nums[i-1]
9+
s += x
10+
heap.Push(&q1, -x)
11+
if q1.Len() > n {
12+
s -= -heap.Pop(&q1).(int)
13+
}
14+
pre[i] = s
15+
}
16+
s = 0
17+
suf := make([]int, m+1)
18+
q2 := hp{}
19+
for i := m; i > n; i-- {
20+
x := nums[i-1]
21+
s += x
22+
heap.Push(&q2, x)
23+
if q2.Len() > n {
24+
s -= heap.Pop(&q2).(int)
25+
}
26+
suf[i] = s
27+
}
28+
ans := int64(1e18)
29+
for i := n; i <= n*2; i++ {
30+
ans = min(ans, int64(pre[i]-suf[i+1]))
31+
}
32+
return ans
33+
}
34+
35+
func min(a, b int64) int64 {
36+
if a < b {
37+
return a
38+
}
39+
return b
40+
}
41+
42+
type hp struct{ sort.IntSlice }
43+
44+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
45+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
46+
func (h *hp) Pop() interface{} {
47+
a := h.IntSlice
48+
v := a[len(a)-1]
49+
h.IntSlice = a[:len(a)-1]
50+
return v
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public long minimumDifference(int[] nums) {
3+
int m = nums.length;
4+
int n = m / 3;
5+
long s = 0;
6+
long[] pre = new long[m + 1];
7+
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
8+
for (int i = 1; i <= n * 2; ++i) {
9+
int x = nums[i - 1];
10+
s += x;
11+
pq.offer(x);
12+
if (pq.size() > n) {
13+
s -= pq.poll();
14+
}
15+
pre[i] = s;
16+
}
17+
s = 0;
18+
long[] suf = new long[m + 1];
19+
pq = new PriorityQueue<>();
20+
for (int i = m; i > n; --i) {
21+
int x = nums[i - 1];
22+
s += x;
23+
pq.offer(x);
24+
if (pq.size() > n) {
25+
s -= pq.poll();
26+
}
27+
suf[i] = s;
28+
}
29+
long ans = 1L << 60;
30+
for (int i = n; i <= n * 2; ++i) {
31+
ans = Math.min(ans, pre[i] - suf[i + 1]);
32+
}
33+
return ans;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def minimumDifference(self, nums: List[int]) -> int:
3+
m = len(nums)
4+
n = m // 3
5+
6+
s = 0
7+
pre = [0] * (m + 1)
8+
q1 = []
9+
for i, x in enumerate(nums[: n * 2], 1):
10+
s += x
11+
heappush(q1, -x)
12+
if len(q1) > n:
13+
s -= -heappop(q1)
14+
pre[i] = s
15+
16+
s = 0
17+
suf = [0] * (m + 1)
18+
q2 = []
19+
for i in range(m, n, -1):
20+
x = nums[i - 1]
21+
s += x
22+
heappush(q2, x)
23+
if len(q2) > n:
24+
s -= heappop(q2)
25+
suf[i] = s
26+
27+
return min(pre[i] - suf[i + 1] for i in range(n, n * 2 + 1))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function minimumDifference(nums: number[]): number {
2+
const m = nums.length;
3+
const n = Math.floor(m / 3);
4+
let s = 0;
5+
const pre: number[] = Array(m + 1);
6+
const q1 = new MaxPriorityQueue();
7+
for (let i = 1; i <= n * 2; ++i) {
8+
const x = nums[i - 1];
9+
s += x;
10+
q1.enqueue(x, x);
11+
if (q1.size() > n) {
12+
s -= q1.dequeue().element;
13+
}
14+
pre[i] = s;
15+
}
16+
s = 0;
17+
const suf: number[] = Array(m + 1);
18+
const q2 = new MinPriorityQueue();
19+
for (let i = m; i > n; --i) {
20+
const x = nums[i - 1];
21+
s += x;
22+
q2.enqueue(x, x);
23+
if (q2.size() > n) {
24+
s -= q2.dequeue().element;
25+
}
26+
suf[i] = s;
27+
}
28+
let ans = Number.MAX_SAFE_INTEGER;
29+
for (let i = n; i <= n * 2; ++i) {
30+
ans = Math.min(ans, pre[i] - suf[i + 1]);
31+
}
32+
return ans;
33+
}

‎solution/2100-2199/2164.Sort Even and Odd Indices Independently/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
<ol>
1212
<li>按 <strong>非递增</strong> 顺序排列 <code>nums</code> <strong>奇数下标</strong> 上的所有值。
13-
1413
<ul>
1514
<li>举个例子,如果排序前 <code>nums = [4,<em><strong>1</strong></em>,2,<em><strong>3</strong></em>]</code> ,对奇数下标的值排序后变为 <code>[4,<em><strong>3</strong></em>,2,<em><strong>1</strong></em>]</code> 。奇数下标 <code>1</code> 和 <code>3</code> 的值按照非递增顺序重排。</li>
1615
</ul>
@@ -20,7 +19,6 @@
2019
<li>举个例子,如果排序前 <code>nums = [<em><strong>4</strong></em>,1,<em><strong>2</strong></em>,3]</code> ,对偶数下标的值排序后变为 <code>[<em><strong>2</strong></em>,1,<em><strong>4</strong></em>,3]</code> 。偶数下标 <code>0</code> 和 <code>2</code> 的值按照非递减顺序重排。</li>
2120
</ul>
2221
</li>
23-
2422
</ol>
2523

2624
<p>返回重排 <code>nums</code> 的值之后形成的数组。</p>

‎solution/2100-2199/2164.Sort Even and Odd Indices Independently/README_EN.md

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
<ol>
1010
<li>Sort the values at <strong>odd indices</strong> of <code>nums</code> in <strong>non-increasing</strong> order.
11-
1211
<ul>
1312
<li>For example, if <code>nums = [4,<strong><u>1</u></strong>,2,<u><strong>3</strong></u>]</code> before this step, it becomes <code>[4,<u><strong>3</strong></u>,2,<strong><u>1</u></strong>]</code> after. The values at odd indices <code>1</code> and <code>3</code> are sorted in non-increasing order.</li>
1413
</ul>
@@ -18,7 +17,6 @@
1817
<li>For example, if <code>nums = [<u><strong>4</strong></u>,1,<u><strong>2</strong></u>,3]</code> before this step, it becomes <code>[<u><strong>2</strong></u>,1,<u><strong>4</strong></u>,3]</code> after. The values at even indices <code>0</code> and <code>2</code> are sorted in non-decreasing order.</li>
1918
</ul>
2019
</li>
21-
2220
</ol>
2321

2422
<p>Return <em>the array formed after rearranging the values of</em> <code>nums</code>.</p>

‎solution/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
2117,
7171
2140,
7272
2145,
73+
2164,
7374
2178,
7475
2241,
7576
2288,

0 commit comments

Comments
 (0)
Please sign in to comment.