[LeetCode 題解]:Candy

來源:互聯網
上載者:User

標籤:style   blog   http   color   strong   io   

There are N children standing in a line. Each child is assigned a rating value.

You are giving candies to these children subjected to the following requirements:

  • Each child must have at least one candy.
  • Children with a higher rating get more candies than their neighbors.

What is the minimum candies you must give?

 

題目的意思是 有N個孩子排成一排,並給每個孩子分配一個rating值。按照如下規則給孩子們分配糖果:

(1) 每個孩子必須分至少一個糖果。

(2)相鄰的孩子間,具有高rating值的孩子要多得一個糖果。

 

按照題目的意思,我們給定一個序列:

(1)遞增序列

  1 5 7 9

  很容易得到分配的糖果數依次為:1,2,3,4

(2)遞減序列

  8 6 4 2

  也很容易得到分配的糖果數依次為:4,3,2,1

(3)單波形序列

  1 3 5 7 6 4 

  有兩個子序列: 1,3,5,7 以及7,6,4

  對應分配糖果序列:1,2,3,4 以及3,2,1

  在此過程中7,在兩個序列中都出現了,但是在左邊需要分配4顆糖,在右邊則要分配3顆糖,那麼在最終的序列中需要分配多的一端。因此最後的分配序列為:

  1,2,3,4,2,1   sum= 1+2+3+4+2+1 = 13

  7 5 4 3 9 10

  有兩個子序列: 7,5,4,3 以及3,9,10

  分配按照子序列發糖: 4,3,2,1 以及1,2,3

  預設最小的值分配最少的糖,1顆。因此最後的分配序列為:

  4,3,2,1,2,3  sum = 4+3+2+1+2+3 = 15

(4)多波形

  1 2 3 9 8 7 5 2 4 6 5 3 4

  看似無序,但是可以分成多個遞增和遞減序列

  遞增序列:     1 2 3 9 _ _ _ 2 4 6 _ 3 4

  遞減序列:     _ _ _ 9 8 7 5 2 _ 6 5 3 _

  增序列分配:   1 2 3 4 _ _ _ 1 2 3 _ 1 2

  減序列分配:   _ _ _ 5 4 3 2 1 _ 3 2 1 _

  最終的分配結果:  1 2 3 5 4 3 2 1 2 3 2 1 2 

 

經過上述分析,可以看出糖果的分配可以分成兩種序列進行分配,一種是非增序列,另一中則是非減序列

分別定義兩個序列 up 以及down,分別記錄非減序列和非增序列

(1)從頭至尾遍曆一次,找出遞增序列up

  array up initial with all element equals to 1

  for i from ratings.begin to ratings.end

  do

    if   ratings[i] > ratings [i-1] then

      up[i] <- up[i-1] +1

    end if

(2)從尾向頭遍曆一次,找出遞減序列down

  array down initial with all element equals to 1

  for i from ratings.rbegin to ratings.rend

  do

    if   ratings[i] > ratings [i+1] then

      up[i] <- up[i+1] +1

    end if

(3) 比較up 和down 相應位置,選擇較大的值作為最終結果

  sum <- 0

  for i from ratings.begin to ratings.end

  do

    sum <-  sum + max{up[i], down[i]}

  end for

  return sum

 1 class Solution { 2 public: 3     int candy(vector<int> &ratings) { 4         int len = ratings.size(); 5         if(len<=1) return len; 6         int i,tot=0; 7         vector<int> up(len,1); 8         vector<int> down(len,1); 9         for(i=1;i<len;i++)10             if(ratings[i]>ratings[i-1]) up[i] = up[i-1]+1;11         for(i=len-2;i>=0;i--)12             if(ratings[i]>ratings[i+1]) down[i]= down[i+1]+1;13         for(i=0;i<len;i++){14             tot += max(up[i],down[i]);15         }16         return tot;       17     }18 };

轉載請註明出處: http://www.cnblogs.com/double-win/ 謝謝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.