当前位置:网站首页>[Leetcode每日一题]396. 旋转函数
[Leetcode每日一题]396. 旋转函数
2022-04-23 01:45:00 【Puppet__】
题目
给定一个长度为 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 位 整数。
示例 1:
输入: nums = [4,3,2,6]
输出: 26
解释:
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
所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。
示例 2:
输入: nums = [100]
输出: 0
提示:
n == nums.length
1 <= n <= 105
-100 <= nums[i] <= 100
代码
package dayLeetCode;
public class dayleetcode396 {
/** * * 数学公式 经典的数学问题,看到前后两项仅在常数上发生变化,则能通过计算找到前两者的关系 * F(0) = 0 * nums[0] + 1 * nums[1] + ... + (n - 1) * nums[n - 1] * F(1) = 1 * nums[0] + 2 * nums[1] + ... + (n - 1) * nums[n - 2] + 0 * nums[n - 1] * = (nums[0] + nums[1] + ... + nums[n-1] - nums[n-1]) + (0 * nums[0] + 1 * nums[1] + ... +(n - 1) * nums[n -1]) -(n -1)*nums[n-1] * = sum + F(0) - n * nums[n-1] 其中sum为数组nums的元素之和 * F(2) = 2 * nums[0] + ... + 0*nums[n-2] + 1*nums[n-1] * = sum + F(1) - n * nums[n-2] * * 所以F(t) = f(t-1)+ sum - n*nums[n-t] * @param nums * @return */
public int maxRotateFunction(int[] nums) {
int sum = 0;
for (int n : nums){
sum += n;
}
int[] dp = new int[nums.length];
for (int i = 0; i < nums.length; i++){
dp[0] += (i * nums[i]);
}
int ans = dp[0];
for (int i = 1; i < nums.length; i++){
dp[i] = dp[i - 1] + sum - nums.length * nums[nums.length - i];
ans = Math.max(dp[i], ans);
}
return ans;
}
public static void main(String[] args) {
dayleetcode396 obj = new dayleetcode396();
int[] nums = {
4, 3, 2, 6};
System.out.println(obj.maxRotateFunction(nums));
}
}
版权声明
本文为[Puppet__]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Puppet__/article/details/124348641
边栏推荐
- Sqlserver data transfer to MySQL
- 《维C中国》乡村助农暖人心第三站嘉宝果农场
- mb_ substr()、mb_ Strpos() function (story)
- keil mdk中文乱码,两种解决方法,字体不再难看
- Digital collection platform settled in digital collection platform to develop SaaS platform of digital collection
- Qingyan environment and Shenzhen Stock Exchange listing: annual revenue of 180 million and market value of 4.1 billion
- PID精讲
- ESP32使用freeRTOS的消息队列
- 稳定币是让公链加速死亡或者取代BTC的超级机会?
- Unity combines itextsharp to generate PDF preparation DLL
猜你喜欢

Oracle database query lock table SQL script and delete lock information script (necessary for database development ETL and DBA)

Abused "architect"!

第六章 使用 matplotlib 绘制热力图

彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?

Digital collection platform settled in digital collection platform to develop SaaS platform of digital collection

Question bank and online simulation examination for safety management personnel of hazardous chemical business units in 2022

Encrypted compressed backup bat script

Detonate the bomb (DFS)

Problem solving: dpkg DEB: error: package name has characters that are't lowercase alphanums or '- +‘

安装mysql出问题求解决
随机推荐
Self taught programming, don't read theory books foolishly, programmer: it's all left over by others
Question bank and online simulation examination for safety management personnel of hazardous chemical business units in 2022
安装mysql出问题求解决
J-Link RTT使用
npm——配置淘宝镜像
Unrelated interprocess communication -- creation and use of named pipes
计蒜客(踏青)(染色块问题的DFS和BFS解法)
Is the stable currency a super opportunity to accelerate the death of the public chain or replace BTC?
Use yolov4 on colab
K zeros after leetcode factorial function
哪些代码需要做单元测试?
Qingyan environment and Shenzhen Stock Exchange listing: annual revenue of 180 million and market value of 4.1 billion
Chapter 6 uses Matplotlib to draw thermodynamic diagram
How can e-procurement become a value-added function in the supply chain?
Jerry's factors that usually affect CPU performance test results are: [article]
Leetcode-阶乘函数后 K 个零
Problem solving: dpkg DEB: error: package name has characters that are't lowercase alphanums or '- +‘
Jerry's CPU performance test [chapter]
DFS parity pruning
Jisuan Hakka spectrum (DFS for direct post algebra)