[Leetcode] 396. Rotate Function 解题报告
来源:互联网 发布:苹果手机数据恢复软件免费版 编辑:程序博客网 时间:2024/05/22 08:24
题目:
Given an array of integers A
and let n to be its length.
Assume Bk
to be an array obtained by rotating the array A
k positions clock-wise, we define a "rotation function" F
on A
as follow:
F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]
.
Calculate the maximum value of F(0), F(1), ..., F(n-1)
.
Note:
n is guaranteed to be less than 105.
Example:
A = [4, 3, 2, 6]F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.
思路:
1、暴力法:就是每个F函数的值都试一遍,最后得到最大值,思路比较直观。这样代码的复杂度就是O(n^2)了,果然无法通过大数据测试。
2、递推法:在网上看到一个非常巧妙的递归方程:F(i) = F(i - 1) + sum(A) - length(A) * A[length(A) - i]。这样就可以由F(i-1)在O(1)的时间内递推得到F(i),将时间复杂度降低到O(n)。可是这个递推方程是怎么得到的呢?自己还是没有理解。
代码:
1、暴力法:
class Solution {public: int maxRotateFunction(vector<int>& A) { if (A.size() == 0) { return 0; } int ans = INT_MIN; for (int i = 0; i < A.size(); ++i) { vector<int> B = rotate(A, i); ans = max(ans, rotateFunction(B)); } return ans; }private: vector<int> rotate(vector<int>& A, int k) { if (A.size() == 0) { return {}; } if (k == 0) { return A; } k = k % A.size(); vector<int> B(A); rotate(B, 0, B.size() - 1); rotate(B, 0, k - 1); rotate(B, k, B.size() - 1); return B; } void rotate(vector<int> &B, int start, int end) { while (start < end) { swap(B[start++], B[end--]); } } int rotateFunction(vector<int>& B) { int ans = 0; for (int i = 0; i < B.size(); ++i) { ans += i * B[i]; } return ans; }};
2、递推法:
class Solution {public: int maxRotateFunction(vector<int>& A) { int length = A.size(), sum_A = 0, sum = 0, ans = INT_MIN; for (int i = 0; i < length; ++i) { sum_A += A[i]; sum += i * A[i]; } ans = sum; for (int i = 1; i < length; ++i) { sum = sum + sum_A - length * A[length - i]; ans = max(ans, sum); } return ans; }};
阅读全文
0 0
- LeetCode 396. Rotate Function 解题报告
- [leetcode] 396. Rotate Function 解题报告
- [Leetcode] 396. Rotate Function 解题报告
- Leetcode 396. Rotate Function 旋转方程 解题报告
- 【LeetCode】Rotate List 解题报告
- LeetCode-Rotate Array-解题报告
- [LeetCode] Rotate Image 解题报告
- [LeetCode] Rotate List 解题报告
- 【Leetcode】396. Rotate Function
- [LeetCode]396. Rotate Function
- LeetCode 396. Rotate Function
- [leetcode]396. Rotate Function
- [LeetCode]--396. Rotate Function
- leetcode 396. Rotate Function
- [LeetCode] 396. Rotate Function
- LeetCode 396. Rotate Function
- leetcode 396. Rotate Function
- 【LeetCode】 396. Rotate Function
- 交换变量
- win7网络打印机重启后脱机的问题
- 公司实施odoo(openerp)Ubuntu14.04安装过程,实施成功!
- react-router 中的history
- 基于vue-cli的vue项目之路由6--重定向4-----path带参重定向
- [Leetcode] 396. Rotate Function 解题报告
- kylin创建cube时的步骤及一些notes
- 服务器上的内容无法复制到本机
- 当你输入一个网址的时候,实际会发生什么
- Ajax data数据格式
- “封装”的简单实现
- MySQL数据库TDSQL架构分析及采用策略扩容流程
- LeetCode--Insert Interval
- Android DiskLruCache完全解析,硬盘缓存的最佳方案