1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實(shí)戰(zhàn)396:旋轉(zhuǎn)函數(shù)

        共 2755字,需瀏覽 6分鐘

         ·

        2021-10-01 23:50

        算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

        今天和大家聊的問題叫做 旋轉(zhuǎn)函數(shù),我們先來看題面:
        https://leetcode-cn.com/problems/rotate-function/

        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.


        示例

        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

        所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。


        解題


        找出規(guī)律f(k)-f(k-1)=sum-n*A[n-k];其中sum表示數(shù)組A的總的元素和,計(jì)算出數(shù)組A的f(0)的結(jié)果,然后逐漸的求出f(k),保存最大值即可;

        class Solution {
        public:
            int maxRotateFunction(vector<int>& A) {
                long sum_A=0;//數(shù)組A的元素和
                long sum_Ak=0;//在k=0,既不旋轉(zhuǎn)是獲得結(jié)果和
                long n=A.size();//數(shù)組A的元素個(gè)數(shù)
                for(int i=0;i<n;++i){//計(jì)算
                    sum_A+=A[i];
                    sum_Ak+=A[i]*i;
                }
                long max_sumk=sum_Ak;//初始化,最大的值
                for(int i=1;i<n;++i){
                    sum_Ak=sum_A+sum_Ak-n*A[n-i];//使用之前的值,求出當(dāng)前的值
                    max_sumk=max_sumk<sum_Ak?sum_Ak:max_sumk;//更新可能的更大的值
                }
                return max_sumk;//發(fā)那會(huì)結(jié)果
            }
        };


        好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

        上期推文:

        LeetCode1-380題匯總,希望對(duì)你有點(diǎn)幫助!

        LeetCode刷題實(shí)戰(zhàn)381:O(1) 時(shí)間插入、刪除和獲取隨機(jī)元素

        LeetCode刷題實(shí)戰(zhàn)382:鏈表隨機(jī)節(jié)點(diǎn)

        LeetCode刷題實(shí)戰(zhàn)383:贖金信

        LeetCode刷題實(shí)戰(zhàn)384:打亂數(shù)組

        LeetCode刷題實(shí)戰(zhàn)385:迷你語法分析器

        LeetCode刷題實(shí)戰(zhàn)386:字典序排數(shù)
        LeetCode刷題實(shí)戰(zhàn)387:字符串中的第一個(gè)唯一字符
        LeetCode刷題實(shí)戰(zhàn)388:文件的最長絕對(duì)路徑
        LeetCode刷題實(shí)戰(zhàn)389:找不同
        LeetCode刷題實(shí)戰(zhàn)390:消除游戲
        LeetCode刷題實(shí)戰(zhàn)391:完美矩形
        LeetCode刷題實(shí)戰(zhàn)392:判斷子序列
        LeetCode刷題實(shí)戰(zhàn)393:UTF-8 編碼驗(yàn)證
        LeetCode刷題實(shí)戰(zhàn)394:字符串解碼
        LeetCode刷題實(shí)戰(zhàn)395:至少有 K 個(gè)重復(fù)字符的最長子串

        瀏覽 52
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            西西人体44www大胆无码 | 黄色在线免费看 | 中文字幕人妻一区二区三区 | 飘花影院午夜伦片理伦片 | 欧美成人性影院 | 加勒比不卡AV | 穿书女配被强啪h | 国产亲近乱来精品视频 | XX视频| 九一综合色 |