leetcode 575.分糖果

思路:开两个数组,一个用来存储非负数的糖果个数,一个用来存储负数的糖果个数,这两个数组都是状态数组,而不是计数数组

如果当前能够吃的种类大于现有的种类,现有的种类个数就是答案;

如果当前能够吃的种类小于等于现有的种类,则只能限制在能够吃的种类个数。

上代码:

复制代码
class Solution {
public:
    int distributeCandies(vector<int>& candyType) {
        int n=candyType.size();
        vector<bool>st1(100010,false);
        vector<bool>st2(100010,false);
        int chi=n/2;
        int target=0;
        for(int i=0;i<n;i++){
            if(candyType[i]<0){
                st2[abs(candyType[i])]=true;
            }
            else{
                st1[candyType[i]]=true;
            }
        }
        for(int i=0;i<100010;i++){
            if(st1[i])
            target++;
            if(st2[i])
            target++;
        }
        if(chi>=target)
        return target;
        else
        return chi;
    }
};
相关推荐
ytttr87317 分钟前
MATLAB基于LDA的人脸识别算法实现(ORL数据库)
数据库·算法·matlab
jianfeng_zhu2 小时前
整数数组匹配
数据结构·c++·算法
smj2302_796826522 小时前
解决leetcode第3782题交替删除操作后最后剩下的整数
python·算法·leetcode
LYFlied3 小时前
【每日算法】LeetCode 136. 只出现一次的数字
前端·算法·leetcode·面试·职场和发展
唯唯qwe-3 小时前
Day23:动态规划 | 爬楼梯,不同路径,拆分
算法·leetcode·动态规划
做科研的周师兄4 小时前
中国土壤有机质数据集
人工智能·算法·机器学习·分类·数据挖掘
来深圳4 小时前
leetcode 739. 每日温度
java·算法·leetcode
yaoh.wang4 小时前
力扣(LeetCode) 104: 二叉树的最大深度 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
hetao17338374 小时前
2025-12-21~22 hetao1733837的刷题笔记
c++·笔记·算法
醒过来摸鱼5 小时前
递归三种分类方法
算法