LeetCode_455

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
    评论
题目描述: 给定一个字符串,请将字符串里的字符按照出现的频率降序排列。 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r'和't'都只出现一次。因此'e'必须出现在'r'和't'之前。此外,"eetr"也是一个有效的答案。 示例 2: 输入: "cccaaa" 输出: "cccaaa" 解释: 'c'和'a'都出现三次。此外,"aaaccc"也是有效的答案。注意"cacaca"是不正确的,因为相同的字母必须放在一起。 示例 3: 输入: "Aabb" 输出: "bbAa" 解释: 此外,"bbaA"也是一个有效的答案,但"Aabb"是不正确的。注意'A'和'a'被认为是两种不同的字符。 Java代码如下: ``` import java.util.*; public class Solution { public String frequencySort(String s) { if (s == null || s.length() == 0) { return ""; } Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); map.put(c, map.getOrDefault(c, 0) + 1); } List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); StringBuilder sb = new StringBuilder(); for (Map.Entry<Character, Integer> entry : list) { char c = entry.getKey(); int count = entry.getValue(); for (int i = 0; i < count; i++) { sb.append(c); } } return sb.toString(); } } ``` 解题思路: 首先遍历字符串,使用HashMap记录每个字符出现的次数。然后将HashMap转换为List,并按照出现次数从大到小进行排序。最后遍历排序后的List,将每个字符按照出现次数依次添加到StringBuilder中,并返回StringBuilder的字符串形式。 时间复杂度:O(nlogn),其中n为字符串s的长度。遍历字符串的时间复杂度为O(n),HashMap和List的操作时间复杂度均为O(n),排序时间复杂度为O(nlogn),StringBuilder操作时间复杂度为O(n)。因此总时间复杂度为O(nlogn)。 空间复杂度:O(n),其中n为字符串s的长度。HashMap和List的空间复杂度均为O(n),StringBuilder的空间复杂度也为O(n)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值