当前位置:网站首页>Daily question - leetcode396 - rotation function - recursion

Daily question - leetcode396 - rotation function - recursion

2022-04-23 14:48:00 Li Fan, hurry up

Original link
 Insert picture description here

Note:

The official solution is very good to understand
List f0 and f1 You can see the recursive relationship , Then the code can be implemented

The code is as follows :

class Solution {
    
public:
    int maxRotateFunction(vector<int>& nums) {
    
        int f = 0, sum = 0;
        for(int i = 0; i < nums.size(); i ++)
            sum += nums[i];
        for(int i = 0; i < nums.size(); i ++)
            f += i * nums[i];
        
        int res = f;
        for(int i = nums.size() - 1; i > 0; i --){
    
            f = f + sum - nums.size() * nums[i];
            res = max(res, f);
        }
        return res;
    }
};

版权声明
本文为[Li Fan, hurry up]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231447599931.html