8000 New Problem Solution "Rotate Function" · JavaScriptor/leetcode@421189e · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 421189e

Browse files
committed
New Problem Solution "Rotate Function"
1 parent 2d433dd commit 421189e

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LeetCode
88

99
| # | Title | Solution | Difficulty |
1010
|---| ----- | -------- | ---------- |
11+
|396|[Rotate Function](https://leetcode.com/problems/rotate-function/) | [C++](./algorithms/cpp/rotateFunction/RotateFunction.cpp)|Easy|
1112
|395|[Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/) | [C++](./algorithms/cpp/longestSubstringWithAtLeastKRepeatingCharacters/LongestSubstringWithAtLeastKRepeatingCharacters.cpp)|Medium|
1213
|394|[Decode String](https://leetcode.com/problems/decode-string/) | [C++](./algorithms/cpp/decodeString/DecodeString.cpp)|Medium|
1314
|393|[UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/) | [C++](./algorithms/cpp/UTF8Validation/UTF8Validation.cpp)|Medium|
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Source : https://leetcode.com/problems/rotate-function/
2+
// Author : Hao Chen
3+
// Date : 2016-11-03
4+
5+
/***************************************************************************************
6+
*
7+
* Given an array of integers A and let n to be its length.
8+
*
9+
* Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we
10+
* define a "rotation function" F on A as follow:
11+
*
12+
* F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].
13+
*
14+
* Calculate the maximum value of F(0), F(1), ..., F(n-1).
15+
*
16+
* Note:
17+
* n is guaranteed to be less than 105.
18+
*
19+
* Example:
20+
*
21+
* A = [4, 3, 2, 6]
22+
*
23+
* F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
24+
* F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
25+
* F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
26+
* F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
27+
*
28+
* So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.
29+
***************************************************************************************/
30+
31+
32+
//
33+
// Asumming we have 4 numbers: a, b, c, d, then
34+
// F(0) = 0a + 1b + 2c + 3d
35+
// F(1) = 3a + 0b + 1c + 2d
36+
// F(2) = 2a + 3b + 0c + 1d
37+
// F(3) = 1a + 2b + 3c + 0d
38+
//
39+
// We can see how F(n) transfrom to F(n+1)
40+
// F(0) - F(1) = -3a + b + c + d
41+
// F(1) - F(2) = a + -3b + c + d
42+
// F(2) - F(3) = a + b + -3c + d
43+
// F(3) - F(0) = a + b + c + -3d
44+
//
45+
// So, we can tansfrom to the following experssion:
46+
//
47+
// F(1) = F(0) - (a+b+c+d) + 4a
48+
// F(2) = F[1] - (a+b+c+d) + 4b
49+
// F(3) = F[2] - (a+b+c+d) + 4c
50+
//
51+
// Then, we can see this fomular:
52+
//
53+
// F(n) = F(n-1) - sum(array) + len(array) * array[n-1]
54+
//
55+
class Solution {
56+
public:
57+
int maxRotateFunction(vector<int>& A) {
58+
int sum = 0;
59+
int F = 0;
60+
for (int i=0; i < A.size(); i++) {
61+
sum += A[i];
62+
F += (i * A[i]);
63+
}
64+
int maxF = F;
65+
int len = A.size();
66+
//cout << "F(0) = " << maxF <<endl;
67+
for (int i=1; i< len; i++) {
68+
F = F - sum + len * A[i-1];
69+
//cout << "F(" << i << ") = " << F << endl;
70+
maxF = max(maxF, F);
71+
}
72+
73+
return maxF;
74+
}
75+
};

0 commit comments

Comments
 (0)
0