LeetCode题解之 Assign Cookies】的更多相关文章

1.题目描述 2.问题分析 使用贪心算法. 3 代码 class Solution { public: int findContentChildren(vector<int>& g, vector<int>& s) { ; sort(g.begin(), g.end()); sort(s.begin(), s.end()); vector<int>::iterator its = s.begin(); ; for (int &x : g) { w…
problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度(也就是胃口)和当前cookies的大小分别进行排序,满足一个小朋友则加1:否则比较下一个cookie是否满足小朋友.记住前提是每个小朋友最多只能得到一个cookie.也就是贪婪算法. class Solution { public: int findContentChildren(vector<i…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCode] 题目地址:https://leetcode.com/problems/assign-cookies/ Difficulty: Easy 题目描述 Assume you are an awesome parent and want to give your children some cook…
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…
这是悦乐书的第234次更新,第247篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第101题(顺位题号是455).假设你是一个很棒的父母,并想给你的孩子一些饼干.但是,你应该给每个孩子一个饼干.每个孩子我都有一个贪婪因子gi,这是孩子满意的cookie的最小尺寸;每个cookie j的大小为sj.如果sj> = gi,我们可以将cookie j分配给孩子i,而孩子我将满足.您的目标是最大化满足您孩子的数量并输出.例如: 输入:[1,2,3],[1,1] 输出:1 说…
package Others; import java.util.Arrays; //Question 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…
Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小,把饼干分给小孩,每个小孩只能分一个饼干,问最多能满足多少个小孩. 思路:遍历小孩,为每个小孩遍历饼干 Java实现: public int findContentChildren(int[] g, int[] s) { int ans = 0; Arrays.sort(s); for (int i…
455. Assign Cookies Easy 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 b…
leetcode题解:无重复字符的最长子串 题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: s = "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 思路 构造子串的首尾指针. 判断子串是否重复的方法:利用string的find_first_of方法,传入子串的最后一个字符,当返回的下标非子串的长度,即end-begin,则表明已经重复. 若重复:则判断长度,比当前记录的最…
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有两个节点--左孩子结点与右孩子结点.C实现的二叉树: struct TreeNode { int val; struct TreeNode *left; // left child struct TreeNode *right; // right child }; DFS DFS的思想非常朴素:根据…