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

6 stars 0 forks source link

【Day 65 】2022-06-04 - 455. 分发饼干 #70

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

C++

class Solution
{
public:
    int findContentChildren(vector<int> &g, vector<int> &s)
    {
        int res = 0;
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        for (int i = 0, j = 0; i < g.size(); i++)
        {
            while (j < s.size() && g[i] > s[j])
                j++;
            if (j < s.size())
            {
                res++;
                j++;
            }
            else
            {
                break;
            }
        }

        return res;
    }
djd28176 commented 2 years ago

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

Idea

Greedy

Code

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

Complexity Analysis

asuka1h 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; int cnt = 0; while( i < g.size() && j < s.size()){ if(s[j] >= g[i]){ cnt++; ++i; ++j; } else{ ++j; }

   }
   return cnt;
}

};

复杂度

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

MichaelXi3 commented 2 years ago

Idea

本题思路:用从大到小的 cookies 尽可能满足最多数量的小孩

Code

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        // 从小到大 sort
        Arrays.sort(g);
        Arrays.sort(s);
        int num = 0;

        int child = g.length - 1;
        int cookie = s.length - 1;
        while (child >=0 && cookie >= 0) {
            // 最大的 cookie 可以满足最贪心的小孩
            if (s[cookie] >= g[child]) {
                num++;
                cookie--;
                child--;
            } else {
                // 最贪心的小孩过于贪心,无法满足
                child--;
            }
        }
        return num;
    }
}

Complexity

Ellie-Wu05 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()

        cnt=0

        i = 0
        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):
                cnt +=1
                j+=1
                i+=1

        return cnt

复杂度

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

MoonLee001 commented 2 years ago

思路

贪心

代码

var findContentChildren = function(g, s) {
    g.sort((a, b) => a - b);
    s.sort((a, b) => a - b);
    const n = g.length, m = s.length;
    let i = 0, j = 0;
    let count = 0;
    while(i < n && j < m) {
        if (s[j] >= g[i]) {
            i++;
            j++;
            count++;
        } else {
            j++;
        }

    }
    return count;
};

复杂度分析

zqwwei commented 2 years ago

Idea

method: feed the smallest s (cookie) to smallest child (g): if not satisfied discard cookie, b/c larger child won't be satified if satified, count++ Time: O(N log N) Space: O(log N)

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

        int i = 0;
        for (var cookie: s) {
            if (i != g.length && cookie >= g[i]) {
                i++;
            }
        }

        return i;
    }
houmk1212 commented 2 years ago

思路

贪心方法,一定是从最小的s[i]开始满足,能够满足最多的小朋友

代码

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

复杂度

carterrr commented 2 years ago

class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s); int res = 0; for(int i = 0,j = 0; i < g.length && j < s.length; j++) { // 下一个饼干试下当前孩子合不合胃口 孩子合胃口了孩子才动 饼干一直后移 if(g[i] <= s[j]) { res ++; i ++; }

    }
    return res;
}

}

xingchen77 commented 2 years ago
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        count = 0
        idx = 0
        while idx < len(s) and count < len(g):
            if s[idx] >= g[count]:
                count += 1
            idx += 1
        return count
Magua-hub 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 res = 0;
        for(int i = 0, j = 0; i < g.size(); i ++) {
            while(j < s.size() && s[j] < g[i]) j ++;
            if(j < s.size() && s[j] >= g[i]) {
                j ++;
                res ++;
            }
            else break;
        }
        return res;
    }
};
wychmod commented 2 years ago

思路

贪心,先排序,然后双指针比较

代码

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

复杂度分析

时间复杂度Onlogn 空间复杂度O1

ZacheryCao commented 2 years ago

Idea

Sort + Greedy

Code

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

Complexity:

Time: O(max(m, n) log ( max(m, n)). m: # children, n: # cookies Space: O(log (max(m,n))

zhiyuanpeng 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, j = 0, 0
        while i < len(g) and j < len(s):
            if g[i] <= s[j]:
                ans += 1
                i += 1
                j += 1
            else:
                while j < len(s) and g[i] > s[j]:
                    j += 1
        return ans

time O(nlogn) space O(1)

Yongxi-Zhou commented 2 years ago

思路

greedy

代码

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

复杂度

time O(max(g, s)) space O(1)

xil324 commented 2 years ago
class Solution(object):
    def findContentChildren(self, g, s):
        """
        :type g: List[int]
        :type s: List[int]
        :rtype: int
        """
        g = sorted(g);
        s = sorted(s);
        index_s = len(s)-1;
        index_g = len(g)-1;
        ans = 0; 
        while index_s >= 0 and index_g >= 0:
            if s[index_s] >= g[index_g]:
                ans += 1; 
                index_s -=1;
                index_g -=1;
            else:
                index_g -= 1;
        return ans
#time complexity: O(nlogn) due to sorting
currybeefer commented 2 years ago

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; 
yanjyumoso commented 2 years ago
class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:

        g.sort()
        s.sort()

        ans = 0

        i, j = 0, 0
        while i < len(g) and j < len(s):
            if g[i] <= s[j]:
                i += 1
                ans += 1
            j += 1
        return ans
miss1 commented 2 years ago

思路

先将g和s排序,再用两个指针,每个饼干尝试一次,不满足条件则换下一块饼干

代码

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

复杂度

XXjo commented 2 years ago

思路

因为每个饼干只能分给一个小盆友,所以要满足尽可能多的小盆友,需要优先将小尺寸饼干给小胃口
1、先将g和s都排序
2、s和g都只遍历一次,当s[j] >= g[i],结果增加1

代码

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

Day65 思路

1.先把孩子胃口和饼干大小从小到大排序(应该按照孩子的胃口从小到大的顺序依次满足每个孩子,且对于每个孩子,应该选择可以满足这个孩子的胃口且尺寸最小的饼干)

  1. 设置一个临时变量temp,每次给饼干后temp+1,下次遍历直接从temp开始,
    class Solution {
    public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        int ans=0;
        //临时变量,用于第二层循环时,不再回溯
        int temp=0;
        //i--g--胃口  j--s--饼干
        for(int i=0; i<g.size();i++){
            for(int j=temp; j<s.size(); j++){
                //胃口小于饼干,则饼干给他,ans+1,临时变量也加+1,直接跳出,不再往后遍历
                if(g[i] <= s[j]){
                    ans+=1;
                    temp= j+1;
                    break;
                }
            }
        }
        return ans;
    }
    };

    复杂度分析

    • 时间复杂度:O()
    • 空间复杂度:O()
shawnhu23 commented 2 years ago

Idea

greedy

Code

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

Complexity

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

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

        cnt=0

        i = 0
        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):
                cnt +=1
                j+=1
                i+=1

        return cnt
KWDFW commented 2 years ago

Day65

455、分发饼干

javascript #贪心算法

思路

1、两个数组分别进行排序

2、遍历每个饼干,看是否有孩子满足

3、有孩子满足就记录下来,并找下一个孩子

4、遍历完所有饼干结束

代码

var findContentChildren = function(g, s) {
  let count = 0;//计数且记录孩子的位置
  g.sort((a, b) => a - b);
  s.sort((a, b) => a - b);
  //分别进行升序排序
  for (let i = 0; i < s.length; i++) {//遍历饼干
    if (count < g.length && g[count] <= s[i]) {
      count++;
    }
    //如果有饼干能满足孩子,就记录,并找下一个孩子
  }
  return count;
};

复杂度分析

时间复杂度:O(nlogn)

空间复杂度:O(log)

LQyt2012 commented 2 years ago

思路

排序加贪心算法

代码

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        g_len = len(g)
        s_len = len(s)
        i = 0
        j = 0
        count = 0
        while i < g_len and j < s_len:
            while j < s_len and s[j] < g[i]:
                j += 1
            if j < s_len and s[j] >= g[i]:
                count += 1
            i += 1
            j += 1
        return count
func findContentChildren(g []int, s []int) int {
    sort.Ints(g)
    sort.Ints(s)
    gLen := len(g)
    sLen := len(s)
    i := 0
    j := 0
    count := 0
    for i < gLen && j < sLen {
        for j < sLen && s[j] < g[i] {
            j++
        }
        if j < sLen && s[j] >= g[i] {
            count++
        }
        i++
        j++
    }
    return count
}

复杂度分析

时间复杂度:O(MlogM+NlogN)
空间复杂度:O(1)

okbug commented 2 years ago
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;
};
Jongeehsu commented 2 years ago
class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int index = 0;
        int result = 0;
        for (int i = 0; i < s.length && index < g.length; i++) {
            if (s[i] >= g[index]) {
                index++;
                result++;
            }
        }
        return result;
    }
}
Time Complexity: O(mlogm + nlogn)
Space Complexity: O(logm + logn)
mo660 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 gNum = 0, sNum = 0, count = 0;
        while (gNum < g.size() && sNum < s.size())
        {
            if (g[gNum] <= s[sNum]){
                gNum++;
                sNum++;
                count++;
            }else{
                sNum++;
            }
        }
        return count;
    }
};
freedom0123 commented 2 years ago

class Solution
{
public:
    int findContentChildren(vector<int> &g, vector<int> &s)
    {
        int res = 0;
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        for (int i = 0, j = 0; i < g.size(); i++)
        {
            while (j < s.size() && g[i] > s[j])
                j++;
            if (j < s.size())
            {
                res++;
                j++;
            }
            else
            {
                break;
            }
        }

        return res;
    } ```
tensorstart 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
};
revisegoal commented 2 years ago

贪心

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 && j < g.length; i++) {
           if (s[i] >= g[j]) {
               j++;
               res++;
           }
        }
        return res;
    }

}
flyzenr 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
houyanlu 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 ans = 0;
        for (int i = g.size() - 1; i >= 0; i--) {
            if (index >= 0 && s[index] >= g[i]) {
                ans++;
                index--;
            }
        }
        return ans;
    }
};

复杂度分析

taojin1992 commented 2 years ago
class Solution {
    // time: O(mlogm) + O(nlogn) + O(min(m, n))
    // space: O(1)
    // two pointers + greedy
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int contentKids = 0;
        int sIndex = 0, gIndex = 0;
        while (sIndex < s.length && gIndex < g.length) {
            if (g[gIndex] <= s[sIndex]) {
                contentKids++;
                gIndex++;
                sIndex++;
            } else {
                sIndex++;
            }
        }

        return contentKids;
    }
}
ShuqianYang 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()

        cnt=0

        i = 0
        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):
                cnt +=1
                j+=1
                i+=1
        return cnt
maggiexie00 commented 2 years ago
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
sallyrubyjade commented 2 years ago
/**
 * @param {number[]} g
 * @param {number[]} s
 * @return {number}
 */
var findContentChildren = function(g, s) {
    g = g.sort((a,b) => a-b);
    s = s.sort((a,b) => a-b);
    var gLen = g.length;
    var sLen = s.length;
    var i = 0;
    var j = 0;
    var maxNum = 0;
    while(i < gLen && j < sLen){
        if(s[j] >= g[i]){
            i++;
            j++;
            maxNum++;
        }else{
            j++;
        }
    }
    return maxNum;
};
fhuang5 commented 2 years ago
class Solution {
    public int findContentChildren(int[] g, int[] s) {
        int child = 0;
        int cookie = 0;
        Arrays.sort(g);
        Arrays.sort(s);
        while (child < g.length && cookie < s.length){
            if (g[child] <= s[cookie]){
                child++;
            }
            cookie++;
        }
        return child;    
    }
}

//Time:O(nlogn)

//Space: O(1)
xiayuhui231 commented 2 years ago

class Solution { public: int findContentChildren(vector &g, vector &s) { int res = 0; sort(g.begin(), g.end()); sort(s.begin(), s.end()); for (int i = 0, j = 0; i < g.size(); i++) { while (j < s.size() && g[i] > s[j]) j++; if (j < s.size()) { res++; j++; } else { break; } }

    return res;
}
zhishinaigai commented 2 years ago
int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        int lg=0;
        int ls=0;
        while(lg<g.size()&&ls<s.size()){
            if(g[lg]<=s[ls]){
                ls++;
                lg++;
            }
            else{
                ls++;
            }
        }
        return lg;
    }