Assign Cookies Solutions in Java
Number 455
Difficulty Easy
Acceptance 49.9%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/assign-cookies/description//// Author : liuyubobobo/// Time : 2017-11-19import java.util.Arrays;/// Greedy Algorithm/// Serve most greedy children first/// Time Complexity: O(nlogn)/// Space Complexity: O(1)public class Solution {public int findContentChildren(int[] g, int[] s) {Arrays.sort(g);Arrays.sort(s);int gi = g.length - 1, si = s.length - 1;int res = 0;while(gi >= 0 && si >= 0){if(s[si] >= g[gi]){res ++;si --;}gi --;}return res;}public static void main(String[] args) {int g1[] = {1, 2, 3};int s1[] = {1, 1};System.out.println((new Solution()).findContentChildren(g1, s1));int g2[] = {1, 2};int s2[] = {1, 2, 3};System.out.println((new Solution()).findContentChildren(g2, s2));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/assign-cookies/description//// Author : liuyubobobo/// Time : 2018-01-17import java.util.Arrays;/// Greedy Algorithm/// Serve least greedy children first/// Time Complexity: O(nlogn)/// Space Complexity: O(1)public class Solution2 {public int findContentChildren(int[] g, int[] s) {Arrays.sort(g);Arrays.sort(s);int gi = 0, si = 0;int res = 0;while(gi < g.length && si < s.length){if(s[si] >= g[gi]){res ++;gi ++;}si ++;}return res;}public static void main(String[] args) {int g1[] = {1, 2, 3};int s1[] = {1, 1};System.out.println((new Solution2()).findContentChildren(g1, s1));int g2[] = {1, 2};int s2[] = {1, 2, 3};System.out.println((new Solution2()).findContentChildren(g2, s2));}}