2789. Largest Element in an Array after Merge Operations Medium

@problem@discussion
#Array#Greedy



1/**
2 * [2789] Largest Element in an Array after Merge Operations
3 *
4 * You are given a 0-indexed array nums consisting of positive integers.
5 * You can do the following operation on the array any number of times:
6 * 
7 * 	Choose an integer i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1]. Replace the element nums[i + 1] with nums[i] + nums[i + 1] and delete the element nums[i] from the array.
8 * 
9 * Return the value of the largest element that you can possibly obtain in the final array.
10 *  
11 * <strong class="example">Example 1:
12 * 
13 * Input: nums = [2,3,7,9,3]
14 * Output: 21
15 * Explanation: We can apply the following operations on the array:
16 * - Choose i = 0. The resulting array will be nums = [<u>5</u>,7,9,3].
17 * - Choose i = 1. The resulting array will be nums = [5,<u>16</u>,3].
18 * - Choose i = 0. The resulting array will be nums = [<u>21</u>,3].
19 * The largest element in the final array is 21. It can be shown that we cannot obtain a larger element.
20 * 
21 * <strong class="example">Example 2:
22 * 
23 * Input: nums = [5,3,3]
24 * Output: 11
25 * Explanation: We can do the following operations on the array:
26 * - Choose i = 1. The resulting array will be nums = [5,<u>6</u>].
27 * - Choose i = 0. The resulting array will be nums = [<u>11</u>].
28 * There is only one element in the final array, which is 11.
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	1 <= nums.length <= 10^5
34 * 	1 <= nums[i] <= 10^6
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/largest-element-in-an-array-after-merge-operations/
40// discuss: https://leetcode.com/problems/largest-element-in-an-array-after-merge-operations/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn max_array_value(nums: Vec<i32>) -> i64 {
46        
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_2789() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.