本文主要是介绍C++ | Leetcode C++题解之第135题分发糖果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution {
public:int candy(vector<int>& ratings) {int n = ratings.size();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;}
};
这篇关于C++ | Leetcode C++题解之第135题分发糖果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!