当前位置:网站首页>[leetcode daily question] 396 Rotation function
[leetcode daily question] 396 Rotation function
2022-04-23 01:50:00 【Puppet__】
subject
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 .
Example 1:
Input : nums = [4,3,2,6]
Output : 26
explain :
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
therefore F(0), F(1), F(2), F(3) The maximum value in is F(3) = 26 .
Example 2:
Input : nums = [100]
Output : 0
Tips :
n == nums.length
1 <= n <= 105
-100 <= nums[i] <= 100
Code
package dayLeetCode;
public class dayleetcode396 {
/** * * The mathematical formula Classical mathematical problems , See that the first and second terms change only in constants , Then we can find the relationship between the first two through calculation * 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] among sum Is an array nums The sum of the elements of * F(2) = 2 * nums[0] + ... + 0*nums[n-2] + 1*nums[n-1] * = sum + F(1) - n * nums[n-2] * * therefore 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://yzsam.com/2022/04/202204230144412992.html
边栏推荐
- 腾讯云接口进行人脸检测 和签名出错问题
- Jerry's factors that usually affect CPU performance test results are: [article]
- Leetcode-阶乘函数后 K 个零
- 什么是代理IP池,如何构建?
- 什么是api接口?
- W801/W800/W806唯一ID/CPUID/FLASHID
- What business scenarios will the BGP server be used in?
- mb_ substr()、mb_ Strpos() function (story)
- Performance introduction of the first new version of cdr2022
- J-link v9 使用技巧之虚拟串口功能
猜你喜欢
如何选择一台好的拨号服务器?
代理IP可用率是不是等同于代理IP的效率?
Is the availability of proxy IP equal to the efficiency of proxy IP?
浅析一下隧道代理IP的优缺点。
Leetcode 112 Total path (2022.04.22)
JSP基础知识总结
Redis implements distributed locks
2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
Shardingsphere sub database and sub table
W801 / w800 / w806 unique ID / CPUID / flashid
随机推荐
W801 / w800 WiFi socket development (I) - UDP
UVC camera encapsulation class
2022第六季完美童模 IPA國民賽領跑元宇宙賽道
最长公共子序列(记录路径版)
ESP32蓝牙Bluetooth Controller API介绍
postman里面使用 xdebug 断点调试
[tutorial] how to use GCC "zero assembly" for white whoring MDK
Batch multiple files into one hex
2022.4.22-----leetcode. three hundred and ninety-six
Under the pressure of sales, domestic mobile phones began to reduce prices, but they haven't put down their final face
What is an API interface?
如何“优雅”的测量系统性能
Challenges often faced by client project management
W801/W800-wifi-socket开发(二)-UDP蓝牙控制wifi连接
中金财富是国企吗,开户安全吗
Dimension C China helping farmers in rural areas warms people's hearts the third stop is jiabaoguo farm
What is a boolean type?
哪些代码需要做单元测试?
MySQL basic record
LSF的使用方法总结