leetcode-pp / 91alg-5-daily-check

91 天学算法第五期打卡
55 stars 14 forks source link

【Day 65 】2021-11-13 - 455. 分发饼干 #84

Open azl397985856 opened 2 years ago

azl397985856 commented 2 years ago

455. 分发饼干

入选理由

暂无

题目地址

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

前置知识

暂无

题目描述

假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j ,都有一个尺寸 sj 。如果 sj >= gi ,我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。

注意:

你可以假设胃口值为正。
一个小朋友最多只能拥有一块饼干。

示例 1:

输入: [1,2,3], [1,1]

输出: 1

解释:

你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
所以你应该输出1。

示例 2:

输入: [1,2], [1,2,3]

输出: 2

解释:

你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
你拥有的饼干数量和尺寸都足以让所有孩子满足。
所以你应该输出2.
StefanLeeee commented 2 years ago

思路

贪心

代码

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

复杂度

laurallalala commented 2 years ago

代码

class Solution(object):
    def findContentChildren(self, g, s):
        """
        :type g: List[int]
        :type s: List[int]
        :rtype: int
        """
        g.sort()
        s.sort()
        i, j = 0, 0
        content = 0
        while i < len(g) and j < len(s):
            if g[i] <= s[j]:
                content += 1
                i += 1
                j += 1
            else:
                j += 1
        return content

复杂度

erik7777777 commented 2 years ago
class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int i = 0, j = 0;
        while (i < g.length && j < s.length) {
            if (g[i] <= s[j]) {
                i++;
            }
            j++;
        }
        return i;
    }
}
vincentLW commented 2 years ago
class Solution {
    public int findContentChildren(int[] g, int[] s) {
        int res = 0;
        Arrays.sort(g);
        Arrays.sort(s);
        int i = 0, j = 0;
        while (i < g.length && j < s.length) {
            if (g[i] <= s[j]) {
                res++;
                i++;
                j++;
            } else if (g[i] > s[j]) {
                j++;
            }
        }
        return res;

    }
}

复杂度 时间复杂度:O(nlogn) 空间复杂度:O(1)

Yufanzh commented 2 years ago

Intuition

first sort g and s\ Then use greedy + two pointers to check both cookie and child one by one

Algorithm in python3

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        # first sort two list and then do greedy
        g = sorted(g)
        s = sorted(s)
        gi = 0
        sj = 0
        res = 0
        while gi < len(g) and sj < len(s):
            if s[sj] >= g[gi]:
                #if cookie is big enough, update res and move pointers
                res += 1
                gi += 1
                sj += 1
            else:
                #if cookie is not big enough, need to move pointer for cookie
                sj += 1
        return res

Complexity Analysis

learning-go123 commented 2 years ago

思路

代码

Go Code:

func findContentChildren(g []int, s []int) int {
    sort.Ints(g)
    sort.Ints(s)
    i,j := 0,0
    for i<len(g) && j<len(s){
        if g[i]<=s[j]{
            i++
        }
        j++
    }
    return i
}

复杂度分析

令 n 为数组长度。

falconruo commented 2 years ago

思路: 这道题给了我们一堆cookie,每个cookie的大小不同,还有一群小朋友,每个小朋友的胃口也不同的,问我们当前的cookie最多能满足几个小朋友。 典型的利用贪婪算法的题目,

复杂度分析:

代码(C++):

方法一、贪心,iterate cookies
class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        if (!s.size()) return 0;

        sort(g.begin(), g.end());
        sort(s.begin(), s.end());

        int j = 0;
        for (int i = 0; i < s.size() && j < g.size(); ++i) {
            if(s[i] >= g[j])
                ++j;
        }
        return j;
    }
};
muimi commented 2 years ago

思路

贪心,排序+双指针

代码

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

复杂度

leolisgit commented 2 years ago

题目

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

思路

使用最小的cookie去尽量满足胃口最小的孩子。这样可以满足尽量多的孩子。

代码

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        if (s.length == 0) return 0;
        int count = 0;
        Arrays.sort(g);
        Arrays.sort(s);
        int i = 0;
        int j = 0;

        while (i < g.length && j < s.length) {
            if (g[i] <= s[j]) {
                count++;
                i++;
                j++;
            } else {
                j++;
            }
        }
        return count;
    }
}

复杂度

时间:O(nlogn)
空间:O(1)

zhangzhengNeu commented 2 years ago

first sort g and s Then use greedy + two pointers to check both cookie and child one by one

Algorithm in python3 class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int:

first sort two list and then do greedy

    g = sorted(g)
    s = sorted(s)
    gi = 0
    sj = 0
    res = 0
    while gi < len(g) and sj < len(s):
        if s[sj] >= g[gi]:
            #if cookie is big enough, update res and move pointers
            res += 1
            gi += 1
            sj += 1
        else:
            #if cookie is not big enough, need to move pointer for cookie
            sj += 1
    return res

Complexity Analysis Time complexity: O(NlogN) Space complexity: O(1)

LAGRANGIST commented 2 years ago

class Solution { public: int findContentChildren(vector& g, vector& s) { sort(g.begin(), g.end()); sort(s.begin(), s.end()); // 双指针 int j = 0; // 遇到满足条件的情形, j才移动一步(同时是一个计数器)
for (int i = 0; i < s.size() && j < g.size(); ++i) { if (s[i] >= g[j]) j++; } return j; } };

jiahui-z commented 2 years ago

思路: super straightforward, just sort

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        int result = 0;
        if (s.length == 0) {
            return result;
        }

        Arrays.sort(g);
        Arrays.sort(s);

        int idx = 0;
        for (int i = 0; i < s.length && idx < g.length; i++) {
            if (s[i] >= g[idx]) {
                result++;
                idx++;
            }
        }

        return result;
    }
}

Time Complexity: O(nlogn) Space Complexity: O(1)

chen445 commented 2 years ago

代码

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        child=0
        cookie=0
        while child < len(g) and cookie<len(s):
            if g[child]<=s[cookie]:
                child+=1
            cookie+=1
        return child

复杂度

Time: O(nlogn)

Space: O(1)

jocelinLX commented 2 years ago

代码: class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() i=j=t=0 while i < len(g) and j< len(s): if g[i]<=s[j]: i+=1 j+=1 t+=1 else: j+=1 return t

huzizailushang commented 2 years ago

class Solution { public: int findContentChildren(vector& g, vector& s) { sort(g.begin(), g.end()); sort(s.begin(), s.end()); int child = 0, cookie = 0; while(child < g.size() && cookie < s.size()) { if(g[child] <= s[cookie]) { child++; } cookie++; } return child; } };

时间:O(n*logn) 空间:O(1)

Menglin-l commented 2 years ago

思路:分别对两个数组排序,从最小值开始比较,判断最小量的饼干能否满足最小食量的孩子,从局部最优解得到全局最优解。


代码部分:

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);

        int cnt = 0;
        for (int i = 0; cnt < g.length && i < s.length; i ++) {
            if (s[i] >= g[cnt]) {
                cnt ++;
            }
        }
        return cnt;
    }
}

复杂度:

Time: O(GlogG + SlogS),G和S为两个数组的长度

Space: O(logG + logS),G和S为两个数组的长度,用于排序的额外空间开销

yyangeee commented 2 years ago

/**

AruSeito commented 2 years ago
var findContentChildren = function(g, s) {
    g.sort((a,b)=>a-b);
    s.sort((a,b)=>a-b);
    let res = 0;
    let i = g.length - 1,j = s.length - 1;
    while(i>=0&&j>=0){
        if(s[j]>=g[i]){
            res ++;
            j--;
            i--;
        }else{
            i--;
            continue;
        }
    }
    return res;
};
TjuAachen commented 2 years ago

Greedy Algorithm

Intuition

To maximize the number of children content, we can maximize the efficiency of assignment by minimizing the difference between expectation and real gain. That is we assign a cookie to satisfy the greediest child it can satisfy. In this way, we can make the fullest use of each cookie and achieve our goal.

Algorithm

Based on the intuition above, we adopt the greedy algorithm and implement it by two pointer technique. To begin with, the two arrays are sorted to facilitate the match between the greediest and the best cookie. Once sorted, both of two pointers are placed at the end of the two arrays separately. While both p and q are no less than 0, the element pointed by p would be compared with the one pointed by q. If s[q]>=g[p], it means the match can be made and both p,q move towards 0 for one unit. And the result would increase by 1 to record this successful assignment. Otherwise, the match is not feasible and we have to find a better cookie by only moving q backward. When the while loop is terminated, the result would be returned as the final result.

Code

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        p,q=len(g)-1,len(s)-1
        result=0
        while(p>=0 and q>=0):
            if s[q]>=g[p]:
                result=result+1
                q=q-1
                p=p-1
            else:
                p=p-1
        return result

Complexity Analysis

Time Complexity: O(max(Nglog(Ng), Nslog(Ns))

Space Complexity: O(1)

Bingbinxu commented 2 years ago

思路 贪心+双指针;通过对饼干和胃口排序,用双指针遍历 代码(C++)

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        int gl = g.size();
        int sl = s.size();
        int gi=0 ,si = 0,res = 0;
        while(gi < gl && si < sl)
        {
            if(s[si] >= g[gi])
            {
                si++;
                gi++;
                res++;
            }
            else
            {
                si++;
            }
        }
        return res;
    }
};

复杂度分析 时间复杂度:O(N^logn) 空间复杂度:O(1)

shamworld commented 2 years ago
var findContentChildren = function(g, s) {
    g.sort((a,b)=>b-a);
    s.sort((a,b)=>b-a);
    let res = 0,index = 0;
    for(let i = 0; i < s.length; i++){
        while(index<g.length){
            if(g[index]<=s[i]){
                res++;
                index++;
                break;
            }
            index++;
        }
    }
    return res;
};
RonghuanYou commented 2 years ago
class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort(reverse=True)
        s.sort(reverse=True)
        gi, si = 0, 0

        cnt = 0

        while gi < len(g) and si < len(s):
            if s[si] >= g[gi]:
                cnt += 1
                si += 1
            gi += 1

        return cnt
BpointA commented 2 years ago

思路

贪心

Python3代码

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        i=0
        j=0
        cnt=0
        while i<len(g) and j<len(s):
            while j<len(s) and  s[j]<g[i] :
                j+=1
            if j<len(s) and i<len(g) and s[j]>=g[i]:
                cnt+=1
            i+=1
            j+=1

        return cnt
mokrs commented 2 years ago
int findContentChildren(vector<int>& g, vector<int>& s) {
    sort(g.begin(), g.end());
    sort(s.begin(), s.end());

    int count = 0;    
    for (int i = 0, j = 0; i < g.size() && j < s.size(); ++i, ++j) {
        while (j < s.size() && g[i] > s[j]) {
            ++j;
        }

        if (j < s.size()) {
            ++count;
        }
    }

    return count;
}
HouHao1998 commented 2 years ago

代码

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

复杂度分析

时间复杂度:O(N^logn) 空间复杂度:O(1)

V-Enzo commented 2 years ago

思路

  1. 双指针,一个遍历小孩的胃口,一个控制饼干的大小。
  2. 对两个数组进行排序,讲胃口大小以及饼干大小从小到大排序。
  3. 如果饼干大小比胃口大小大,那么就可以衡量下一个小朋友,同时自增饼干下标。
    class Solution {
    public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        int j=0;
        for(int i=0; i<s.size()&&j<g.size(); i++)
        {
            if(s[i]>=g[j]) j++;
        }
        return j;
    }
    };

    Complexity:

    Time:O(n^logn) Space:O(1)

Laurence-try commented 2 years ago

思路

Order and then greedy

代码

使用语言:Python3

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        count = 0
        g_p = 0
        s_p = 0
        g.sort()
        s.sort()
        while s_p != len(s):
            if s[s_p] >= g[g_p]:
                count += 1
                g_p += 1
                s_p += 1
            else:
                s_p += 1
            if g_p == len(g):
                break
        return count

复杂度分析 时间复杂度:O(nlogn) 空间复杂度:O(1)

KennethAlgol commented 2 years ago

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        if(g == null || s == null){
            return 0;
        }

        int res = 0;
        Arrays.sort(g);
        Arrays.sort(s);

        int i = 0;
        int j = 0;
        while(i < g.length && j < s.length){
            while(j < s.length && s[j] < g[i]){
                j++;
            }
            if(j < s.length && s[j] >= g[i]){
                res++;
                i++;
                j++;
            }
        }

        return res;
    }
}
LareinaWei commented 2 years ago

Thinking

Order and greedy. Increase count and move to the next child if the child is content with the current cookie. Always move the cookie pointer to the next cookie.

Code

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        i, j, count = 0, 0, 0
        while i < len(g) and j < len(s):
            if g[i] <= s[j]:
                count += 1
                i += 1
            j += 1

        return count

Complexity

Time complexity: O(nlogn). Time for sorting the lists.
Space complexity: O(1).

asterqian commented 2 years ago

思路

贪心算法,先对两个数组进行排序,用尽量小的饼干去喂胃口小的孩子。如果满足条件可以喂,则更新答案和饼干数组指针,否则小孩数组指针不变,饼干数组指针前进一格

代码

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        int j = 0;
        int res = 0;
        for (int i = 0; i < g.size() && j < s.size(); ++i) {
            if (s[j] >= g[i]) {
                res++;
                j++;
            } else {
                j++;
                i--;
            }
        }
    }
    return res;
};
时间复杂度 O(nlogn + mlogm)
空间复杂度 O(1)
JinhMa commented 2 years ago

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

guangsizhongbin commented 2 years ago
func findContentChildren(g []int, s []int) (ans int) {
    sort.Ints(g)
    sort.Ints(s)
    n, m := len(g), len(s)
    for i, j := 0, 0; i < n && j < m; i++ {
        for j < m && g[i] > s[j]{
            j++
        }

        if j < m {
            ans++
            j++
        }
    }
    return 
}
okbug commented 2 years ago
/**
 * @param {number[]} g
 * @param {number[]} s
 * @return {number}
 */
function sort(arr) {
    arr.sort((a, b) => a - b);
}
var findContentChildren = function(g, s) {
    sort(g);
    sort(s);
    console.log(g, s)
    let res = 0;
    for (let i = 0, j = 0; i < g.length && j < s.length; i++, j++) {
        while (j < s.length && g[i] > s[j]) {
            j++;
        }

        if (j < s.length) {
            res++;
        }
    }

    return res;
};
sumukeio commented 2 years ago

class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s);

    int cnt = 0;
    for (int i = 0; cnt < g.length && i < s.length; i ++) {
        if (s[i] >= g[cnt]) {
            cnt ++;
        }
    }
    return cnt;
}

}

Auto-SK commented 2 years ago

思路

贪心法,给最小胃口的小孩最小的饼干。

程序

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        child, cookie = 0, 0
        while child < len(g) and cookie < len(s):
            if s[cookie] >= g[child]:
                child += 1
            cookie += 1
        return child

复杂度

ForLittleBeauty commented 2 years ago

思路


贪心+双指针

将g与s排序后,用尽可能小的s满足g,如果完成则双指针向后移动,如果不满足则将s增大


代码

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        count = 0
        gi = 0
        si = 0
        while gi<len(g) and si<len(s):
            if s[si]>=g[gi]:
                count+=1
                gi+=1
                si+=1
            else:
                si+=1
        return count

时间复杂度: O(nlogn) 排序

空间复杂度: O(1)

newbeenoob commented 2 years ago

思路:


对小朋友的胃口和饼干的尺寸进行排序,优先让大的饼干满足胃口大的孩子即可

/**
 * @param {number[]} g
 * @param {number[]} s
 * @return {number}
 */
var findContentChildren = function(g, s) {
    g.sort((a , b) => a - b);
    s.sort((a, b) => a - b);

    let ans = 0;
    let i = s.length - 1;
    let j = g.length - 1;
    while( i >= 0 && j >= 0 ) {
        if(s[i] >= g[j]){
            // 可以喂饱
            ans++;
            i--;
            j--;
        } else {
            j--;
        }
    }

    return ans;
};

复杂度分析


标签


排序 , 贪心

machuangmr commented 2 years ago

思路

BreezePython commented 2 years ago

思路

贪心思路,从吃的少的孩子开始分配,获得最大化满足。

代码

class Solution:
    def findContentChildren(self, g, s):
        g.sort()
        s.sort()
        ret, point, ln = 0, -1, len(s)
        for child in g:
            while point < ln - 1:
                point += 1
                if s[point] >= child:
                    ret += 1
                    break
        return ret

复杂度

yj9676 commented 2 years ago
class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int numOfChildren = g.length, numOfCookies = s.length;
        int count = 0;
        for (int i = 0, j = 0; i < numOfChildren && j < numOfCookies; i++, j++) {
            while (j < numOfCookies && g[i] > s[j]) {
                j++;
            }
            if (j < numOfCookies) {
                count++;
            }
        }
        return count;
    }
}
jaysonss commented 2 years ago

思路

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        if(s.length==0)return 0;

        Arrays.sort(g);
        Arrays.sort(s);

        int i=0,j=0;
        while(i<g.length && j<s.length){
            if(s[j]>=g[i]){
                i++;
                j++;
            }else{
                j++;
            }
        }
        return i;
    }
}
leo173701 commented 2 years ago
class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        if len(s)==0:
            return 0
        g.sort()
        s.sort()
        left = 0
        right = 0
        res = 0
        while left<len(g) and right < len(s):
            if g[left]<=s[right]:
                left+=1
                right+=1
                res +=1
            else:
                right+=1
        return res
HWFrankFung commented 2 years ago

Codes

var findContentChildren = function (g, s) {
    g.sort(asc);
    s.sort(asc);

    let gp = 0,
        sp = 0;

    while (sp < s.length && gp < g.length) {
        if (s[sp] >= g[gp]) gp++;
        sp++;
    }
    return gp;

    function asc(a, b) {
        return a - b;
    }
};
Richard-LYF commented 2 years ago

class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() n, m = len(g), len(s) i = j = count = 0

    while i < n and j < m:
        while j < m and g[i] > s[j]:
            j += 1
        if j < m:
            count += 1
        i += 1
        j += 1

    return count
ChenJingjing85 commented 2 years ago

思路

贪心策略: 局部最优为:最小的饼干给胃口最小的孩子

代码

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        ans = 0
        gi = 0
        si = 0
        while gi < len(g) and si < len(s):
            while si < len(s) and g[gi] > s[si]:
                si += 1
            if si < len(s):
                ans += 1
                gi += 1
                si += 1
        return ans
chakochako commented 2 years ago

def findContentChildren(self, g: List[int], s: List[int]) -> int:

        g.sort()
        s.sort()

        count = i = j = 0

        while i < len(g) and j < len(s):
            while j < len(s) and g[i] > s[j]:
                j += 1

            if j < len(s):
                count += 1
                i += 1
                j += 1

        return count
winterdogdog commented 2 years ago
/**
 * @param {number[]} g
 * @param {number[]} s
 * @return {number}
 */
var findContentChildren = function(g, s) {
    g.sort((a, b) => a - b);
    s.sort((a, b) => a - b);
    let ans = 0;
    for(let i = 0, j = 0; i < g.length && j < s.length; i++, j++) {
        while(j < s.length && s[j] < g[i]) {
            j++
        }
        if (j < s.length) {
            ans++
        }
    }
    return ans;
};
brainlds commented 2 years ago

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

vuesch commented 2 years ago

思路

贪心+双指针,给胃口最小的孩子吃最小的饼干

代码

 /**
  * 
  * @param {gi} 胃口大小
  * @param {sj} 饼干大小
  * @returns 
  */
 const handle = function (g, s) {
  // 先进行排序
  g = g.sort((a, b) => a - b);
  s = s.sort((a, b) => a - b);
  let gi = 0; 
  let sj = 0; 
  let res = 0;
  while (gi < g.length && sj < s.length) {
    if (s[sj] >= g[gi]) {
      gi++;
      sj++;
      res++;
    } else {
      sj++;
    }
  }
  return res;
};

复杂度解析

时间复杂度:O(N^logn) 空间复杂度:O(1)

Zhang6260 commented 2 years ago

JAVA版本

思路:其实该题有二种思路,一种是拿饼干去找满足最低要求的小孩子的胃口,一种是那种胃口的值去找合适的饼干。(注意要做最低的要求,所以需要对二个数组进行排序!!!)

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

时间复杂度:O(n)

空间复杂度:O(1)