assign-cookies

https://leetcode.com/problems/assign-cookies/

用贪心算法即可。

package com.company;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

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

public class Main {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("Hello!");
        Solution solution = new Solution();

        // Your Codec object will be instantiated and called as such:
        int[] g = {1, 2, 3};
        int[] s = {3};
        int ret = solution.findContentChildren(g, s);
        System.out.printf("ret:%d\n", ret);

        System.out.println();

    }

}

 

posted @ 2016-11-17 19:01  blcblc  阅读(234)  评论(0)    收藏  举报