java greedy solution


  • 0
    P

    //贪婪算法,总而言之就是先从饼干里选最大的,然后给最能吃的小孩。如果喂不饱这个最能吃的,就看看能不能喂饱第二能吃的...

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

Log in to reply
 

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