日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php框架 > 框架設計 > leetcode || 135、Candy

leetcode || 135、Candy

來源:程序員人生   發(fā)布時間:2015-06-09 08:33:16 閱讀次數(shù):3485次

problem:

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?

Hide Tags
 Greedy
題意: 給小盆友發(fā)糖果,屬性高的糖果要更多

thinking:

(1)1道簡單的貪心題目,為什么通過率這么低??題目中根本沒提屬性相等時怎樣發(fā)糖果。又是1道不嚴謹?shù)念}目。提交發(fā)現(xiàn),

122的小盆友發(fā)了4個糖果,說明,第3個小盆友發(fā)了1個糖果,換做你是小盆友,你會高興嗎...

(2)撇開相等的情況不說,這道題只要處理好遞減的情況就好了。遞增或亂序好處理些

code:

class Solution{ public: int candy(vector<int> &ratings) { int Total = 0; /// Total candies int length = 0; /// Continuous descending length of rate int nPreCanCnt = 1; /// Previous child's candy count int beforeDenc = nPreCanCnt; if(ratings.begin() != ratings.end()) { Total++; //Counting the first child's candy (1). for(vector<int>::iterator i = ratings.begin()+1; i!= ratings.end(); i++) { if(*i < *(i⑴)) { length++; if(beforeDenc <= length) { Total++; } Total += length; nPreCanCnt = 1; //This step is important, it ensures that once we leave the decending sequence, candy number start from 1 } else { int curCanCnt = 0; if(*i > *(i⑴)) { curCanCnt = (nPreCanCnt + 1); } else { curCanCnt = 1; } Total += curCanCnt; nPreCanCnt = curCanCnt; length = 0; //reset length of decending sequence beforeDenc = curCanCnt; } } } return Total; } };


生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 日韩精品在线观看视频 | 高清不卡一区二区三区 | 免费人成在线观看网站 | 91一区二区 | 久久久久综合 | 国产精品美女久久久久aⅴ国产馆 | 日本精品久久久久久久 | 91久久精品国产91久久 | 亚洲成人在线网站 | 超碰在线网址 | 欧美激情一二三区 | 国产精品网站在线观看 | 福利一区福利二区 | 网曝91综合精品门事件在线 | 中文字幕网站 | 亚洲免费二区 | 成人免费网站在线观看 | 精品国产一区二区三区四区四 | 久久国产精品99久久久久久牛牛 | 国产午夜亚洲精品理论片色戒 | 伊人888 | 三级网站免费观看 | 成人欧美一区二区三区在线湿哒哒 | 久久免费看片 | 欧美日韩精品电影 | 欧美亚洲另类视频 | 成人a在线| 亚洲欧美视频 | 久久精品国产一区二区三区 | 九九在线视频 | 中文字幕国产一区二区 | 激情综合社区 | 亚洲精品在线免费看 | 欧美一区二区精品 | 一区二区不卡 | 国产精品一区二区三区四区视频 | 久久99国产精品久久99大师 | 国产一区亚洲二区 | 99精品免费久久久久久久久 | 成人免费视频网站在线看 | 国产二区精品 |