您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页Leetcode - Rotate Function

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;
    }
}

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

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

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务