simple java greedy algorithm


  • 1
    2
    public class Solution {
        public int findContentChildren(int[] g, int[] s) {
            int ret = 0;
            int i = 0;
            int j = 0;
            Arrays.sort(g);
            Arrays.sort(s);
            while (i < g.length && j < s.length) {
                if (s[j] >= g[i]) {
                    ret++;
                    i++;
                    j++;
                }
                else {
                    j++;
                }
            }
            return ret;
        }
    }
    

  • 0
    A

    while another solution has one less variable, for me this one is easier to read because it has another.


Log in to reply
 

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