洪晨的前端隨記2021年12月13日
阿新 • • 發佈:2021-12-13
假設你是一位很棒的家長,想要給你的孩子們一些小餅乾。但是,每個孩子最多隻能給一塊餅乾。
對每個孩子 i,都有一個胃口值g[i],這是能讓孩子們滿足胃口的餅乾的最小尺寸;並且每塊餅乾 j,都有一個尺寸 s[j]。如果 s[j]>= g[i],我們可以將這個餅乾 j 分配給孩子 i ,這個孩子會得到滿足。你的目標是儘可能滿足越多數量的孩子,並輸出這個最大數值。
來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/assign-cookies
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。
心之所向,素履以往 生如逆旅,一葦以航import java.util.Arrays; class Solution { public int findContentChildren(int[] g, int[] s) { if (g == null || g.length == 0 || s == null || s.length == 0) { return 0; } Arrays.sort(g); Arrays.sort(s); int i = 0, j = 0; int ret = 0; while (i < g.length && j < s.length) { if (g[i] <= s[j]) { ret++; i++; j++; } else { j++; } } return ret; } }