当前位置:网站首页>396. Rotate Function
396. Rotate Function
2022-04-23 08:17:00 【SUNNY_ CHANGQI】
Descption of problem
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.
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/rotate-function
Chinese version
Given a length of n Array of integers for nums .
hypothesis arrk It's an array nums Clockwise rotation k Array after position , We define nums Of Rotation function F by :
F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1]
return F(0), F(1), ..., F(n-1) Maximum of .
The generated test cases make the answers meet the requirements 32 position Integers .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/rotate-function
Solution_1
- generate the array by rotating the nus k positions
- got all the summarization of F
- evaluate all the sum values and got its biggest value
codes like following (there are flaws in here.)
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
private:
int max_sum = INT_MIN; // in case the cur_sum
public:
vector<int> rotate_k_vector(int k, vector<int> nums) {
vector<int> ans;
// initialize ans by nums
ans = nums;
int n = ans.size();
if (n == 0) return ans;
if (k == 0) return ans;
if (k > n) k = k % n;
reverse(ans.begin(), ans.begin() + n - k);
reverse(ans.begin() + n - k, ans.end());
reverse(ans.begin(), ans.end());
return ans;
}
int maxRotateFunction(vector<int>& nums) {
int n = nums.size();
int cur_sum = 0;
for (int i = 0; i < n; i++) {
vector<int> ans = rotate_k_vector(i, nums);
for (int j = 0; j < n; j++) {
cur_sum += ans[j] * j;
}
max_sum = max(max_sum, cur_sum);
cur_sum = 0;
}
return max_sum;
}
};
int main()
{
Solution s;
vector<int> nums{
4, 3, 2, 6};
cout << s.maxRotateFunction(nums) << endl;
return 0;
}
The flaw is following:
solution_2
Intuition
F ( 0 ) = 0 × n u m s [ 0 ] + 1 × n u m s [ 1 ] + ⋯ + ( n − 1 ) × n u m s [ n − 1 ] F(0) = 0 \times nums[0] +1 \times nums[1] + \cdots + (n-1)\times nums[n-1] F(0)=0×nums[0]+1×nums[1]+⋯+(n−1)×nums[n−1]
F ( 1 ) = 1 × n u m s [ 0 ] + 2 × n u m s [ 1 ] + ⋯ + ( n % n ) × n u m s [ n − 1 ] F(1) = 1 \times nums[0] +2 \times nums[1] + \cdots + (n\%n)\times nums[n-1] F(1)=1×nums[0]+2×nums[1]+⋯+(n%n)×nums[n−1]
F ( 1 ) − F ( 0 ) = n u m s [ 0 ] + n u m s [ 1 ] + ⋯ + n u m s [ n − 2 ] − ( n − 1 ) × n u m s [ n − 1 ] F(1) - F(0) = nums[0] +nums[1] + \cdots + nums[n-2] - (n-1)\times nums[n-1] F(1)−F(0)=nums[0]+nums[1]+⋯+nums[n−2]−(n−1)×nums[n−1]
sort it out
F ( 1 ) − F ( 0 ) = n u m S u m − n u m s [ n − 1 ] − ( n − 1 ) × n u m s [ n − 1 ] F(1) - F(0) = numSum -nums[n-1] - (n-1)\times nums[n-1] F(1)−F(0)=numSum−nums[n−1]−(n−1)×nums[n−1]
F ( 1 ) = F ( 0 ) + n u m S u m + n × n u m s [ n − 1 ] F(1) =F(0) + numSum + n\times nums[n-1] F(1)=F(0)+numSum+n×nums[n−1]
abstract the 1 to a
F ( k ) = F ( k − 1 ) + n u m S u m + n × n u m s [ n − k ] F(k) =F(k-1) + numSum + n\times nums[n-k] F(k)=F(k−1)+numSum+n×nums[n−k]
The codes for this
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
public:
int maxRotateFunction(vector<int>& nums) {
int sum = 0;
int len = nums.size();
int max_sum = INT_MIN;
int f = 0;
for (int i = 0; i < len; i++) {
sum += nums[i];
f += i * nums[i];
}
max_sum = f;
for (int i = len - 1; i >= 0; i--) {
f = f + sum - len * nums[i];
max_sum = max(max_sum, f);
}
return max_sum;
}
};
int main()
{
Solution s;
vector<int> nums{
4, 3, 2, 6};
cout << s.maxRotateFunction(nums) << endl;
return 0;
}
solution_3
intuition
view the 0 , 1 , ⋯ , n − 1 0, 1, \cdots, n-1 0,1,⋯,n−1 as the transfer variable
The results( O ( n 2 ) O(n^2) O(n2)):
The codes :
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
public:
int maxRotateFunction(vector<int>& nums) {
int cur_sum = 0;
// set max_sum the minimum possible value
int max_sum = INT_MIN;
int n = nums.size();
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cur_sum += nums[j] * ((j + cnt) % n);
}
max_sum = max(max_sum, cur_sum);
cur_sum = 0;
cnt++;
}
return max_sum;
}
};
int main()
{
Solution s;
vector<int> nums{
4, 3, 2, 6};
cout << s.maxRotateFunction(nums) << endl;
return 0;
}
版权声明
本文为[SUNNY_ CHANGQI]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230717047345.html
边栏推荐
- 如何在SQL Server中导入excel数据,2019版
- DataBinding的使用五
- 【学习】从零开始的音视频开发(9)——NuPlayer
- Reverse linked list exercise
- AAAI 2022 recruit speakers!!
- Compiler des questions de principe - avec des réponses
- Qt利用QtXlsx操作excel文件
- Common regular expressions
- [Effective Go 中文翻译]函数篇
- Install MySQL for Ubuntu and query the average score
猜你喜欢
关于ORB——SLAM运行中关键帧位置越来越近的异常说明
Idea: export Yapi interface using easyyapi plug-in
Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
Qt编译QtXlsx库
The third divisor of leetcode simple question
几种智能机器人室内定位方法对比
Draw a circle quickly in MATLAB (the one that can be drawn directly given the coordinates and radius of the center of the circle)
vslam PPT
一键清理项目下pycharm和Jupyter缓存文件
输入/输出系统
随机推荐
校园转转二手市场源码下载
一键清理项目下pycharm和Jupyter缓存文件
CGM优化血糖监测管理——移宇科技亮相四川省国际医学交流促进会
The third divisor of leetcode simple question
vslam PPT
Compiling principle questions - with answers
nn.Module类的讲解
Canvas learning Chapter 1
监控智能回放是什么,如何使用智能回放查询录像
3C装配中的机械臂运动规划
An article understands variable lifting
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
Compiler des questions de principe - avec des réponses
Depth of binary tree
LeetCode简单题之计算字符串的数字和
输入/输出系统
Why are there 1px problems? How?
PHP generates short links: convert numbers to letters and letters to numbers
华硕笔记本电脑重装系统后不能读取usb,不能上网
剑指offer day24 数学(中等)