当前位置:网站首页>Daily question (April 22, 2022) - rotation function
Daily question (April 22, 2022) - rotation function
2022-04-23 02:08:00 【Fill your head with water】
Catalog
396. Rotation function
Title Description :
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(k) = 0 * arrk[0] + 1 * arrk[1] + … + (n - 1) * arrk[n - 1]
// k=0 Clockwise rotation 0 A place arr0=[4,3,2,6] F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25 // k=1 Clockwise rotation 1 A place arr1=[6,4,3,2] F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16 // k=2 Clockwise rotation 2 A place arr2=[2,6,4,3] F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23 // k=3 Clockwise rotation 3 A place arr3=[3,2,6,4] 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 .
Timeout solution :
Ideas :
Use double pointer , One end and one tail , Every rotation , Moving the two pointers to the left is the rotated slice .
func maxRotateFunction(nums []int) int {
length := len(nums)
if length==1 {
return 0
}
// Two hands move one bit to the left in each round
pointHead := 0
pointTail := length - 1
ans := math.MinInt64
for i := 0; i < length; i++ {
f := 0
head := pointHead
for j := 0; j < length; j++ {
f = j*nums[head] + f
head = (head + 1) % length
}
// If the head pointer pushes back , Negative , Just point to the end of the queue
pointHead--
if pointHead < 0 {
pointHead = length - 1
}
pointTail--
ans = int(math.Max(float64(ans),float64(f)))
}
return ans
}
Positive solution :
Ideas :
Observe F(0), …, F(k) The law of is available :
Every time you rotate the array , The coefficient of the last value becomes 0, The coefficients of other values are +1
So define sum Used to store arrays and , andF(k)+sum
That's equal to all the coefficients +1, Then subtract the value after the last change and remove it, that'sF(k+1)
The value of the
By way of example 1 For example :
- sum Is the sum of all the elements in the slice , Every time you add sum, Equivalent to F(k) Every coefficient of is +1
for example :
sum = 4 + 3 + 2 + 6 = 15
F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
F(0)+sum = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6)+ 4 + 3 + 2 + 6
= (1 * 4) + (2 * 3) + (3 * 2) + (4 * 6) = 40
And the law observed above : Every time you rotate the array , The coefficient of the last value becomes 0, The coefficients of other values are +1
Then we observe F(1) Relative to F(0)+sum What's the difference :
F(1) = (1 * 4) + (2 * 3) + (3 * 2)+ (0 * 6)
F(0)+sum = (1 * 4) + (2 * 3) + (3 * 2) + (4 * 6)
You can see that the last value should correspond to 0*6 and F(0)+sum But inside 4*6
So we can figure out :
F(1) = F(0) + sum - The value after the last change = 25 + 15 - 24 = 16
func maxRotateFunction(nums []int) int {
// sum: The sum of all elements in the array
// curSum: F(k) Every time you rotate the array , Plug in F(k) The one from the formula
sum, curSum, ans := 0, 0, 0
for i := 0; i < len(nums); i++ {
// Sum arrays
sum = sum + nums[i]
// seek F(0)
curSum = curSum + i*nums[i]
}
ans = curSum
for i := len(nums) - 1; i > 0; i-- {
// F(K+1) = F(K) + sum - The value after the last change
curSum = curSum + sum - len(nums)*nums[i]
ans = max(ans, curSum)
}
return ans
}
func max(a, b int) int {
if b > a {
return b
}
return a
}
Submit results :
版权声明
本文为[Fill your head with water]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230206063768.html
边栏推荐
- OJ daily practice - Finish
- 89 logistic regression user portrait user response prediction
- Open3d point cloud processing
- Keil MDK Chinese garbled code, two solutions, the font is no longer ugly
- Flink real-time data warehouse project - Design and implementation of DWS layer
- Kubernetes cluster installation based on Kirin SP10 server version
- ThinkPHP kernel development blind box mall source code v2 0 docking easy payment / Alibaba cloud SMS / qiniu cloud storage
- Startup of openstack service
- Echo "new password" |passwd -- stdin user name
- EBS:PO_EMPLOYEE_HIERARCHIES_ALL
猜你喜欢
Dynamic batch processing and static batch processing of unity
Kubernetes cluster installation based on Kirin SP10 server version
005_redis_set集合
Batch multiple files into one hex
Uncover floating-point operations hidden by the ARM compiler
Common formatting problems after word writing
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
Open3d point cloud processing
代理IP可用率是不是等同于代理IP的效率?
世界读书日 | 技术人不要错过的好书(IT前沿技术)
随机推荐
arduino esp8266 网络升级 OTA
Esp32 message queue using FreeRTOS
007_Redis_Jedis连接池
Cc2541 emulator CC debugger tutorial
MySQL C language connection
代理IP可用率是不是等同于代理IP的效率?
Some tips for using proxy IP.
有哪些业务会用到物理服务器?
leetcode:27. 移除元素【count remove小操作】
Flink real-time data warehouse project - Design and implementation of DWS layer
Shardingsphere introduction and sub table usage
What businesses use physical servers?
有哪些常见的代理ip问题?
Startup of openstack service
Realize linear regression with tensorflow (including problems and solutions in the process)
easyswoole环境配置
openstack 服务的启动
Echo "new password" |passwd -- stdin user name
简洁开源的一款导航网站源码
013_基于Session实现短信验证码登录流程分析