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

91 算法第六期打卡仓库
28 stars 0 forks source link

【Day 65 】2022-02-14 - 455. 分发饼干 #75

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.
Richard-LYF commented 2 years ago

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

    return res

onlogn+ omlogm logm + logn

wenlong201807 commented 2 years ago

代码块


var findContentChildren = function (g, s) {
  g = g.sort((a, b) => a - b);
  s = s.sort((a, b) => a - b);
  let result = 0;
  let index = s.length - 1;
  for (let i = g.length - 1; i >= 0; i--) {
    if (index >= 0 && s[index] >= g[i]) {
      result++;
      index--;
    }
  }
  return result;
};

时间复杂度和空间复杂度

ZhangNN2018 commented 2 years ago
class Solution(object):
    def change(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int
        """
        dp = [0]*(amount + 1)
        dp[0] = 1
        # 遍历物品
        for i in range(len(coins)):
            # 遍历背包
            for j in range(coins[i], amount + 1):
                dp[j] += dp[j - coins[i]]
        return dp[amount]
zwx0641 commented 2 years ago

class Solution { public: int findContentChildren(vector& g, vector& s) { sort(g.begin(), g.end()); sort(s.begin(), s.end());

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

};

ZJP1483469269 commented 2 years ago
class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        h = []

        for w in g:
            heappush(h , -w)
        s.sort(reverse = True)

        j = 0
        while h and j < len(s):
            w = -heappop(h)
            if w > s[j]:
                continue
            else:
                j += 1
        return j
charlestang commented 2 years ago

思路

贪心。

用尽可能小的饼干,去满足胃口尽可能小的孩子。

代码

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()

        i = 0
        j = 0
        ans = 0

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

时间复杂度 O(nlogn)

空间复杂度 O(logn) 排序导致的

yetfan commented 2 years ago

思路 每人最多一块小饼干,排序 优先满足需求小的

代码

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

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

        return i

复杂度 时间 O(mlogm+nlogn) 空间 O(logm+logn)

ZacheryCao commented 2 years ago

idea

greedy + sort

code

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

        return i

Complexity:

Time: (max(M, N) * log(max(M,N)) M, N: length of s and g Space: O(1)

zhy3213 commented 2 years ago

思路

贪心

代码

    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        start, count = len(s)-1, 0 
        for index in range(len(g)-1, -1, -1):
            if  start>=0 :
                if s[start]>=g[index]:
                    start  -= 1
                    count += 1
            else:
                break
        return count
xinhaoyi commented 2 years ago

class Solution { public int findContentChildren(int[] g, int[] s) { //1.首先需要将这两个数组排序一下,让他们有序,这样在遍历的时候往后走就相当于“给他吃” //也就是小孩子从胃口最小的到胃口最大的排,饼干从最小块的到最大块的排。 //2.因为我们要求的是满足孩子饱腹的最小的饼干,所以s[]数组只要一大于等于他,a就可以往后++了,相当于吃上了 //3.接着就到下一个小孩了,因为小孩的大小也是从小到大, //所以前面的饼干连第一个胃口小的孩子都吃不饱,更别说第二位了,所以饼干也得从上一位吃完的下一块试起。 //4.什么时候结束呢?知道孩子都吃完了或者饼干都遍历完了就退出 //前者是孩子都有的吃,而后者是饼干已经没得匹配了就中断了。 int a=0 ,b=0; Arrays.sort(g); Arrays.sort(s); while(a<g.length&&b<s.length){ if(g[a]<=s[b])a++; b++; } return a; } }

zjsuper commented 2 years ago

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

Tesla-1i commented 2 years ago
class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        h = []

        for w in g:
            heappush(h , -w)
        s.sort(reverse = True)

        j = 0
        while h and j < len(s):
            w = -heappop(h)
            if w > s[j]:
                continue
            else:
                j += 1
        return j
yan0327 commented 2 years ago
func findContentChildren(g []int, s []int) int {
    sort.Ints(g)
    sort.Ints(s)
    l,r:=0,0
    for l<len(g)&&r<len(s){
        if g[l] <= s[r]{
            l++
        }
            r++
    }
    return l
}
1149004121 commented 2 years ago
  1. 分发饼干

思路

贪心算法。优先满足胃口小的孩子,两个指针分别指向g和s的起点,如果满足,则都向后移一位,如果不满足,则ps向后移一位。

代码

var findContentChildren = function(g, s) {
    let res = 0;
    g.sort((a, b) => a - b);
    s.sort((a, b) => a - b);
    let pg = 0, ps = 0;
    while(pg < g.length && ps < s.length){
        if(g[pg] <= s[ps]){
            pg++;
            ps++;
            res++
        }else{
            ps++
        }
    };
    return res;
};

复杂度分析

Serena9 commented 2 years ago

代码

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        s.sort(reverse=True)
        g.sort(reverse=True)
        gi, si = 0, 0
        count = 0
        while gi < len(g) and si < len(s):
            if s[si] >= g[gi]:
                count += 1
                si += 1
            gi += 1
        return count
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;
};
haixiaolu commented 2 years ago

思路

贪心 + 双指针, 拍完序后, 先满足胃口小的孩子, 先比较最后的饼干是否满足最后孩子的胃口, 满足count + 1, 不满足往前移动指针

代码 / Python

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        count = 0
        kid, cookie = len(g) - 1, len(s) - 1
        g = sorted(g)
        s = sorted(s)

        while min(kid, cookie) >= 0:
            if g[kid] <= s[cookie]:
                count += 1
                cookie -= 1
            kid -= 1
        return count 

复杂度分析:

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;
    }
}
jiaqiliu37 commented 2 years ago

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

GaoMinghao commented 2 years ago

代码

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

贪心,小块的饼干分给胃口小的孩子

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

        return total;
    }
}

TC: O(nlogn) SC: O(1)

luhnn120 commented 2 years ago

思路

贪心算法

代码

var findContentChildren = function(g, s) {
  g = g.sort((a, b) => a - b)
  s = s.sort((a, b) => a - b)
  let count = 0;
  for(let i = 0; i < g.length; i++){
    while(s.length > 0){
      if(g[i] <= s.shift()){
        count++;
        break;
      }
    }
  }
  return count;
};

复杂度

空间复杂度 O(1) 时间复杂度 O(NlogN)

baddate 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 index = s.size() - 1;
        int result = 0;
        for (int i = g.size() - 1; i >= 0; i--) {
            if (index >= 0 && s[index] >= g[i]) {
                result++;
                index--;
            }
        }
        return result;
    }
};
linyang4 commented 2 years ago

思路

贪心+二分

  1. 先对2个数组从小到大排序
  2. 对于g中每一个元素, 利用二分法, 从s中找到符合条件的最小饼干尺寸

代码(JavaScript)

var findContentChildren = function(g, s) {
    g.sort((left, right) => left - right)
    s.sort((left, right) => left - right)
    let ans = 0
    let sIndex = 0
    for(let i = 0; i < g.length; i++) {
        // 找到第一个满足的饼干
        getLeastCookie(sIndex, s.length - 1, g[i])
    }

    function getLeastCookie(start, end, target) {
        if (start > end) {
            return
        }
        const mid = (start + end) >>> 1
        if (s[mid] >= target) {
            if (mid === sIndex || s[mid - 1] < target) {
                ans++
                sIndex = mid + 1
            } else {
                getLeastCookie(start, mid - 1, target)
            }
        } else {
            getLeastCookie(mid + 1, end, target)
        }
    }
    return ans
};

复杂度

m为g的长度, n为s的长度

cszys888 commented 2 years ago
class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        p_g = 0
        p_s = 0
        n_g = len(g)
        n_s = len(s)
        result = 0
        while p_g < n_g and p_s < n_s:
            if g[p_g] <= s[p_s]:
                result += 1
                p_g += 1
                p_s += 1
            else:
                p_s += 1
        return result

time complexity: O(mlogm + nlogn) space complexity: O(logn + logm)

anyingbinglong commented 2 years ago

class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() p_g = 0 p_s = 0 n_g = len(g) n_s = len(s) result = 0 while p_g < n_g and p_s < n_s: if g[p_g] <= s[p_s]: result += 1 p_g += 1 p_s += 1 else: p_s += 1 return result time complexity: O(mlogm + nlogn) space complexity: O(logn + logm)

CoreJa commented 2 years ago

思路

贪心法:两个数组分别排序,然后同时遍历,如果s[i]>=g[j],即满足了条件,则计数并使i和j分别前移,否则只移i,直到能满足当前g[j]。i和j任一指针超出数组返回计数值。复杂度$O(m+n)$​

代码

class Solution:
    # 贪心法:两个数组分别排序,然后同时遍历,如果`s[i]`>=`g[j]`,即满足了条件,则计数并使`i`和`j`分别前移,否则只移`i`,
    # 直到能满足当前`g[j]`。`i`和`j`任一指针超出数组返回计数值。复杂度$O(m+n)$
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        idx = 0
        ans = 0
        m = len(g)
        for cookie in s:
            if cookie >= g[idx]:
                ans += 1
                idx += 1
                if idx >= m:
                    return ans
        return ans
HWFrankFung commented 2 years ago

Codes

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        int a=0 ,b=0;
        Arrays.sort(g);
        Arrays.sort(s);
        while(a<g.length&&b<s.length){
            if(g[a]<=s[b])a++;
            b++;
        }
        return a;
    }
}
ivangin commented 2 years ago

code:

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

        int ans = 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]){
                ans++;
                i++;
                j++;
            }
        }

        return ans;
    }
Alexno1no2 commented 2 years ago
#首先要对g和s排序
#用两个指针分别指向两个列表的末尾
#循环
#如果满足`s[r_s] >= g[r_g]`,则都向前移动
#否则只需要移动g的指针

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        r_g = len(g) - 1
        r_s = len(s) - 1
        count = 0
        while r_g >= 0 and r_s >= 0:
            if s[r_s] >= g[r_g]:
                count += 1
                r_g -= 1
                r_s -= 1
            else:
                r_g -= 1
        return count
LannyX commented 2 years ago

思路

贪心

代码

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

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

复杂度分析

falconruo commented 2 years ago

思路:

贪心

复杂度分析:

代码(C++):

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        if (s.empty()) return 0;

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

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

        return res;
    }
};
callmeerika commented 2 years ago

思路

贪心算法

代码

  var findContentChildren = function (g, s) {
    const gn = g.length;
    const sn = s.length;
    g.sort((v1, v2) => v1 - v2);
    s.sort((v1, v2) => v1 - v2);
    let i = 0;
    let j = 0;
    let res = 0;
    while (i < gn && j < sn) {
      while (j < sn && s[j] < g[i]) j++;
      if (j < sn) res++;
      i++;
      j++;
    }
    return res;
  };

复杂度

时间复杂度:O(mlogm+nlogn)
空间复杂度:O(logm+logn)

wdwxw 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;
    }
}
xuhzyy commented 2 years ago
class Solution(object):
    def findContentChildren(self, g, s):
        """
        :type g: List[int]
        :type s: List[int]
        :rtype: int
        """
        m, n = len(g), len(s)
        if m == 0 or n == 0:
            return 0
        g.sort()
        s.sort()
        l, r, ans = m - 1, n - 1, 0
        while(l >= 0 and r >= 0):
            if s[r] < g[0]:
                break
            if s[r] >= g[l]:
                ans += 1
                r -= 1
                l -= 1
            else:
                l -= 1
        return ans
alongchong 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;
    }
}
honeymeng-hub 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;

}

}

eliassama commented 2 years ago

整体思路

题目其实是需要找出 s 数组中元素大于等于比 g 数组中元素的个数。

如:

g = [1,2,3,4]
s = [1,1,1,1,1,1,1,4,1,1]

这个例子中,s 只有 [ 1, 4 ] 这两个大于等于 g 中的 [ 1, 2 ] 或 [ 1, 3 ] 或 [ 1, 4 ],而不管组合是哪一个,都是两个元素,所以结果是 2 。

排序 + 贪心

首先对两个数组进行排序,排序时需要注意,js 的数组排序是将数组中每个元素 toString 转换后,进行排序的。所以可能会出现 11 排在 2 的前面。

之后双指针同时对两个两个数组进行遍历,对比两边的元素:

Code
function numberSort( x: number, y: number ) {
    if (x < y) {
        return -1;
    } else if (x > y) {
        return 1;
    } else {
        return 0;
}}

function findContentChildren( g: number[], s: number[] ): number {
    let ans: number = 0;
    g.sort( numberSort );
    s.sort( numberSort );

    for ( let gIdx = 0, sIdx = 0; sIdx < s.length; ) {
        if ( g[ gIdx ] <= s[ sIdx ] ) {
            ++ gIdx;
            ++ sIdx;
            ++ ans;
        }else {
            ++ sIdx
        }
    }

    return ans;
};
复杂度分析

时间复杂度:O( n )。

空间复杂度:O( 1 )。

Riuusee commented 2 years ago

思路

贪心

代码

import java.util.Arrays;

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

        return ans;
    }
}

复杂度分析

declan92 commented 2 years ago

思路
每个孩子最多一块饼干,能分到的孩子越多越好,则饼干分出去越多越好;

  1. 对胃口值和饼干排序,每次分出最小的饼干给胃口最小的人;
  2. 饼干尺寸大于胃口,则结果值+1;丢弃该胃口值和饼干进入下一个状态;
  3. 饼干尺寸小于胃口,丢弃饼干,进入下一个状态;
  4. 进入下一个状态后,执行步骤1;
    java code
    class Solution {
    public int findContentChildren(int[] g, int[] s) {
        int glen = g.length;
        int slen = s.length;
        if(slen == 0 || glen == 0) return 0;
        Arrays.sort(g);
        Arrays.sort(s);
        int i = 0, j = 0,ans = 0;
        while(i < glen && j < slen){
            if(g[i] <= s[j]){
                i++;
                j++;
                ans++;
            }else{
                j++;
            }
        }
        return ans;
    }
    }

    时间:$O(max(mlogm,nlogn))$,m是g的长度,n是s的长度
    空间:$O(logn)$

Myleswork 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 gcur = 0;
        int scur = 0;
        int count = 0;
        while(gcur<g.size() && scur<s.size()){
            if(g[gcur]-s[scur]<=0){  //够了,换下一个小朋友
                gcur++;
                count++;
            }
            scur++;
        }
        return count;
    }
};

复杂度分析

时间复杂度:O(n)

空间复杂度:O(1)

biscuit279 commented 2 years ago

思路:

降序排序,先用最大的饼干满足胃口最大的孩子,如果不满足,说明这个孩子无法被满足,换下一个孩子,如果能满足,换下一个饼干

class Solution(object):
    def findContentChildren(self, g, s):
        """
        :type g: List[int]
        :type s: List[int]
        :rtype: int
        """
        count = 0
        g.sort(reverse = True)
        s.sort(reverse = True)    
        gi,si = 0,0

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

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

junbuer commented 2 years ago

思路

Aobasyp commented 2 years ago

思路 贪心求解 class Solution { public: int findContentChildren(vector& g, vector& s) {

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

    int gleft =0; 
    int sleft =0; 

    int count =0; 
    while(gleft< g.size() && sleft< s.size())
    {

        if(g[gleft]<=s[sleft])
        {
            count++; 
            gleft++; 
            sleft++; 
        }
        else
            sleft++;
    }

    return count; 

}

};

时间复杂度为 O(nlogn) 空间复杂度: O(logn)

Moin-Jer 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; } }

googidaddy 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() 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

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

demo410 commented 2 years ago
public int findContentChildren(int[] g, int[] s) {
        int glen = g.length;
        int slen = s.length;
        if(slen == 0 || glen == 0) return 0;
        Arrays.sort(g);
        Arrays.sort(s);
        int i = 0, j = 0,ans = 0;
        while(i < glen && j < slen){
            if(g[i] <= s[j]){
                i++;
                j++;
                ans++;
            }else{
                j++;
            }
        }
        return ans;
    }
Toms-BigData commented 2 years ago
func findContentChildren(g []int, s []int) (ans int) {
    sort.Slice(g, func(i, j int) bool {if g[i] > g[j] {return true};return false})
    sort.Slice(s, func(i, j int) bool {if s[i] > s[j] {return true};return false})
    glen := len(g)
    slen := len(s)
    a:=0
    b:=0
    for a<glen&&b<slen {
        if g[a] <= s[b]{
            ans++
            a++
            b++
        } else {
            a++
        }
    }
    return
}
hdyhdy commented 2 years ago

思路:优先满足小胃口

func findContentChildren(g []int, s []int) (res int) {
    sort.Ints(g)
    sort.Ints(s)
    gFlag, sFlag := 0, 0
    for gFlag < len(g) && sFlag < len(s) {
        if g[gFlag] <= s[sFlag] {
            res++
            gFlag++
        }
        sFlag++
    }
    return
}
Hacker90 commented 2 years ago

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