C++ solution


  • 8
    J
    	int maxRotateFunction(vector<int>& A) {
    		if (A.size() == 0) return 0;
    
    		long long allsum = 0;
    		long long sum2 = 0;
    		for (int i = 0; i < A.size(); i++) {
    			allsum += A[i] * i;
    			sum2 += A[i];
    		}
    
    		long long result = allsum;
    		for (int i = 0; i < A.size(); i++) {
    			allsum -= sum2;
    			allsum += A[i];
    			allsum += A[i] * int(A.size() - 1);
    			result = max(allsum, result);
    		}
    
    		return result;
    	}
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.