【leetcode】455. Assign Cookies
problem
solution1:
But, you should give each child at most one cookie.
对小朋友的满意程度(也就是胃口)和当前cookies的大小分别进行排序,满足一个小朋友则加1;否则比较下一个cookie是否满足小朋友。记住前提是每个小朋友最多只能得到一个cookie.也就是贪婪算法。
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
int res = ;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
for(int i=, j=; i<g.size() && j<s.size();)//err.
{
if(g[i]<=s[j]) { res++; j++; i++; }
else j++;
}
return res;
}
};
solution2:简化版本,即满足小朋友的个数即是所求解的,小朋友的坐标即是满足小朋友的个数。
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int i = ;
for(int j=; i<g.size() && j<s.size(); j++)//err.
{
if(g[i]<=s[j]) i++;
}
return i;
}
};
参考
1. Leetcode_455. Assign Cookies;
完
【leetcode】455. Assign Cookies的更多相关文章
- 【LeetCode】455. Assign Cookies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- [leetcode greedy]455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- LeetCode:455. Assign Cookies
package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...
- 455. Assign Cookies - LeetCode
Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- 【题解】Luogu P5071 [Ynoi2015]此时此刻的光辉
众所周知lxl是个毒瘤,Ynoi道道都是神仙题,题面好评 原题传送门 一看这题没有修改操作就知道这是莫队题(我也只会莫队) 我博客里对莫队的简单介绍 一个数N可以分解成\(p_1^{c_1}p_2^{ ...
- es日常维护
1.查看es日志curl -XGET http://10.26.41.60:9200/xdm-logs-2018.08.22?pretty=true 2.删除es日志curl -XDELETE 'ht ...
- 线上问题排查(2)——JDK内置工具
https://www.cnblogs.com/keanuyaoo/p/3253663.html 常用命令目录: jps命令(Java Virtual Machine Process Status T ...
- webpack引入eslint详解
- English trip EM2-LP-4B At School Teacher:Russell
课上内容(Lesson) Where is Loki a student? Loki is in Meten, BaobaoStreet, Chengdu. What is he studying? ...
- 为iframe添加onclick事件
如果页面上有iframe时,鼠标点击在iframe内时,包含iframe的document是不响应任何事件的, 例如: $("#iframe1").click(function() ...
- 牛客网练习赛23 F 托米的游戏
链接:https://www.nowcoder.com/acm/contest/156/F 来源:牛客网 题目描述 题目背景编不下去了 托米有一棵有根树 T, 树根为1,每轮他会在剩下的子树中等概率一 ...
- 【框架】selenium网页端的基本自动化框架(四)
- R语言scale与unscale函数
一.scale函数 R语言base库中自带数据标准化接口scale函数,函数介绍如下 Usage scale(x, center = TRUE, scale = TRUE) Arguments x: ...
- (淘宝无限适配)手机端rem布局详解
从网易与淘宝的font-size思考前端设计稿与工作流 本文结合自己对网易与淘宝移动端首页html元素上的font-size这个属性的思考与学习,讨论html5设计稿尺寸以及前端与设计之间协作流程的问 ...