396. Rotate Function

Descption of problem

You are given an integer array nums of length n.

Assume arrk to be an array obtained by rotating nums by k positions clock-wise. We define the rotation function F on nums as follow:

F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].
Return the maximum value of F(0), F(1), ..., F(n-1).

The test cases are generated so that the answer fits in a 32-bit integer.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-function

Chinese version

给定一个长度为 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 位 整数。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-function

Solution_1

  1. generate the array by rotating the nus k positions
  2. got all the summarization of F
  3. evaluate all the sum values and got its biggest value

codes like following (there are flaws in here.)

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
private:
    int max_sum = INT_MIN; // in case the cur_sum
public:
    vector<int> rotate_k_vector(int k, vector<int> nums) {
        vector<int> ans;
        // initialize ans by nums 
        ans = nums;
        int n = ans.size();
        if (n == 0) return ans;
        if (k == 0) return ans;
        if (k > n) k = k % n;
        reverse(ans.begin(), ans.begin() + n - k);
        reverse(ans.begin() + n - k, ans.end());
        reverse(ans.begin(), ans.end());
        return ans;
    }
     int maxRotateFunction(vector<int>& nums) {
         int n = nums.size();
         int cur_sum = 0;

         for (int i = 0; i < n; i++) {
             vector<int> ans = rotate_k_vector(i, nums);
                for (int j = 0; j < n; j++) {
                    cur_sum += ans[j] * j;
                }
                max_sum = max(max_sum, cur_sum);
                cur_sum = 0;
         }
            return max_sum;
    }
};
int main() 
{
    Solution s;
    vector<int> nums{4, 3, 2, 6};
    cout << s.maxRotateFunction(nums) << endl;
    return 0;
}

The flaw is following:

在这里插入图片描述

solution_2

在这里插入图片描述

Intuition

F ( 0 ) = 0 × n u m s [ 0 ] + 1 × n u m s [ 1 ] + ⋯ + ( n − 1 ) × n u m s [ n − 1 ] F(0) = 0 \times nums[0] +1 \times nums[1] + \cdots + (n-1)\times nums[n-1] F(0)=0×nums[0]+1×nums[1]++(n1)×nums[n1]
F ( 1 ) = 1 × n u m s [ 0 ] + 2 × n u m s [ 1 ] + ⋯ + ( n % n ) × n u m s [ n − 1 ] F(1) = 1 \times nums[0] +2 \times nums[1] + \cdots + (n\%n)\times nums[n-1] F(1)=1×nums[0]+2×nums[1]++(n%n)×nums[n1]
F ( 1 ) − F ( 0 ) = n u m s [ 0 ] + n u m s [ 1 ] + ⋯ + n u m s [ n − 2 ] − ( n − 1 ) × n u m s [ n − 1 ] F(1) - F(0) = nums[0] +nums[1] + \cdots + nums[n-2] - (n-1)\times nums[n-1] F(1)F(0)=nums[0]+nums[1]++nums[n2](n1)×nums[n1]
sort it out
F ( 1 ) − F ( 0 ) = n u m S u m − n u m s [ n − 1 ] − ( n − 1 ) × n u m s [ n − 1 ] F(1) - F(0) = numSum -nums[n-1] - (n-1)\times nums[n-1] F(1)F(0)=numSumnums[n1](n1)×nums[n1]
F ( 1 ) = F ( 0 ) + n u m S u m + n × n u m s [ n − 1 ] F(1) =F(0) + numSum + n\times nums[n-1] F(1)=F(0)+numSum+n×nums[n1]
abstract the 1 to a
F ( k ) = F ( k − 1 ) + n u m S u m + n × n u m s [ n − k ] F(k) =F(k-1) + numSum + n\times nums[n-k] F(k)=F(k1)+numSum+n×nums[nk]

The codes for this

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
public:
    int maxRotateFunction(vector<int>& nums) {
        int sum = 0;
        int len = nums.size();
        int max_sum = INT_MIN;
        int f = 0;
        for (int i = 0; i < len; i++) {
            sum += nums[i];
            f += i * nums[i];
        }
        max_sum = f;
        for (int i = len - 1; i >= 0; i--) {
            f = f + sum - len * nums[i];
            max_sum = max(max_sum, f);
        }
        return max_sum;
    }
};
int main() 
{
    Solution s;
    vector<int> nums{4, 3, 2, 6};
    cout << s.maxRotateFunction(nums) << endl;
    return 0;
}

solution_3

intuition
view the 0 , 1 , ⋯   , n − 1 0, 1, \cdots, n-1 0,1,,n1 as the transfer variable

The results( O ( n 2 ) O(n^2) O(n2)):

在这里插入图片描述

The codes :

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
public:
    int maxRotateFunction(vector<int>& nums) {
        int cur_sum = 0;
        // set max_sum the minimum possible value
        int max_sum = INT_MIN;
        int n = nums.size();
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cur_sum += nums[j] * ((j + cnt) % n);
            }
            max_sum = max(max_sum, cur_sum);    
            cur_sum = 0;
            cnt++;
        }
        return max_sum;
    }
};
int main() 
{
    Solution s;
    vector<int> nums{4, 3, 2, 6};
    cout << s.maxRotateFunction(nums) << endl;
    return 0;
}
好的,您需要让这个代码段能够正常暂停,我来帮您修改一下。根据您提供的代码,我发现其中缺少了音频对象的创建和设置,我将在修改中加入这部分内容。下面是修改后的代码: ``` Page({ data: { isPlaying: false, animation: {}, audio: null }, onLoad: function () { // 初始化帧动画 const animation = wx.createAnimation({ duration: 1000, timingFunction: 'linear', }) this.animation = animation // 初始化音频对象 const audio = wx.createInnerAudioContext() audio.src = 'your-audio-source' // 填写你的音频源地址 this.setData({audio}) // 监听音频播放结束事件 audio.onEnded(() => { this.setData({ isPlaying: false, animation: animation.rotate(0).export() }) }) }, togglePlay: function () { const isPlaying = this.data.isPlaying const animation = this.animation const audio = this.data.audio if (isPlaying) { // 暂停播放 audio.pause() this.setData({ isPlaying: false, animation: animation.rotate(0).export() }) } else { // 开始播放 audio.play() this.setData({ isPlaying: true, animation: animation.rotate(360).export() }) } }, handleListButtonClick: function () { // 处理歌曲列表按钮点击事件 console.log('歌曲列表按钮点击') }, handlePlayPauseButtonClick: function () { // 处理播放/暂停按钮点击事件 this.togglePlay() }, handleNextButtonClick: function () { // 处理下一首按钮点击事件 console.log('下一首按钮点击') }, handlePrevButtonClick: function () { // 处理上一首按钮点击事件 console.log('上一首按钮点击') } }) ``` 在修改后的代码中,我加入了音频对象的创建和设置,您只需要将 'your-audio-source' 替换为您自己的音频源地址即可。我还在onLoad函数中监听了音频播放结束事件,当音频播放结束时,将isPlaying状态设置为false,同时将动画对象恢复到初始状态。在togglePlay函数中,我修改了播放和暂停的逻辑,使用audio.play()和audio.pause()方法来控制音频的播放和暂停,并在数据更新时,将动画对象的状态也更新到界面中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值