2789. Largest Element in an Array after Merge Operations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public long maxArrayValue(int[] nums) {
long max = 0; // 注意潜在的整数越界问题

for (int i = nums.length - 1; i >= 0; i--) {
if (nums[i] <= max) {
max += nums[i];
} else {
max = nums[i];
}
}

return max;
}
}

References

2789. Largest Element in an Array after Merge Operations