575. Distribute Candies

本文探讨了LeetCode上的一道关于糖果分配的问题,旨在将不同种类的糖果平均分成两份,求解其中一份能包含的最大种类数量。文章提供了三种解决方法:排序后计数不同元素、去除重复元素后比较集合大小、使用unordered_set快速统计。

题目描述【Leetcode

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.

Example 1:
Input: candies = [1,1,2,2,3,3]
Output: 3
Explanation:
There are three different kinds of candies (1, 2 and 3), and two candies for each kind.
Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too.
The sister has three different kinds of candies.

Example 2:
Input: candies = [1,1,2,3]
Output: 2
Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1].
The sister has two different kinds of candies, the brother has only one kind of candies.

Note:
The length of the given array is in range [2, 10,000], and will be even.
The number in given array is in range [-100,000, 100,000].

这道题就是把数组平均分为两组,其中一组包含种类最多,求该种类个数

方法一:
就是先排好序,然后就找不同的数字即可

class Solution {
public:
    int distributeCandies(vector<int>& candies) {
        int n = candies.size();
        if(n == 0) return 0;
       sort(candies.begin(),candies.end());
       int count = 1;
       int temp = candies[0];
        for(int i = 1; i < n; i++ ){
            if(candies[i] != candies[i-1] && count < n/2)count++;
        }
        return count;
    }
};

方法二:删除重复项之后大小与之前数组作比较:

class Solution {
public:
    int distributeCandies(vector<int>& candies) {
        int n = candies.size();
        if(n == 0) return 0;
        sort(candies.begin(),candies.end());
        candies.erase(unique(candies.begin(),candies.end()),candies.end());
        int t = candies.size();
        if(t >= n/2) return n/2;
        if(t  < n/2) return t;
    }
};

方法三:用unordered_set,类似于方法二,但是运行更快:

class Solution {
public:
    int distributeCandies(vector<int>& candies) {
        int n = candies.size();
        unordered_set<int> s(candies.begin(), candies.end());
        if (s.size() >= n/2) return n/2;
        else return s.size();
    }
};
内容概要:本文提出了一种基于融合鱼鹰算法和柯西变异的改进麻雀优化算法(OCSSA),用于优化变分模态分解(VMD)的参数,进而结合卷积神经网络(CNN)与双向长短期记忆网络(BiLSTM)构建OCSSA-VMD-CNN-BILSTM模型,实现对轴承故障的高【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)精度诊断。研究采用西储大学公开的轴承故障数据集进行实验验证,通过优化VMD的模态数和惩罚因子,有效提升了信号分解的准确性与稳定性,随后利用CNN提取故障特征,BiLSTM捕捉时间序列的深层依赖关系,最终实现故障类型的智能识别。该方法在提升故障诊断精度与鲁棒性方面表现出优越性能。; 适合人群:具备一定信号处理、机器学习基础,从事机械故障诊断、智能运维、工业大数据分析等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①解决传统VMD参数依赖人工经验选取的问题,实现参数自适应优化;②提升复杂工况下滚动轴承早期故障的识别准确率;③为智能制造与预测性维护提供可靠的技术支持。; 阅读建议:建议读者结合Matlab代码实现过程,深入理解OCSSA优化机制、VMD信号分解流程以及CNN-BiLSTM网络架构的设计逻辑,重点关注参数优化与故障分类的联动关系,并可通过更换数据集进一步验证模型泛化能力。
Problem Statement You have an unlimited supply of two types of candies: small candies and large candies. The weight of a small candy is X grams, and the weight of a large candy is Y grams. Large candies are heavier than small candies (that is, X<Y). There are N children, numbered 1 to N. You have decided to distribute candies so that the following conditions are satisfied: For i=1,…,N, child i receives exactly A i ​ candies in total of the two types. The total weights of candies distributed to the N children are all equal. Determine whether there exists a distribution method that satisfies the conditions. If it exists, find the maximum possible value for the number of large candies distributed. DeepL 翻译    问题陈述 你有无限量的两种糖果:小糖果和大糖果。小糖果的重量是 X 克,大糖果的重量是 Y 克。大糖果比小糖果重(即 X<Y )。 有 N 个孩子,编号为 1 至 N 。 你决定分发糖果,以满足以下条件: 对于 i=1,…,N 来说, i 个孩子收到的两种糖果的总重量正好是 A i ​ 。 分给 N 个孩子的糖果的总重量都相等。 确定是否存在满足条件的分配方法。如果存在,求大号糖果分配数量的最大可能值。    Constraints 2≤N≤2×10 5 1≤A i ​ ≤10 9 1≤X<Y≤10 9 All input values are integers. DeepL 翻译    限制因素 2≤N≤2×10 5 1≤A i ​ ≤10 9 1≤X<Y≤10 9 所有输入值均为整数。    Input The input is given from Standard Input in the following format: N X Y A 1 ​ … A N ​ DeepL 翻译    输入 输入内容由标准输入法提供,格式如下 N X Y A 1 ​ … A N ​    Output If there is no distribution method that satisfies the conditions, output -1. If there exists a distribution method that satisfies the conditions, output the maximum possible value for the number of large candies distributed in such a distribution method. DeepL 翻译    输出 如果没有满足条件的分配方法,则输出 -1。 如果存在满足条件的分配方法,则输出这种分配方法所分配的大颗糖果数量的最大可能值。
11-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值