3357. Minimize the Maximum Adjacent Element Difference Hard

@problem@discussion
#Array#Binary Search#Greedy



1/**
2 * [3357] Minimize the Maximum Adjacent Element Difference
3 *
4 * You are given an array of integers nums. Some values in nums are missing and are denoted by -1.
5 * You can choose a pair of positive integers (x, y) exactly once and replace each missing element with either x or y.
6 * You need to minimize the maximum absolute difference between adjacent elements of nums after replacements.
7 * Return the minimum possible difference.
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">nums = [1,2,-1,10,8]</span>
12 * Output: <span class="example-io">4</span>
13 * Explanation:
14 * By choosing the pair as (6, 7), nums can be changed to [1, 2, 6, 10, 8].
15 * The absolute differences between adjacent elements are:
16 * 
17 * 	|1 - 2| == 1
18 * 	|2 - 6| == 4
19 * 	|6 - 10| == 4
20 * 	|10 - 8| == 2
21 * </div>
22 * <strong class="example">Example 2:
23 * <div class="example-block">
24 * Input: <span class="example-io">nums = [-1,-1,-1]</span>
25 * Output: <span class="example-io">0</span>
26 * Explanation:
27 * By choosing the pair as (4, 4), nums can be changed to [4, 4, 4].
28 * </div>
29 * <strong class="example">Example 3:
30 * <div class="example-block">
31 * Input: <span class="example-io">nums = [-1,10,-1,8]</span>
32 * Output: <span class="example-io">1</span>
33 * Explanation:
34 * By choosing the pair as (11, 9), nums can be changed to [11, 10, 9, 8].
35 * </div>
36 *  
37 * Constraints:
38 * 
39 * 	2 <= nums.length <= 10^5
40 * 	nums[i] is either -1 or in the range [1, 10^9].
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/minimize-the-maximum-adjacent-element-difference/
46// discuss: https://leetcode.com/problems/minimize-the-maximum-adjacent-element-difference/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn min_difference(nums: Vec<i32>) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_3357() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.