LeetCode 135. Candy (O(n)時間 O(n)和O(1)空間的兩種實現),leetcodecandy

來源:互聯網
上載者:User

LeetCode 135. Candy (O(n)時間 O(n)和O(1)空間的兩種實現),leetcodecandy

貪心演算法,從局部最優推廣成全域最優。

這裡介紹O(n)時間 O(n)和O(1)空間的兩種實現方法。


O(n)時間 O(n)空間實現,參考了cnblog, 1957的解法

建立candy數組,初始化為1. 用pre_child_candy記錄前一個孩子拿到的糖果數

1. 從左往右遍曆

1) 如果ratings[i] > ratings[i-1], 那麼candy[i] = ++ pre_child_candy, 亦即這個孩子比前一個孩子多拿一個糖果

2) 反之,candy[i] = pre_child_candy = 1. (注意到我們已經將candy全部初始化為1了)

2. 從右往左遍曆,pre_child_candy初始化為1, 做類似的操作

1) 如果ratings[i-1] > ratings[i], 那麼candy[i] = max(++ pre_child_candy, candy[i]), 注意到這裡採用max函數是因為此時的candy同時應該滿足1中從左往右遍曆的約束。

2) 反之,candy[i] = pre_child_candy = 1. 

這樣我們最低限度地實現了題目的約束,每個較高分的孩子所得的糖果都比其相鄰的孩子所得的糖果多,從局部最優推廣到了全域最優。

代碼:

class Solution {public:    int candy(vector<int> &ratings)     {    vector<int> candy(ratings.size(), 1);        for (int pre_child_candy=1, i=1; i < ratings.size(); ++ i)    {    if (ratings[i] > ratings[i-1])    {    candy[i] = ++ pre_child_candy;    } else    {    pre_child_candy = 1;    }    }    for (int pre_child_candy=1, i=static_cast<int>(ratings.size())-2; i >= 0; -- i)    {    if (ratings[i] > ratings[i+1])    {    candy[i] = max(++ pre_child_candy, candy[i]);    } else    {    pre_child_candy = 1;    }    }    return accumulate(candy.begin(), candy.end(), 0);    }};



O(n)時間 O(1)空間實現,參考了cnblog, Felix的部落格,但不同的是,在此對遞增、遞減、相等三種情況進行了分類討論

tot: 糖果總數,初始化為1

dec_length: 遞減序列的長度,初始化為0

candy_before_dec: 給出現遞減序列前的那個孩子發出的糖果,初始化為1

pre_child_candy: 給前一個孩子發出的糖果,初始化為1


1. 如果僅考慮遞增序列

亦即恒有 ratings[i] > ratings[i - 1]

從下標i = 1開始訪問,那麼只需要在每次迭代中,執行tot += ++ pre_child_candy即可。


2. 考慮含有遞減子序列的ratings[] = {1, 4, 3, 2, 1}; 正確的糖果數應該為1, 4, 3, 2, 1

i = 1, ratings[1] > ratings[0],

tot += ++ pre_child_candy; //第二個孩子只分到了2個糖果,先別著急,後面會“補償”他pre_child_candy, candy_before_dec都指向第二個孩子,此時它們也都等於2. 也就是
i = 2, ratings[2] < ratings[1] (ratings[i] < ratings[i - 1])

我們認為這是遞減序列的開始,++ dec_length; (dec_length等於1了).tot += dec_length; // tot += 1pre_child_candy = 1; // 更新它,防止潛在的遞增序列需要這個變數
i = 3, ratings[3] <ratings[2]
++ dec_length; (dec_length == 2 now)tot += dec_length; // tot+=2pre_child_candy = 1; // 更新它,防止潛在的遞增序列需要這個變數// 這時候發現,dec_lenght >= pre_child_candy, // 遞減序列的長度已經大於“給出現遞減序列前的那個孩子發出的糖果”,我們就補償給第二個孩子1個糖果,++ tot

i = 4, ratings[4] < ratings[3] 

++ dec_length; (dec_length == 3 now)tot += dec_length; // tot+=3pre_child_candy = 1; // 更新它,防止潛在的遞增序列需要這個變數// 這時候發現,dec_lenght >= pre_child_candy, // 遞減序列的長度已經大於“給出現遞減序列前的那個孩子發出的糖果”,我們就補償給第二個孩子1個糖果,++ tot
我們最後就發現,我們成功的給第二個孩子了4個糖果,而i=2,3,4的情況,則分別給tot增加了1,2,3, 這剛好是實際情況的逆序。

所以,我們處理好了含有遞減子序列的情況


3. 考慮相鄰孩子ratings相等的情況,可以考慮ratings[] = {1, 4, 4, 3, 2, 1}

從邏輯上,這個孩子暫時只需要給1個糖果,同時將遞減序列長度歸零,candy_before_dec和pre_child_candy置1即可。


代碼:

class Solution {public:    int candy(vector<int> &ratings)     {    int tot = 1, dec_length = 0, candy_before_dec = 1, pre_child_candy = 1;    for (int i = 1; i < ratings.size(); ++ i)    {    if (ratings[i] > ratings[i-1])    {    tot += ++ pre_child_candy;    dec_length = 0;    candy_before_dec = pre_child_candy;    } else if (ratings[i] == ratings[i-1])    {    ++ tot;        dec_length = 0;        candy_before_dec = pre_child_candy = 1;    } else // <    {    tot += ((++dec_length>=candy_before_dec)? 1: 0);    tot += dec_length;    pre_child_candy = 1;    }    }    return tot;    }};


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.