Java | Leetcode Java题解之第135题分发糖果

题目:

题解:

java 复制代码
class Solution {
    public int candy(int[] ratings) {
        int n = ratings.length;
        int ret = 1;
        int inc = 1, dec = 0, pre = 1;
        for (int i = 1; i < n; 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;
    }
}
相关推荐
秋天的怀念_David几秒前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
Forget the Dream2 小时前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
LK_073 小时前
【力扣】142. 环形链表 II |Python
python·leetcode·链表
Pniubi3 小时前
力扣21合并两个有序链表
算法·leetcode·职场和发展
黑十3 小时前
FDE 怎么快速搭一个可演示页面?最小前端方案
java·大数据·前端·数据库·人工智能
神明不懂浪漫4 小时前
【第八章】多线程(一)
java·开发语言·经验分享·笔记
用户54817845909504 小时前
Java 与 Python 在大模型与智能体领域的选型
java·python
文丑颜不良啊4 小时前
LeetCode3341~3360题目解析
java·leetcode
hai_android4 小时前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
用户9338104348454 小时前
MyBatis-Plus LambdaQueryWrapper 隐式转换踩坑:不知不觉索引失效
java