当前位置:网站首页>396. Rotate Function
396. Rotate Function
2022-04-23 07: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.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-function
Chinese version
给定一个长度为 n 的整数数组 nums 。
假设 arrk 是数组 nums 顺时针旋转 k 个位置后的数组,我们定义 nums 的 旋转函数 F 为:
F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1]
返回 F(0), F(1), ..., F(n-1)中的最大值 。
生成的测试用例让答案符合 32 位 整数。
来源:力扣(LeetCode)
链接: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://blog.csdn.net/weixin_38396940/article/details/124347191
边栏推荐
猜你喜欢
The simple problem of leetcode is to calculate the numerical sum of strings
dried food! Point based: differentiable Poisson solver
[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP
一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载
一篇文章看懂变量提升(hoisting)
【解释】get ORA-12838: cannot read/modify an object after modifying it in parallel
Ribbon start process
LeetCode简单题之三除数
【Appium】测试时遇到手机内嵌H5页面的切换问题
Principle of sentinel integrating Nacos to update data dynamically
随机推荐
以下程序实现从字符串str中删除第i个字符开始的连续n个字
thinkphp6+jwt 实现登录验证
惨了,搞坏了领导的机密文件,吐血分享备份文件的代码技巧
編譯原理題-帶答案
Ignis公链的NFT生态发展:Unicorn.art的捐赠开发之路
Jetson Xavier NX(3)Bazel Mediapipe 安装
Brief description of CPU
Ubuntu安装Mysql并查询平均成绩
Ribbon start process
LeetCode简单题之三除数
C outputs a two-dimensional array with the following characteristics.
有意思的js 代码
Samsung, March to the west again
Vowel substring in statistical string of leetcode simple problem
Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
Rearranging log files for leetcode simple question
PHP high precision computing
Implementation of promise all
Usage of databinding
Alibaba sentinel learning QA