All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0301_0400.s0396_rotate_function.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0301_0400.s0396_rotate_function;

// #Medium #Dynamic_Programming #Math

public class Solution {
    public int maxRotateFunction(int[] nums) {
        int allSum = 0;
        int len = nums.length;
        int f = 0;
        for (int i = 0; i < len; i++) {
            f += i * nums[i];
            allSum += nums[i];
        }
        int max = f;
        for (int i = len - 1; i >= 1; i--) {
            f = f + allSum - len * nums[i];
            max = Math.max(f, max);
        }
        return max;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy