C语言 | Leetcode C语言题解之第135题分发糖果

题目:

题解:

int candy(int* ratings, int ratingsSize) {
    int ret = 1;
    int inc = 1, dec = 0, pre = 1;
    for (int i = 1; i < ratingsSize; i++) {
        if (ratings[i] >= ratings[i - 1]) {
            dec = 0;
            pre = ratings[i] == ratings[i - 1] ? 1 : pre + 1;
            ret += pre;
            inc = pre;
        } else {
            dec++;
            if (dec == inc) {
                dec++;
            }
            ret += dec;
            pre = 1;
        }
    }
    return ret;
}

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-11 08:40:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-11 08:40:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-11 08:40:04       87 阅读
  4. Python语言-面向对象

    2024-06-11 08:40:04       97 阅读

热门阅读

  1. 【Python】易错点——数组;列表;内存分配

    2024-06-11 08:40:04       29 阅读
  2. NLP--朴素贝叶斯

    2024-06-11 08:40:04       24 阅读
  3. vue基础

    vue基础

    2024-06-11 08:40:04      33 阅读