力扣原题《分发糖果》,采用二分原则,纯手搓,待验证

n 个孩子站成一排。给你一个整数数组 ratings 表示每个孩子的评分。

你需要按照以下要求,给这些孩子分发糖果:

每个孩子至少分配到 1 个糖果。

相邻两个孩子中,评分更高的那个会获得更多的糖果。

请你给每个孩子分发糖果,计算并返回需要准备的 最少糖果数目 。
示例 1:

输入:ratings = [1,0,2]

输出:5

解释:你可以分别给第一个、第二个、第三个孩子分发 2、1、2 颗糖果。
示例 2:

输入:ratings = [1,2,2]

输出:4

解释:你可以分别给第一个、第二个、第三个孩子分发 1、2、1 颗糖果。

第三个孩子只得到 1 颗糖果,这满足题面中的两个条件。
提示:

n == ratings.length

1 <= n <= 2 * 104

0 <= ratings[i] <= 2 * 104

我的答案

cpp 复制代码
class Candy {
public:
    Candy() {}
    int candy(vector<int>& ratings) {
        int candyCount = 0;
        for (int i = 0; i < ratings.size(); i++)
        {
           candyCount+= toCompare(ratings, i);
        }
        std::cout <<std::endl;
        return candyCount;
    }
    int toCompare(vector<int>& ratings,int i)//返回该孩子的糖果数量,二分原则
    {
        int canGiveCandyCount = 1;//每人默认一个
        int theFrontIdx = i, thebBackIdx = i;
        int tag = 0;//防止重复分配
        int leftContinuous = -1;//出现连续的情况
        while (--theFrontIdx>=0)
        {
            if (ratings[theFrontIdx] < ratings[i]) 
            {
                if (theFrontIdx + 1 == i) 
                    ++tag;
                leftContinuous++;
                if (theFrontIdx-1>=0&& ratings[theFrontIdx-1]>=ratings[theFrontIdx])
                {
                   canGiveCandyCount++;
                   break;
                }
                else 
                    canGiveCandyCount++;
            }
            else 
                break;
        }
        int rightContinuous = -1;//出现连续的情况
        while (++thebBackIdx < ratings.size())
        {
            if (ratings[thebBackIdx] < ratings[i]) 
            {  
                if (thebBackIdx - 1 == i) 
                    ++tag;
                rightContinuous++;
                if (thebBackIdx + 1 < ratings.size() && ratings[thebBackIdx + 1] >= ratings[thebBackIdx])
                {
                    canGiveCandyCount++;
                    break;
                }
                else
                    canGiveCandyCount++;
            }
            else
                break;
        }
        if (tag == 2) //去重
        {
            if (leftContinuous > 0 && rightContinuous > 0)
                canGiveCandyCount = canGiveCandyCount - leftContinuous - rightContinuous;
            else//虽然没有重复,但是左右多加了一次
                canGiveCandyCount--;
        }
        std::cout<< canGiveCandyCount<<"," ;
        return canGiveCandyCount;
    }
};

int main()
{
    vector<vector<int>> ratings;
    ratings.push_back({ 1,2,3,1,0 });
    ratings.push_back({ 1,3,4,5,2 });
    ratings.push_back({ 1,3,2,2,1 });
    ratings.push_back({ 1,0,2 });
    ratings.push_back({ 1,2,2 });
    Candy c1;
    for (vector<vector<int>>::iterator iter = ratings.begin(); iter != ratings.end(); iter++)
    {
        std::cout << "最少准备糖果:" << c1.candy(*(iter)) << std::endl;
    }
相关推荐
玛卡巴卡ldf2 小时前
【LeetCode 手撕算法】(普通数组)53-最大子数组和、56-合并区间、189-轮转数组、238-除了自身以外数组的乘积
数据结构·算法·leetcode
Trouvaille ~2 小时前
【项目篇】从零手写高并发服务器(七):定时器TimerWheel与线程池
运维·服务器·网络·c++·reactor·高并发·muduo库
j_xxx404_3 小时前
蓝桥杯基础--模拟
数据结构·c++·算法·蓝桥杯·排序算法
Sakinol#3 小时前
Leetcode Hot 100 ——动态规划part02
算法·leetcode·动态规划
sqyno1sky3 小时前
零成本抽象在C++中的应用
开发语言·c++·算法
cm6543203 小时前
C++中的职责链模式
开发语言·c++·算法
小菜鸡桃蛋狗3 小时前
C++——类和对象(中)
开发语言·c++
MORE_773 小时前
leecode-灌溉花园-贪心算法and动态规划
算法·贪心算法·动态规划
feng_you_ying_li3 小时前
c++之二叉搜索树的实现
c++