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;
    }
};
相关推荐
阿贵---15 分钟前
C++中的备忘录模式
开发语言·c++·算法
setmoon21431 分钟前
C++中的观察者模式实战
开发语言·c++·算法
2403_8355684733 分钟前
C++代码规范化工具
开发语言·c++·算法
tankeven1 小时前
HJ138 在树上游玩
c++·算法
lihihi1 小时前
P1209 [USACO1.3] 修理牛棚 Barn Repair
算法
weixin_387534222 小时前
Ownership - Rust Hardcore Head to Toe
开发语言·后端·算法·rust
xsyaaaan2 小时前
leetcode-hot100-链表
leetcode·链表
庞轩px2 小时前
MinorGC的完整流程与复制算法深度解析
java·jvm·算法·性能优化
Queenie_Charlie2 小时前
Manacher算法
c++·算法·manacher
闻缺陷则喜何志丹2 小时前
【树的直径 离散化】 P7807 魔力滋生|普及+
c++·算法·洛谷·离散化·树的直径