396. Rotate Function

Given an array of integers A and let n to be its length.

Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a “rotation function” F on A as follow:

F(k) = 0 * Bk[0] + 1 * Bk[1] + … + (n-1) * Bk[n-1].

Calculate the maximum value of F(0), F(1), …, F(n-1).

Note:
n is guaranteed to be less than 105.

Example:

A = [4, 3, 2, 6]

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

So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.
Subscribe to see which companies asked this question
题目解释:对于一个给定的数组,对数组进行加权求和,权重就是元素所在的索引号,然后再对数组进行顺时针旋转,也就是所有元素向右移动一位,最后一位移到最前面。求输出最大的和是多少。


虽然是一道easy题,但还是没做出来呀,参考了别人的答案,发现貌似有规律,引入了下面的一句话:
http://blog.csdn.net/vipin_pei/article/details/52539260
解题思路:一开始我想到的是用队列存放权重,然后队列元素分别与数组相乘求和,但是后来发现,这种做法简直就是暴力太愚蠢。应该从中寻找规律,我们不妨设想,所有数组元素向右移动一位会和会发生什么变化,结果我们可以看到,除了最后一位元素,所有元素的权重值都加了1,也就是和多加了A[0]+A[1]+…+A[N-2],而对于最后一位元素,被移到最前面,也就是A[n-1]的权重从(n-1)变成了0, 因此和减少了 (n-1)*A[n-1]

所以说,每一次移动的和S(new),和相对于上一次移动的和S(old)的关系如下:

S(new) = S(old) + { S - A[n-1] - (n-1)*A[n-1] } = S(old) + { S - n*A[n-1] }; // S = A[0]+A[2]+A[3]+…+A[n]

因此,每顺时针移动一位,只需要进行一次计算就好。


class Solution {
public:
    int maxRotateFunction(vector<int>& A) {
        int len=A.size();
        int oldsum=0;
        int maxsum;
        int newsum=0;
        int sa=0;
        for(int i=0;i<len;i++)
       { 
           oldsum+=A[i]*i;
           sa+=A[i];
       }
        newsum=maxsum=oldsum;
        for(int i=len-1;i>=0;i--)
       {
           newsum+=sa-len*A[i];
           if(newsum>maxsum)
                maxsum=newsum;
       }
        return maxsum;
    }
};

不为别的只为记录~

好的,您需要让这个代码段能够正常暂停,我来帮您修改一下。根据您提供的代码,我发现其中缺少了音频对象的创建和设置,我将在修改中加入这部分内容。下面是修改后的代码: ``` 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、付费专栏及课程。

余额充值