Skip to content

Commit 7517072

Browse files
committed
+ problem 2439
1 parent 75b6fa2 commit 7517072

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 2439. Minimize Maximum of Array
2+
You are given a **0-indexed** array `nums` comprising of `n` non-negative integers.
3+
4+
In one operation, you must:
5+
6+
* Choose an integer `i` such that `1 <= i < n` and `nums[i] > 0`.
7+
* Decrease `nums[i]` by 1.
8+
* Increase `nums[i - 1]` by 1.
9+
10+
Return *the **minimum** possible value of the **maximum** integer of* `nums` *after performing **any** number of operations*.
11+
12+
#### Example 1:
13+
<pre>
14+
<strong>Input:</strong> nums = [3,7,1,6]
15+
<strong>Output:</strong> 5
16+
<strong>Explanation:</strong>
17+
One set of optimal operations is as follows:
18+
1. Choose i = 1, and nums becomes [4,6,1,6].
19+
2. Choose i = 3, and nums becomes [4,6,2,5].
20+
3. Choose i = 1, and nums becomes [5,5,2,5].
21+
The maximum integer of nums is 5. It can be shown that the maximum number cannot be less than 5.
22+
Therefore, we return 5.
23+
</pre>
24+
25+
#### Example 2:
26+
<pre>
27+
<strong>Input:</strong> nums = [10,1]
28+
<strong>Output:</strong> 10
29+
<strong>Explanation:</strong>
30+
It is optimal to leave nums as is, and since 10 is the maximum value, we return 10.
31+
</pre>
32+
33+
#### Constraints:
34+
* `n == nums.length`
35+
* <code>2 <= n <= 10<sup>5</sup></code>
36+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
37+
38+
## Solutions (Rust)
39+
40+
### 1. Solution
41+
```Rust
42+
impl Solution {
43+
pub fn minimize_array_value(nums: Vec<i32>) -> i32 {
44+
let n = nums.len();
45+
let mut lo = *nums.iter().min().unwrap() as i64;
46+
let mut hi = *nums.iter().max().unwrap() as i64;
47+
48+
while lo < hi {
49+
let m = (lo + hi) / 2;
50+
let mut x = nums[n - 1] as i64;
51+
52+
for i in (1..n).rev() {
53+
if x > m {
54+
x += nums[i - 1] as i64 - m;
55+
} else {
56+
x = nums[i - 1] as i64;
57+
}
58+
}
59+
60+
if x > m {
61+
lo = m + 1;
62+
} else {
63+
hi = m;
64+
}
65+
}
66+
67+
hi as i32
68+
}
69+
}
70+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 2439. 最小化数组中的最大值
2+
给你一个下标从 **0** 开始的数组 `nums` ,它含有 `n` 个非负整数。
3+
4+
每一步操作中,你需要:
5+
6+
* 选择一个满足 `1 <= i < n` 的整数 `i` ,且 `nums[i] > 0`
7+
*`nums[i]` 减 1 。
8+
*`nums[i - 1]` 加 1 。
9+
10+
你可以对数组执行 **任意** 次上述操作,请你返回可以得到的 `nums` 数组中 **最大值** **最小** 为多少。
11+
12+
#### 示例 1:
13+
<pre>
14+
<strong>输入:</strong> nums = [3,7,1,6]
15+
<strong>输出:</strong> 5
16+
<strong>解释:</strong>
17+
一串最优操作是:
18+
1. 选择 i = 1 ,nums 变为 [4,6,1,6] 。
19+
2. 选择 i = 3 ,nums 变为 [4,6,2,5] 。
20+
3. 选择 i = 1 ,nums 变为 [5,5,2,5] 。
21+
nums 中最大值为 5 。无法得到比 5 更小的最大值。
22+
所以我们返回 5 。
23+
</pre>
24+
25+
#### 示例 2:
26+
<pre>
27+
<strong>输入:</strong> nums = [10,1]
28+
<strong>输出:</strong> 10
29+
<strong>解释:</strong>
30+
最优解是不改动 nums ,10 是最大值,所以返回 10 。
31+
</pre>
32+
33+
#### 提示:
34+
* `n == nums.length`
35+
* <code>2 <= n <= 10<sup>5</sup></code>
36+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
37+
38+
## 题解 (Rust)
39+
40+
### 1. 题解
41+
```Rust
42+
impl Solution {
43+
pub fn minimize_array_value(nums: Vec<i32>) -> i32 {
44+
let n = nums.len();
45+
let mut lo = *nums.iter().min().unwrap() as i64;
46+
let mut hi = *nums.iter().max().unwrap() as i64;
47+
48+
while lo < hi {
49+
let m = (lo + hi) / 2;
50+
let mut x = nums[n - 1] as i64;
51+
52+
for i in (1..n).rev() {
53+
if x > m {
54+
x += nums[i - 1] as i64 - m;
55+
} else {
56+
x = nums[i - 1] as i64;
57+
}
58+
}
59+
60+
if x > m {
61+
lo = m + 1;
62+
} else {
63+
hi = m;
64+
}
65+
}
66+
67+
hi as i32
68+
}
69+
}
70+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn minimize_array_value(nums: Vec<i32>) -> i32 {
3+
let n = nums.len();
4+
let mut lo = *nums.iter().min().unwrap() as i64;
5+
let mut hi = *nums.iter().max().unwrap() as i64;
6+
7+
while lo < hi {
8+
let m = (lo + hi) / 2;
9+
let mut x = nums[n - 1] as i64;
10+
11+
for i in (1..n).rev() {
12+
if x > m {
13+
x += nums[i - 1] as i64 - m;
14+
} else {
15+
x = nums[i - 1] as i64;
16+
}
17+
}
18+
19+
if x > m {
20+
lo = m + 1;
21+
} else {
22+
hi = m;
23+
}
24+
}
25+
26+
hi as i32
27+
}
28+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,7 @@
14341434
[2433][2433l]|[Find The Original Array of Prefix Xor][2433] |![rs]
14351435
[2434][2434l]|[Using a Robot to Print the Lexicographically Smallest String][2434] |![rs]
14361436
[2437][2437l]|[Number of Valid Clock Times][2437] |![py]
1437+
[2439][2439l]|[Minimize Maximum of Array][2439] |![rs]
14371438
[2441][2441l]|[Largest Positive Integer That Exists With Its Negative][2441] |![rs]
14381439
[2442][2442l]|[Count Number of Distinct Integers After Reverse Operations][2442] |![py]
14391440
[2443][2443l]|[Sum of Number and Its Reverse][2443] |![py]
@@ -2939,6 +2940,7 @@
29392940
[2433]:Problemset/2433-Find%20The%20Original%20Array%20of%20Prefix%20Xor/README.md#2433-find-the-original-array-of-prefix-xor
29402941
[2434]:Problemset/2434-Using%20a%20Robot%20to%20Print%20the%20Lexicographically%20Smallest%20String/README.md#2434-using-a-robot-to-print-the-lexicographically-smallest-string
29412942
[2437]:Problemset/2437-Number%20of%20Valid%20Clock%20Times/README.md#2437-number-of-valid-clock-times
2943+
[2439]:Problemset/2439-Minimize%20Maximum%20of%20Array/README.md#2439-minimize-maximum-of-array
29422944
[2441]:Problemset/2441-Largest%20Positive%20Integer%20That%20Exists%20With%20Its%20Negative/README.md#2441-largest-positive-integer-that-exists-with-its-negative
29432945
[2442]:Problemset/2442-Count%20Number%20of%20Distinct%20Integers%20After%20Reverse%20Operations/README.md#2442-count-number-of-distinct-integers-after-reverse-operations
29442946
[2443]:Problemset/2443-Sum%20of%20Number%20and%20Its%20Reverse/README.md#2443-sum-of-number-and-its-reverse
@@ -4443,6 +4445,7 @@
44434445
[2433l]:https://leetcode.com/problems/find-the-original-array-of-prefix-xor/
44444446
[2434l]:https://leetcode.com/problems/using-a-robot-to-print-the-lexicographically-smallest-string/
44454447
[2437l]:https://leetcode.com/problems/number-of-valid-clock-times/
4448+
[2439l]:https://leetcode.com/problems/minimize-maximum-of-array/
44464449
[2441l]:https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/
44474450
[2442l]:https://leetcode.com/problems/count-number-of-distinct-integers-after-reverse-operations/
44484451
[2443l]:https://leetcode.com/problems/sum-of-number-and-its-reverse/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,7 @@
14341434
[2433][2433l]|[找出前缀异或的原始数组][2433] |![rs]
14351435
[2434][2434l]|[使用机器人打印字典序最小的字符串][2434] |![rs]
14361436
[2437][2437l]|[有效时间的数目][2437] |![py]
1437+
[2439][2439l]|[最小化数组中的最大值][2439] |![rs]
14371438
[2441][2441l]|[与对应负数同时存在的最大正整数][2441] |![rs]
14381439
[2442][2442l]|[反转之后不同整数的数目][2442] |![py]
14391440
[2443][2443l]|[反转之后的数字和][2443] |![py]
@@ -2939,6 +2940,7 @@
29392940
[2433]:Problemset/2433-Find%20The%20Original%20Array%20of%20Prefix%20Xor/README_CN.md#2433-找出前缀异或的原始数组
29402941
[2434]:Problemset/2434-Using%20a%20Robot%20to%20Print%20the%20Lexicographically%20Smallest%20String/README_CN.md#2434-使用机器人打印字典序最小的字符串
29412942
[2437]:Problemset/2437-Number%20of%20Valid%20Clock%20Times/README_CN.md#2437-有效时间的数目
2943+
[2439]:Problemset/2439-Minimize%20Maximum%20of%20Array/README_CN.md#2439-最小化数组中的最大值
29422944
[2441]:Problemset/2441-Largest%20Positive%20Integer%20That%20Exists%20With%20Its%20Negative/README_CN.md#2441-与对应负数同时存在的最大正整数
29432945
[2442]:Problemset/2442-Count%20Number%20of%20Distinct%20Integers%20After%20Reverse%20Operations/README_CN.md#2442-反转之后不同整数的数目
29442946
[2443]:Problemset/2443-Sum%20of%20Number%20and%20Its%20Reverse/README_CN.md#2443-反转之后的数字和
@@ -4443,6 +4445,7 @@
44434445
[2433l]:https://leetcode.cn/problems/find-the-original-array-of-prefix-xor/
44444446
[2434l]:https://leetcode.cn/problems/using-a-robot-to-print-the-lexicographically-smallest-string/
44454447
[2437l]:https://leetcode.cn/problems/number-of-valid-clock-times/
4448+
[2439l]:https://leetcode.cn/problems/minimize-maximum-of-array/
44464449
[2441l]:https://leetcode.cn/problems/largest-positive-integer-that-exists-with-its-negative/
44474450
[2442l]:https://leetcode.cn/problems/count-number-of-distinct-integers-after-reverse-operations/
44484451
[2443l]:https://leetcode.cn/problems/sum-of-number-and-its-reverse/

0 commit comments

Comments
 (0)