Algorithm/src/main/java/greedy/FindContentChildren.java
2025-04-04 12:05:49 +08:00

35 lines
1.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package greedy;
import java.util.Arrays;
/**
* 题目: 455. 分发饼干 (maxProfit)
* 描述:给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
* 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
* 返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
* 示例 1
输入: g = [1,2,3], s = [1,1]
输出: 1
解释:你有三个孩子和两块小饼干3 个孩子的胃口值分别是1,2,3。
虽然你有两块小饼干,由于他们的尺寸都是 1你只能让胃口值是 1 的孩子满足。
所以你应该输出 1。
* 链接https://leetcode.cn/problems/assign-cookies/
*/
public class FindContentChildren {
public int findContentChildren(int[] g, int[] s) {
int i=0,j=0,cnt=0;
Arrays.sort(g);
Arrays.sort(s);
while (i<g.length&&j<s.length){
if(g[i]<=s[j]) {
cnt++;
i++;
}
j++;
}
return cnt;
}
}