摘要:是數(shù)組各位累加和,是按照對數(shù)組乘積變換后的累加和,是題目所求的不同變換累加和的最大值。
Problem
Given an array of integers A and let n to be its length.
Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow:
F(k) = 0 Bk[0] + 1 Bk[1] + ... + (n-1) * Bk[n-1].
Calculate the maximum value of F(0), F(1), ..., F(n-1).
Note:
n is guaranteed to be less than 105.
Example:
A = [4, 3, 2, 6] F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25 F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16 F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23 F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.
NoteoneSum是數(shù)組A各位累加和,patternSum是按照pattern對數(shù)組乘積變換后的累加和,max是題目所求的不同變換累加和的最大值。
oneSum = 1A + 1B + 1C + 1D; patternSum = 0A + 1B + 2C + 3D;
然后做一個遞歸:每次對patternSum減少oneSum,并加上當(dāng)前遞歸對應(yīng)的A[i]*len,這樣做的道理是什么呢:
max = patternSum = 0A + 1B + 2C + 3D; A[i]*len = 4A; patternSum = -A + 0B + 1C + 2D + 4A = 0B + 1C + 2D + 3A; max = Math.max(max, patternSum)= Math.max(0A1B2C3D, 3A0B1C2D);
也就是說,每一次循環(huán),相當(dāng)于改變一次pattern,從0123到3012,再到2301,到1230結(jié)束,取這之中的最大值返回即可。
Solutionpublic class Solution { public int maxRotateFunction(int[] A) { if (A == null || A.length == 0) return 0; int oneSum = 0, len = A.length, patternSum = 0; for (int i = 0; i < len; i++) { oneSum += A[i]; patternSum += (A[i] * i); } int max = patternSum; for (int i = 0; i < len; i++) { patternSum += len * A[i] - oneSum; max = Math.max(max, patternSum); } return max; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/66224.html
摘要:問題描述解題思路使用數(shù)組自帶的方法和方法把數(shù)組最后一個取出來加入到頭部。使用數(shù)組的方法得到后個數(shù),再用方法刪去后個數(shù),最后用方法把得到的后個數(shù)添加到數(shù)組前面。 問題描述: 189.Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, t...
摘要:題目要求代表對數(shù)組在位置上進(jìn)行順時針的旋轉(zhuǎn)后生成的數(shù)組。暴力循環(huán)按照題目的要求,執(zhí)行兩次循環(huán)即可以獲得的所有值,只需要從中比較最大值即可。 題目要求 Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k p...
Problem Given an array, rotate the array to the right by k steps, where k is non-negative. Example Example 1: Input: [1,2,3,4,5,6,7] and k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the r...
Problem You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to modify the input 2D mat...
LeetCode[48] Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 復(fù)雜度O(N^2),O(1) 代碼 public void ro...
閱讀 3705·2021-11-22 13:52
閱讀 3603·2019-12-27 12:20
閱讀 2385·2019-08-30 15:55
閱讀 2144·2019-08-30 15:44
閱讀 2262·2019-08-30 13:16
閱讀 574·2019-08-28 18:19
閱讀 1881·2019-08-26 11:58
閱讀 3436·2019-08-26 11:47