Untitled
unknown
plain_text
2 months ago
1.2 kB
2
Indexable
import java.util.Arrays; public class AssignCookies { public static int findContentChildren(int[] g, int[] s) { // Sort both greed factors and cookie sizes Arrays.sort(g); Arrays.sort(s); int child = 0; // Pointer for greed array int cookie = 0; // Pointer for size array int count = 0; // Traverse both arrays while (child < g.length && cookie < s.length) { // If the cookie can satisfy the child's greed if (s[cookie] >= g[child]) { count++; // Increase the count of happy children child++; // Move to the next child } cookie++; // Move to the next cookie } return count; } public static void main(String[] args) { int[] g1 = {1, 2, 3}; // Greed factors of children int[] s1 = {1, 1}; // Sizes of cookies System.out.println(findContentChildren(g1, s1)); // Output: 1 int[] g2 = {1, 2}; // Greed factors of children int[] s2 = {1, 2, 3}; // Sizes of cookies System.out.println(findContentChildren(g2, s2)); // Output: 2 } }
Editor is loading...
Leave a Comment