leetcode 455. Assign Cookies和2410. Maximum Matching of Players With Trainers

目录

[455. Assign Cookies](#455. Assign Cookies)

[2410. Maximum Matching of Players With Trainers](#2410. Maximum Matching of Players With Trainers)


455. Assign Cookies

排序+双指针+贪心

cpp 复制代码
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()&&j<s.size();i++){
            while(j<s.size() && s[j]<g[i] ){
                j++;
            }
            if(j == s.size())
                break;
            res++;
            j++;
        }
        return res;
    }
};

2410. Maximum Matching of Players With Trainers

这道题和上面的是一模一样的。

cpp 复制代码
class Solution {
public:
    int matchPlayersAndTrainers(vector<int>& players, vector<int>& trainers) {
        sort(players.begin(),players.end());
        sort(trainers.begin(),trainers.end());
        int res = 0;
        for(int i = 0,j = 0;i<players.size()&&j<trainers.size();i++){
            while(j<trainers.size() && trainers[j]<players[i]){
                j++;
            }
            if(j == trainers.size())
                break;
            res++;
            j++;
        }
        return res;
    }
};
相关推荐
源代码•宸18 分钟前
Leetcode—721. 账户合并【中等】
c++·经验分享·算法·leetcode·并查集
緈福的街口20 分钟前
【leetcode】77.组合
算法·leetcode·职场和发展
快去睡觉~1 小时前
力扣152:乘积最大子数组
算法·leetcode·职场和发展
程序员Xu1 小时前
【LeetCode热题100道笔记】二叉树的中序遍历
笔记·算法·leetcode
快去睡觉~2 小时前
力扣416:分割等和子集
数据结构·c++·算法·leetcode·职场和发展·动态规划
仙俊红2 小时前
LeetCode每日一题,2025-9-5
算法·leetcode·职场和发展
阿维的博客日记2 小时前
LeetCode 240: 搜索二维矩阵 II - 算法详解(秒懂系列
算法·leetcode·矩阵
君万4 小时前
【LeetCode每日一题】94. 二叉树的中序遍历 104. 二叉树的最大深度
算法·leetcode·golang
Imxyk4 小时前
力扣:2322. 从树中删除边的最小分数
数据结构·算法·leetcode
农场主John4 小时前
(双指针)LeetCode 209 长度最小的子数组
数据结构·算法·leetcode