Leetcode-2439

Minimize Maximum of Array

Time complexity: $O(|nums|)$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>

class Solution {
public:
    int minimizeArrayValue(std::vector<int>& nums) {
        int i = 0;
        long long max_num = 0;
        long long sum = 0;
        for (int n : nums) {
            sum += n;
            ++i;
            if (sum) {
                max_num = std::max(max_num, (sum - 1) / i + 1);
            }
        }
        return max_num;
    }
};

Leetcode-2439

Licensed under CC BY-NC-SA 4.0