LeetCode:455.Assign Cookies 分配饼干

LeetCode: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.

Note:
You may assume the greed factor is always positive.
You cannot assign more than one cookie to one child.

Example 1:

Input: [1,2,3], [1,1]

Output: 1

Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
You need to output 1.

Example 2:

Input: [1,2], [1,2,3]

Output: 2

Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
You have 3 cookies and their sizes are big enough to gratify all of the children,
You need to output 2.


假设你是一个很棒的父母,并想给你的孩子一些饼干。但是,你应该给每个孩子一个饼干。每个孩子i都有一个贪婪因子gi,这是孩子满意的饼干的最小尺寸;并且每个饼干 j的大小为sj。如果sj> = gi,我们可以将饼干 j分配给子i,而孩子i将满足。您的目标是使满意的孩子数量最大并且输出最大数量。

注意:您可以假设贪婪因子始终为正。您不能为一个孩子分配多个cookie。示例1:输入:[1,2,3],[1,1]输出:1
说明:您有3个孩子和2个cookie。3个孩子的贪婪因子是1,2,3。即使你有2个饼干,因为它们的大小都是1,你只能让孩子的贪婪因子是1的满足。您需要输出1.
示例2:输入:[1,2],[1,2,3]输出:2
说明:您有2个孩子和3个cookie。2个孩子的贪婪因子是1,2。你有3个饼干,它们的大小足以满足所有孩子,你需要输出2。


贪心思想保证每次操作都是局部最优的,并且最后得到的结果是全局最优的。


每个孩子都有一个满足度,每个饼干都有一个大小,自由饼干的大小大于或等于一个孩子的满足度,这个孩子才会获得满足。求解最多可以获得满足的孩子的数量。
import java.util.Arrays;
import java.util.Scanner;

public class AssignCookies {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int gi = sc.nextInt();//gi>=si
		int si = sc.nextInt();
		int aa[] = new int[gi];//greed factor
		int bb[] = new int[si];		

		for(int k=0;k<gi;k++) {
			aa[k]=sc.nextInt();
		}
		for(int k=0;k<si;k++) {
			bb[k]=sc.nextInt();
		}
		
//		int aa[] = {1,2,3};
//		int bb[] = {1,2};//
		
		System.out.println(findContentChildren(aa, bb));
		System.out.println(findContentChildrenTwo(aa, bb));
		
	}

	private static int findContentChildren(int g[],int s[]) {
		Arrays.sort(g);
		Arrays.sort(s);
		int num = 0;
		for(int i = 0,j = 0;i<g.length && j<s.length; ) {
			if(g[i]<=s[i]) {
				num++;
				i++;
				j++;
			}
			else {
				j++;
			}
		}
		return num;
	}
	
	//2.
	private static int findContentChildrenTwo(int g[],int s[]) {
		Arrays.sort(g);
		Arrays.sort(s);
		int i = 0,j = 0;
		while(i<g.length && j<s.length) {
			if(g[i]<=s[i]) {
				i++;
				j++;
			}
		}
		return i;
	}
	

}//end class

3
2
1 2 3
1 2
2
2

3
4
1 2 3
1 2 3 4
3
3

这道题目给了两组array,一组代表孩子的贪婪因子(胃口值),另一组代表饼干的大小。要分发饼干给尽可能多的孩子,并且饼干的大小是可以满足孩子的胃口的。 不能把大的饼干去满足很小胃口的孩子,除非没有选择。尽可能的去把小饼干发给小胃口的孩子。首先把两个array 重新排列,从小到大。然后遍历饼干array,找到可以满足的孩子,发给他曲奇,记住他的index,下次就直接从下一个孩子开始找。
public class Solution 
{
    public int findContentChildren(int[] g, int[] s) 
    {
        int res = 0;
        int index_g = 0;
        Arrays.sort(g);
        Arrays.sort(s);
    
        for(int i=0; i< s.length; i++)
        {
            if(s[i] >= g[index_g])
            {
                res++;
                index_g++;
                
                if(index_g >= g.length)
                    break;
            }
        }
        
        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值