LeetCode 135 Candy(贪心算法)】的更多相关文章

135. Candy 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 mo…
1. 前言 由于后面还有很多题型要写,贪心算法目前可能就到此为止了,上一篇博客的地址为 LeetCode解题记录(贪心算法)(一) 下面正式开始我们的刷题之旅 2. 贪心 763. 划分字母区间(中等) 题目链接 思路 想切割,要有首尾两个指针,确定了结尾指针,就能确定下一个切割的开始指针. 遍历字符串,如果已扫描部分的所有字符,都只出现在已扫描的范围内,即可做切割. 注意 : 贪心的思想为,只要是扫描过的字符,都出现在我扫描的范围之类,我就切割,不去考虑其他的条件,这样能保证切割的数量最多 代…
贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配   17.8% 困难 45 跳跃游戏 II   25.5% 困难 55 跳跃游戏   30.6% 中等 122 买卖股票的最佳时机 II C#LeetCode刷题之#122-买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 48.8% 简单 134 加油站   40.0% 中等 135 分发糖果   33.0% 困难 316 去除重复字母   21.6% 困难 321 拼接最大数   20…
1. 前言 目前得到一本不错的算法书籍,页数不多,挺符合我的需要,于是正好借这个机会来好好的系统的刷一下算法题,一来呢,是可以给部分同学提供解题思路,和一些自己的思考,二来呢,我也可以在需要复习的时候,通过博客来回顾自己,废话不多说,开始! 目前的规划 2. 算法解释 顾名思义,贪心算法或贪心思想采用贪心的策略,保证每次操作都是局部最优的,从而使最后得到的结果是全局最优的. 举一个最简单的例子:小明和小王喜欢吃苹果,小明可以吃五个,小王可以吃三个.已知苹果园里有吃不完的苹果,求小明和小王一共最多…
原文:http://www.cnblogs.com/AndyJee/p/4483043.html 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…
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…
原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果,就补上,如果多分了糖果,就减掉 究竟补多少或减多少,这很容易计算,不啰嗦了. 时间复杂度:O(n) 代码: int candy(vector<int> &ratings) { int n = ratings.size(); ; ; ; ; i < n; i++) { ]) { pr…
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…
原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res; int len = ratings.size(); for (int i = 0; i < len; i++) { res.push_back(1); } for (int j = 0; j < len; j++) { if (j > 0 && ratings[j] &g…
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one sha…