Skip to content

Commit 82d4422

Browse files
committed
feat: add solutions to lc problem: No.2616
No.2616.Minimize the Maximum Difference of Pairs
1 parent 490508b commit 82d4422

File tree

4 files changed

+199
-8
lines changed

4 files changed

+199
-8
lines changed

solution/2600-2699/2616.Minimize the Maximum Difference of Pairs/README.md

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ tags:
6565

6666
我们注意到,最大差值具备单调性,即如果最大差值 $x$ 满足条件,那么 $x-1$ 也一定满足条件。因此我们可以使用二分查找的方法,找到最小的满足条件的最大差值。
6767

68-
我们可以将数组 `nums` 排序,然后枚举最大差值 $x$,判断是否存在 $p$ 个下标对,每个下标对对应数值取差值的最大值不超过 $x$。如果存在,那么我们就可以将 $x$ 减小,否则我们就将 $x$ 增大。
68+
我们可以将数组 $\textit{nums}$ 排序,然后枚举最大差值 $x$,判断是否存在 $p$ 个下标对,每个下标对对应数值取差值的最大值不超过 $x$。如果存在,那么我们就可以将 $x$ 减小,否则我们就将 $x$ 增大。
6969

70-
判断是否存在 $p$ 个下标对,每个下标对对应数值取差值的最大值不超过 $x$,可以使用贪心的方法。我们从左到右遍历数组 `nums`,对于当前遍历到的下标 $i$,如果 $i+1$ 位置的数与 $i$ 位置的数的差值不超过 $x$,那么我们就可以将 $i$ 和 $i+1$ 位置的数作为一个下标对,更新下标对的数量 $cnt$,然后将 $i$ 的值增加 $2$。否则,我们就将 $i$ 的值增加 $1$。遍历结束,如果 $cnt$ 的值大于等于 $p$,那么就说明存在 $p$ 个下标对,每个下标对对应数值取差值的最大值不超过 $x$,否则就说明不存在。
70+
判断是否存在 $p$ 个下标对,每个下标对对应数值取差值的最大值不超过 $x$,可以使用贪心的方法。我们从左到右遍历数组 $\textit{nums}$,对于当前遍历到的下标 $i$,如果 $i+1$ 位置的数与 $i$ 位置的数的差值不超过 $x$,那么我们就可以将 $i$ 和 $i+1$ 位置的数作为一个下标对,更新下标对的数量 $cnt$,然后将 $i$ 的值增加 $2$。否则,我们就将 $i$ 的值增加 $1$。遍历结束,如果 $cnt$ 的值大于等于 $p$,那么就说明存在 $p$ 个下标对,每个下标对对应数值取差值的最大值不超过 $x$,否则就说明不存在。
7171

72-
时间复杂度 $O(n \times (\log n + \log m))$,其中 $n$ 是数组 `nums` 的长度,而 $m$ 是数组 `nums` 中的最大值与最小值的差值。空间复杂度 $O(1)$。
72+
时间复杂度 $O(n \times (\log n + \log m))$,其中 $n$ 是数组 $\textit{nums}$ 的长度,而 $m$ 是数组 $\textit{nums}$ 中的最大值与最小值的差值。空间复杂度 $O(1)$。
7373

7474
<!-- tabs:start -->
7575

@@ -176,6 +176,73 @@ func minimizeMax(nums []int, p int) int {
176176
}
177177
```
178178

179+
#### TypeScript
180+
181+
```ts
182+
function minimizeMax(nums: number[], p: number): number {
183+
nums.sort((a, b) => a - b);
184+
const n = nums.length;
185+
let l = 0,
186+
r = nums[n - 1] - nums[0] + 1;
187+
const check = (diff: number): boolean => {
188+
let cnt = 0;
189+
for (let i = 0; i < n - 1; ++i) {
190+
if (nums[i + 1] - nums[i] <= diff) {
191+
++cnt;
192+
++i;
193+
}
194+
}
195+
return cnt >= p;
196+
};
197+
while (l < r) {
198+
const mid = (l + r) >> 1;
199+
if (check(mid)) {
200+
r = mid;
201+
} else {
202+
l = mid + 1;
203+
}
204+
}
205+
return l;
206+
}
207+
```
208+
209+
#### Rust
210+
211+
```rust
212+
impl Solution {
213+
pub fn minimize_max(mut nums: Vec<i32>, p: i32) -> i32 {
214+
nums.sort();
215+
let n = nums.len();
216+
let (mut l, mut r) = (0, nums[n - 1] - nums[0] + 1);
217+
218+
let check = |diff: i32| -> bool {
219+
let mut cnt = 0;
220+
let mut i = 0;
221+
while i < n - 1 {
222+
if nums[i + 1] - nums[i] <= diff {
223+
cnt += 1;
224+
i += 2;
225+
} else {
226+
i += 1;
227+
}
228+
}
229+
cnt >= p
230+
};
231+
232+
while l < r {
233+
let mid = (l + r) / 2;
234+
if check(mid) {
235+
r = mid;
236+
} else {
237+
l = mid + 1;
238+
}
239+
}
240+
241+
l
242+
}
243+
}
244+
```
245+
179246
<!-- tabs:end -->
180247

181248
<!-- solution:end -->

solution/2600-2699/2616.Minimize the Maximum Difference of Pairs/README_EN.md

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0,
5959

6060
<!-- solution:start -->
6161

62-
### Solution 1: Binary search + Greedy
62+
### Solution 1: Binary Search + Greedy
6363

64-
We find that the maximum difference has the monotonicity, that is, if the maximum difference $x$ satisfies the condition, then $x-1$ must also satisfy the condition. Therefore, we can use the binary search method to find the smallest maximum difference that satisfies the condition.
64+
We notice that the maximum difference has monotonicity: if a maximum difference $x$ is feasible, then $x-1$ is also feasible. Therefore, we can use binary search to find the minimal feasible maximum difference.
6565

66-
We can sort the array `nums`, then enumerate the maximum difference $x$, and determine whether there are $p$ index pairs, where each index pair corresponds to the maximum value of the difference of the corresponding value. If it exists, we can reduce $x$, otherwise we can increase $x$.
66+
First, sort the array $\textit{nums}$. Then, for a given maximum difference $x$, check whether it is possible to form $p$ pairs of indices such that the maximum difference in each pair does not exceed $x$. If possible, we can try a smaller $x$; otherwise, we need to increase $x$.
6767

68-
Determine whether there are $p$ index pairs, where each index pair corresponds to the maximum value of the difference of the corresponding value, which can be achieved by using the greedy method. We traverse the array `nums` from left to right, and for the current traversed index $i$, if the difference between the number at the $i+1$ position and the number at the $i$ position is no more than $x$, then we can take the number at the $i$ and $i+1$ positions as an index pair, update the number of index pairs $cnt$, and then increase the value of $i$ by $2$. Otherwise, we will increase the value of $i$ by $1$. When the traversal is over, if the value of $cnt$ is greater than or equal to $p$, then it means that there are $p$ index pairs, where each index pair corresponds to the maximum value of the difference of the corresponding value, otherwise it means that it does not exist.
68+
To check whether $p$ such pairs exist with maximum difference at most $x$, we can use a greedy approach. Traverse the sorted array $\textit{nums}$ from left to right. For the current index $i$, if the difference between $\textit{nums}[i+1]$ and $\textit{nums}[i]$ does not exceed $x$, we can form a pair with $i$ and $i+1$, increment the pair count $cnt$, and increase $i$ by $2$. Otherwise, increase $i$ by $1$. After traversing, if $cnt \geq p$, then such $p$ pairs exist; otherwise, they do not.
6969

70-
The time complexity is $O(n \times (\log n + \log m))$, where $n$ is the length of the array `nums`, and $m$ is the difference between the maximum value and the minimum value in the array `nums`. The space complexity is $O(1)$.
70+
The time complexity is $O(n \times (\log n + \log m))$, where $n$ is the length of $\textit{nums}$ and $m$ is the difference between the maximum and minimum values in $\textit{nums}$. The space complexity is $O(1)$.
7171

7272
<!-- tabs:start -->
7373

@@ -174,6 +174,73 @@ func minimizeMax(nums []int, p int) int {
174174
}
175175
```
176176

177+
#### TypeScript
178+
179+
```ts
180+
function minimizeMax(nums: number[], p: number): number {
181+
nums.sort((a, b) => a - b);
182+
const n = nums.length;
183+
let l = 0,
184+
r = nums[n - 1] - nums[0] + 1;
185+
const check = (diff: number): boolean => {
186+
let cnt = 0;
187+
for (let i = 0; i < n - 1; ++i) {
188+
if (nums[i + 1] - nums[i] <= diff) {
189+
++cnt;
190+
++i;
191+
}
192+
}
193+
return cnt >= p;
194+
};
195+
while (l < r) {
196+
const mid = (l + r) >> 1;
197+
if (check(mid)) {
198+
r = mid;
199+
} else {
200+
l = mid + 1;
201+
}
202+
}
203+
return l;
204+
}
205+
```
206+
207+
#### Rust
208+
209+
```rust
210+
impl Solution {
211+
pub fn minimize_max(mut nums: Vec<i32>, p: i32) -> i32 {
212+
nums.sort();
213+
let n = nums.len();
214+
let (mut l, mut r) = (0, nums[n - 1] - nums[0] + 1);
215+
216+
let check = |diff: i32| -> bool {
217+
let mut cnt = 0;
218+
let mut i = 0;
219+
while i < n - 1 {
220+
if nums[i + 1] - nums[i] <= diff {
221+
cnt += 1;
222+
i += 2;
223+
} else {
224+
i += 1;
225+
}
226+
}
227+
cnt >= p
228+
};
229+
230+
while l < r {
231+
let mid = (l + r) / 2;
232+
if check(mid) {
233+
r = mid;
234+
} else {
235+
l = mid + 1;
236+
}
237+
}
238+
239+
l
240+
}
241+
}
242+
```
243+
177244
<!-- tabs:end -->
178245

179246
<!-- solution:end -->
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
impl Solution {
2+
pub fn minimize_max(mut nums: Vec<i32>, p: i32) -> i32 {
3+
nums.sort();
4+
let n = nums.len();
5+
let (mut l, mut r) = (0, nums[n - 1] - nums[0] + 1);
6+
7+
let check = |diff: i32| -> bool {
8+
let mut cnt = 0;
9+
let mut i = 0;
10+
while i < n - 1 {
11+
if nums[i + 1] - nums[i] <= diff {
12+
cnt += 1;
13+
i += 2;
14+
} else {
15+
i += 1;
16+
}
17+
}
18+
cnt >= p
19+
};
20+
21+
while l < r {
22+
let mid = (l + r) / 2;
23+
if check(mid) {
24+
r = mid;
25+
} else {
26+
l = mid + 1;
27+
}
28+
}
29+
30+
l
31+
}
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function minimizeMax(nums: number[], p: number): number {
2+
nums.sort((a, b) => a - b);
3+
const n = nums.length;
4+
let l = 0,
5+
r = nums[n - 1] - nums[0] + 1;
6+
const check = (diff: number): boolean => {
7+
let cnt = 0;
8+
for (let i = 0; i < n - 1; ++i) {
9+
if (nums[i + 1] - nums[i] <= diff) {
10+
++cnt;
11+
++i;
12+
}
13+
}
14+
return cnt >= p;
15+
};
16+
while (l < r) {
17+
const mid = (l + r) >> 1;
18+
if (check(mid)) {
19+
r = mid;
20+
} else {
21+
l = mid + 1;
22+
}
23+
}
24+
return l;
25+
}

0 commit comments

Comments
 (0)