From a101162a6d0c847f2424d8f7babed5a79a2fa912 Mon Sep 17 00:00:00 2001 From: ShusenTang Date: Wed, 29 Jan 2020 18:17:37 +0800 Subject: [PATCH] add 396 --- README.md | 1 + solutions/396. Rotate Function.md | 51 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 solutions/396. Rotate Function.md diff --git a/README.md b/README.md index 12749af..62405dd 100644 --- a/README.md +++ b/README.md @@ -246,6 +246,7 @@ My LeetCode solutions with Chinese explanation. 我的LeetCode中文题解。 | 393 |[UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/393.%20UTF-8%20Validation.md)|Medium| | | 394 |[Decode String](https://leetcode.com/problems/decode-string/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/394.%20Decode%20String.md)|Medium| | | 395 |[Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/395.%20Longest%20Substring%20with%20At%20Least%20K%20Repeating%20Characters.md)|Medium| | +| 396 |[Rotate Function](https://leetcode.com/problems/rotate-function/)|[C++](solutions/396.%20Rotate%20Function.md)|Medium| | | 397 |[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/397.%20Integer%20Replacement.md)|Medium| | | 398 |[Random Pick Index](https://leetcode.com/problems/random-pick-index/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/398.%20Random%20Pick%20Index.md)|Medium| | | 400 |[Nth Digit](https://leetcode.com/problems/nth-digit)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/400.%20Nth%20Digit.md)|Easy| | diff --git a/solutions/396. Rotate Function.md b/solutions/396. Rotate Function.md new file mode 100644 index 0000000..ce082a3 --- /dev/null +++ b/solutions/396. Rotate Function.md @@ -0,0 +1,51 @@ +# [396. Rotate Function](https://leetcode.com/problems/rotate-function/) + +# 思路 + +给定一个数组,然后给出F(i)的定义,求F(i)的最大值。 +此题最重要的就是根据定义快速求解出F(i)。 + +先写出前几项(注意将A[i]对齐了): +``` +F(0) = 0*A[0] + 1*A[1] + 2*A[2] + 3*A[3] + ... + (n-2)*A[n-2] + (n-1) * A[n-1] +F(1) = 1*A[0] + 2*A[2] + 3*A[3] + ... + (n-1)*A[n-2] + 0*A[n-1] +F(2) = 2*A[0] + 3*A[2] + 4*A[3] + ... + 0*A[n-2] + 1*A[n-1] +... +``` + +所以我们有 +``` +F(1) = F(0) + sum - A[n-1] - (n-1)*A[n-1]; +F(2) = F(1) + sum - A[n-2] - (n-1)*A[n-2]; +... + +F(i) = F(i-1) + sum - A[n-i] - (n-1)*A[n-i] = F(i-1) + sum - n * A[n-i]; +其中 sum = A[0] + A[1] + ... A[n-1] +``` + +所以我们可以先遍历一遍数组把sum和F(0)求出来, 再遍历一遍数组把每个F(i)都求出来同时保持一个全局最大值。 + +注意可能会溢出,所以我们用long long型。 + +两次遍历,时间复杂度O(n),用滚动数组的思想可优化空间复杂度O(1) + +# C++ +``` C++ +class Solution { +public: + int maxRotateFunction(vector& A) { + long long sum = 0, f0 = 0, n = A.size(); + for(int i = 0; i < n; i++){ + sum += A[i]; + f0 += i*A[i]; + } + long long res = f0, fi = f0; + for(int i = 1; i < n; i++){ + // fi = fi + sum - A[n-i] - (n-1)*A[n-i]; + fi += (sum - n * A[n-i]); + res = max(res, fi); + } + return int(res); + } +}; +``` \ No newline at end of file