Rotate Function – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
You are given an integer array nums
of length n
.
Assume arrk
to be an array obtained by rotating nums
by k
positions clock-wise. We define the rotation function F
on nums
as follow:
F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].
Return the maximum value of F(0), F(1), ..., F(n-1)
.
The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: nums = [4,3,2,6] Output: 26 Explanation: 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.
Example 2:
Input: nums = [100] Output: 0
Constraints:
n == nums.length
1 <= n <= 105
C++ Rotate Function LeetCode Solution
class Solution {
public:
int maxRotateFunction(vector<int>& A) {
long sum = 0, fn = 0;
int len = A.size();
for(int i=0;i<len;i++) {
sum += A[i];
fn += (i * A[i]);
}
long l = 1, r;
long newfn = fn;
while(l < len) {
r = l + len - 1;
long removed = (l-1) * A[l-1];
long added = r * A[r%len];
newfn = newfn - removed + added - sum;
fn = max(fn, newfn);
l++;
}
return (int)fn;
}
};
Java Rotate Function LeetCode Solution
public int maxRotateFunction(int[] A) {
if(A.length == 0){
return 0;
}
int sum =0, iteration = 0, len = A.length;
for(int i=0; i<len; i++){
sum += A[i];
iteration += (A[i] * i);
}
int max = iteration;
for(int j=1; j<len; j++){
// for next iteration lets remove one entry value of each entry and the prev 0 * k
iteration = iteration - sum + A[j-1]*len;
max = Math.max(max, iteration);
}
return max;
}
Python 3 Rotate Function LeetCode Solution
class Solution(object):
def maxRotateFunction(self, A):
r = curr = sum(i * j for i,j in enumerate(A))
s = sum(A)
k = len(A)
while A:
curr += s - A.pop() * k
r = max(curr, r)
return r
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1