LeetCode题解之 Assign Cookies

1、题目描述

2、问题分析

使用贪心算法。

3 代码

 1 class Solution {
 2 public:
 3
 4     int findContentChildren(vector<int>& g, vector<int>& s) {
 5         int nums = 0;
 6         sort(g.begin(), g.end());
 7         sort(s.begin(), s.end());
 8
 9         vector<int>::iterator its = s.begin();
10         int index = 0;
11        for (int &x : g) {
12           while (index < s.size() && s[index] < x)
13               index++;
14            if (index == s.size())
15                break;
16            else {
17                index++;
18                nums++;
19            }
20        }
21
22         return nums;
23
24     }
25 };

原文地址:https://www.cnblogs.com/wangxiaoyong/p/10546223.html

时间: 2024-08-29 02:19:48

LeetCode题解之 Assign Cookies的相关文章

[leetcode greedy]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

455. Assign Cookies - LeetCode

Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小,把饼干分给小孩,每个小孩只能分一个饼干,问最多能满足多少个小孩. 思路:遍历小孩,为每个小孩遍历饼干 Java实现: public int findContentChildren(int[] g, int[] s) { int ans = 0; Arrays.sort(s); for (int i

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

LeetCode题解

Reverse Words in a String 考虑几个特殊的情况1.若字符窜s="  "2.字符窜s=“a  b  d     e”3.字符窜s=“ a” class Solution { public: void reverseWords(string &s) { int i; int cas=0; string st[100]; s+=' '; for(i=0;i<s.size();i++) { if(i==0 && s[0]==' ') con

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

[LeetCode 题解]: Reverse Nodes in K-Groups

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod