当前位置:网站首页>[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
边栏推荐
- Instructions for Jerry's reset IO maintenance level [chapter]
- In the context of Internet plus, how can enterprises innovate customer service?
- 稳定币是让公链加速死亡或者取代BTC的超级机会?
- Unity combines itextsharp to generate PDF preparation DLL
- Find number (DFS)
- W801 / w800 WiFi socket development (I) - UDP
- . net unit test Part 1: common Net unit test framework?
- Jerry's AI server [chapter]
- Full Permutation (DFS and next_permutation solution)
- The most understandable life cycle of dependency injection
猜你喜欢
About how to import C4d animation into lumion
无关联进程间通信 —— 命名管道的创建和使用
FL studio20. 8 the latest Chinese version installation and download graphic tutorial
When should I write unit tests? What is TDD?
教程】如何用GCC“零汇编”白嫖MDK
W801/W800-wifi-socket开发(一)-UDP
Error in face detection and signature of Tencent cloud interface
[经验教程]支付宝余额自动转入余额宝怎么设置关闭取消支付宝余额自动转入余额宝?
彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?
2022 crane driver (limited to bridge crane) examination question bank and online simulation examination
随机推荐
keil mdk中文乱码,两种解决方法,字体不再难看
Leetcode 112 Total path (2022.04.22)
无关联进程间通信 —— 命名管道的创建和使用
Soatest preliminary understanding
[经验教程]支付宝余额自动转入余额宝怎么设置关闭取消支付宝余额自动转入余额宝?
王子救公主(DFS)
2022 low voltage electrician examination questions and answers
How can e-procurement become a value-added function in the supply chain?
Android sqliteopenhelper data table structure upgrade
Self taught programming, don't read theory books foolishly, programmer: it's all left over by others
Server 2019 the available memory of the server is half of the actual memory
Level 4 city area table xlsx, SQL file, domestic, provincial, county, street, township level 4 address in China (name, linkage ID, level, end level or not (1-yes))
Redis implements distributed locks
Counting garlic guest: the solution of the equation
什么时候应该编写单元测试?什么是TDD?
What code needs unit testing?
Chris LATTNER, father of llvm: the golden age of compilers
[蓝桥杯][2019年第十届真题]外卖店优先级
2022第六季完美童模 IPA國民賽領跑元宇宙賽道
leetcode771. Gemstones and stones