Leetcode - Rotate Function

My code:

public class Solution {
    public int maxRotateFunction(int[] A) {
        if (A == null || A.length == 0) {
            return 0;
        }
        
        int sum = 0;
        int iteration = 0;
        
        for (int i = 0; i < A.length; i++) {
            sum += A[i];
            iteration += i * A[i];
        }
        
        int max = iteration;
        for (int i = 1; i < A.length; i++) {
            iteration = iteration - sum + A[i - 1] * A.length;
            max = Math.max(max, iteration
            );
        }
        
        return max;
    }
}

reference:
https://discuss.leetcode.com/topic/58616/java-solution-o-n-with-non-mathametical-explaination

一开始超时了。后来看了答案,发现是可以用递推公式的,还是太急了,一点不会就看答案。

Anyway, Good luck, Richardo! — 10/12/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/d16b5f6d33c3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞