candy
来源:互联网 发布:腾讯云域名实名认证 编辑:程序博客网 时间:2024/06/05 15:36
先从左向右扫描一遍,如果当前小盆宇比左边的小盆宇评分高,就多给他一颗糖,然后再从右向左扫描一遍。
class Solution {public: int candy(vector<int> &ratings) { int res = 0; int n = ratings.size(); if (n == 0) { return res; } int *t = new int[n]; for (int i = 0; i < n; ++i) { t[i] = 1; } for (int i = 1; i < n; ++i) { if (ratings[i] > ratings[i-1]) { t[i] = t[i-1] + 1; } } for (int i = n - 1; i >= 1; --i) { if (ratings[i] < ratings[i-1]) { t[i-1] = (t[i] + 1) > t[i-1] ? (t[i] + 1) : t[i-1]; } } for (int i = 0; i < n; ++i) { res += t[i]; } return res; }};
0 0
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- candy
- Candy
- Candy
- Candy
- Candy
- 考试系统维护--不同版本SQL数据导入导出
- Qt 2048
- POJ 题目1995 Raising Modulo Numbers(快速幂)
- 大仙说道之Android studio实现Service AIDL
- (DS 《算法竞赛入门经典》)LA 3644 X-Plosives(并查集)
- candy
- Regular Expression Matching
- 锋利的SQL2014:使用NULL比较搜索条件
- 锋利的SQL2014:SQL的同时操作概念
- pat:hello world test
- IOS开发的一些细微知识点
- <div>手绘页面布局(上下左右中)
- cocos2d-x CCControlButton,九妹图用按钮
- IMread和cvloadimage