rotate-function

https://leetcode.com/problems/rotate-function/

public class Solution {
    public int maxRotateFunction(int[] A) {
        int all = 0;
        int sum = 0;
        int ret = Integer.MIN_VALUE;
        for (int i=0; i<A.length; i++) {
            all += A[i];
            sum += i * A[i];
        }
        if (sum > ret) {
            ret = sum;
        }
        for (int i=A.length-1; i>0; i--) {
            sum -= A.length * A[i];
            sum += all;
            if (sum > ret) {
                ret = sum;
            }
        }
        return ret;
    }
}

 

posted @ 2016-09-17 22:55  blcblc  阅读(225)  评论(0)    收藏  举报