leetcode第十八周解题总结-贪心算法
来源:互联网 发布:vc60怎么编写c语言 编辑:程序博客网 时间:2024/06/05 21:03
455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
题意解析: 典型的贪心问题,分曲奇,每个孩子至少分到一块,但是每个孩子都有贪心因子g,是他们满足的最小值,要求尽量满足更多的孩子。
解题思路:要满足更多的孩子,优先考虑满足贪心因子最小的孩子即可。
class Solution {public: int findContentChildren(vector<int>& g, vector<int>& s) { int n = g.size(); int m = s.size(); sort(g.begin(),g.end()); sort(s.begin(),s.end()); int counter = 0; for(int i=0,j=0; i <n&&j < m; j++) { if(g[i] <= s[j]) { counter ++; i ++; } } return counter; }};
55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
题意解析: 跳格子游戏,每个格子上的数字代表了在该位置能跳的最远距离。
解题思路:这道题也是贪心算法,记录当前能走到的位置,然后在范围内往前走,不断更新能到达的位置,最后判断是否已经到达结尾。
class Solution {public: bool canJump(vector<int>& nums) { int i = 0; for(int reach = 0; i <= reach&& i <nums.size(); i++){ reach = max(nums[i] + i, reach); } return (i==nums.size()); }};
- leetcode第十八周解题总结-贪心算法
- leetcode-第十八周
- 第十八周leetcode题
- 第十八周总结
- leetcode做题总结,贪心算法I
- leetcode第七周解题总结
- leetcode第八周解题总结
- leetcode第十周解题总结
- leetcode第十七周解题总结
- 【贪心】贪心算法总结
- 第十八周OJ-Q152解题方法
- 算法第十八周作业01
- leetcode第六周解题总结(98)
- leetcode第十二周解题总结--动态规划
- leetcode第十六周解题总结--图
- LeetCode解题思路总结
- leetcode之贪心算法
- leetcode 贪心算法
- 正向代理、反向代理总结
- 采购单价PB00价税分离实际操作之一
- Android的动画移动后点击事件失效问题
- java语言基础(80)——递归获取指定目录下所有的文件
- javascript原型和继承
- leetcode第十八周解题总结-贪心算法
- 概念
- 性能测试工具JMeter使用指南
- 更详细的天气预报接口
- ActiveMQ消息传送机制以及ACK机制详解
- map的简单模拟实现
- websphere8.5配置db2数据源
- LINQ系列:LINQ to ADO.NET概述
- HDU 4734 数位DP 解题报告