当前位置:网站首页>Niuke.com: Candy distribution
Niuke.com: Candy distribution
2022-06-25 11:24:00 【lsgoose】


This question is numb , At least... Is given in the stem , In fact, it is obvious to use greed .
So how can we be greedy , as everyone knows , At least it is 1, We naturally think of , Next to him , If you're older than him , Then add at least one to ensure the minimum , With this concept , We can talk about the idea of this problem .
Let's first look from left to right , Assume that everything is as long as 1, If the current number is larger than the previous number , So we just add one to the minimum number of candies , Otherwise, it will not be dealt with .
It's obvious that , We cannot guarantee that if the current number is larger than the following , But the number of sweets is also bigger than that of the later , Because we only looked at the comparison between the current number and the previous number for the first time .
To fix , We need to look again from right to left , If the current number is larger than the right , And the number of sweets is less than or equal to that on the right ( The reason for taking equals is that we didn't deal with the equals sign in the first traversal )
The code is as follows :
class Solution {
public:
/**
* pick candy
* @param arr int integer vector the array
* @return int integer
*/
int candy(vector<int>& arr) {
// write code here
int n=arr.size();
vector<int> num(n, 1);
for(int i=1;i<n;++i){
if(arr[i]>arr[i-1]){
num[i]=num[i-1]+1;
}
}
int res=num[n-1];
for(int i=n-2;i>=0;i--){
if(arr[i]>arr[i+1] && num[i]<num[i+1]){
num[i]=num[i+1]+1;
}
res+=num[i];
}
return res;
}
};边栏推荐
- Technical practice and development trend of video conference all in one machine
- Jincang KFS data cascade scenario deployment
- 金仓数据库 KingbaseES 插件dbms_session
- Detailed explanation of Spark's support source code for Yan priority
- 牛客网:主持人调度
- Upload and modify the use of avatars
- 基于超算平台气象预警并行计算架构研究
- 基於Minifilter框架的雙緩沖透明加解密驅動 課程論文+項目源碼
- C disk uses 100% cleaning method
- GaussDB others内存比较高的场景
猜你喜欢

Redis6 note02 configuration file, publish and subscribe, new data type, jedis operation

Design and implementation of university laboratory goods management information system based on SSH

SQL注入漏洞(繞過篇)

FPGA displays characters and pictures based on VGA

开源社邀请您参加OpenSSF开源安全线上研讨会

中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析

Introduction to JVM principle

Explanation and use of kotlin syntax for Android

Double buffer transparent encryption and decryption driven course paper + project source code based on minifilter framework

Technical practice and development trend of video conference all in one machine
随机推荐
西山科技冲刺科创板:拟募资6.6亿 郭毅军夫妇有60%表决权
Writing wechat applet with uni app
子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?
Network remote access using raspberry pie
COSCon'22 讲师征集令
SQL injection vulnerability (bypass)
PHP如何提取字符串中的图片地址
建造者模式
JVM 原理简介
Tidb applicable scenarios
CMU puts forward a new NLP paradigm - reconstructing pre training, and achieving 134 high scores in college entrance examination English
基于超算平台气象预警并行计算架构研究
时创能源冲刺科创板:拟募资11亿 年营收7亿净利反降36%
杭州/北京内推 | 阿里达摩院招聘视觉生成方向学术实习生(人才计划)
Hangzhou / Beijing neitui Ali Dharma academy recruits academic interns in visual generation (talent plan)
今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
如何实现移动端富文本编辑器功能
Multiple environment variables
视频会议一体机的技术实践和发展趋势
中国信通院沈滢:字体开源协议——OFL V1.1介绍及合规要点分析