Java sort solution


  • 0
    D
    public 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 p1 = 0;
            int p2 = 0;
            int content = 0;
            while (p1 < g.length && p2 < s.length) {
                if (g[p1] <= s[p2]) {
                    p1++;
                    p2++;
                    content++;
                } else {
                    p2++;
                }
            }
            return content;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.