1802. Maximum Value at a Given Index in a Bounded Array Medium

@problem@discussion
#Binary Search#Greedy



1/**
2 * [1802] Maximum Value at a Given Index in a Bounded Array
3 *
4 * You are given three positive integers: n, index, and maxSum. You want to construct an array nums (0-indexed) that satisfies the following conditions:
5 * 
6 * 	nums.length == n
7 * 	nums[i] is a positive integer where 0 <= i < n.
8 * 	abs(nums[i] - nums[i+1]) <= 1 where 0 <= i < n-1.
9 * 	The sum of all the elements of nums does not exceed maxSum.
10 * 	nums[index] is maximized.
11 * 
12 * Return nums[index] of the constructed array.
13 * Note that abs(x) equals x if x >= 0, and -x otherwise.
14 *  
15 * Example 1:
16 * 
17 * Input: n = 4, index = 2,  maxSum = 6
18 * Output: 2
19 * Explanation: nums = [1,2,<u>2</u>,1] is one array that satisfies all the conditions.
20 * There are no arrays that satisfy all the conditions and have nums[2] == 3, so 2 is the maximum nums[2].
21 * 
22 * Example 2:
23 * 
24 * Input: n = 6, index = 1,  maxSum = 10
25 * Output: 3
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	1 <= n <= maxSum <= 10^9
31 * 	0 <= index < n
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/maximum-value-at-a-given-index-in-a-bounded-array/
37// discuss: https://leetcode.com/problems/maximum-value-at-a-given-index-in-a-bounded-array/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn max_value(n: i32, index: i32, max_sum: i32) -> i32 {
43        0
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_1802() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.