当前位置:网站首页>Bm95 points candy problem
Bm95 points candy problem
2022-06-21 17:37:00 【Programmer Xiao Li】
A group of children play games , Now please give out candy according to the score of the game , Requirements are as follows :
1. No matter how much each child scores , At least one candy .
2. Between any two adjacent children , Children who score more must take more candy .( If the same, there is no such restriction )
Given an array arrarr Represents the score array , Please return the minimum number of sweets needed .
requirement : The time complexity is O(n)O(n) The space complexity is O(n)O(n)
Everyone has at least one .
Traverse once from left to right , Meet the people on the right .( Similar to a microphone , When traversing from left to right , Only care about whether it is higher than the one on your left )

Traverse once from right to left , Meet the people on the left .( Similar to a microphone , From right to left , Only care about whether it is higher than the one on your right )

public int candy (int[] arr) {
// write code here
int[] result = new int[arr.length];
// At least one per person
for (int i = 0 ; i < result.length; i++){
result[i] = 1;
}
// The score on the right is higher than that on the left , One more point
for (int i = 1 ; i < arr.length; i++){
if (arr[i] > arr[i-1]){
result[i] = result[i-1] + 1;
}
}
// The score on the left is higher than that on the right
for (int i = arr.length - 1; i > 0; i--){
if (arr[i - 1] > arr[i]){
result[i - 1] = result[i-1] < result[i] + 1 ? result[i] + 1 : result[i-1];
}
}
int sum = 0;
for (int i = 0 ; i < result.length; i++){
sum += result[i];
}
return sum;
}notes : From right to left , It should be noted that the left side is more likely to get candy than the right side .
边栏推荐
- 软件测试体系学习及构建(14)-测试基础之软件测试和开发模型概述
- Vscade tool
- 常见设置模式
- AttributeError: module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘
- The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
- Oracle JDBC Driver
- 数据类型
- Four areas of telephone memory
- Implementation of decode function in GP
- node服务器 res.end()中写中文,客户端中乱码问题的解决方法
猜你喜欢
随机推荐
node服务器 res.end()中写中文,客户端中乱码问题的解决方法
自然科学的根本任务
第八章 可编程接口芯片及应用【微机原理】
Uni app framework learning notes
From Beijing "moisten" to Chicago, engineer Baoyu "moisten" the secret of growth
Kotlin DSL build
程序员进修之路
PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分
Compose 中的附带效应
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
MySQL 1055错误-this is incompatible with sql_mode=only_full_group_by解决方案
AS 3744.1标准中提及ISO8191测试,两者测试一样吗?
经纬度转换为距离
Accélérer le déploiement de l'application Native Cloud et compléter l'authentification de compatibilité entre Yanrong yrcloudfile et Tianyi Cloud
Implementation of decode function in GP
硅橡胶玻纤管EN45545防火试验的难易程度
The new razor component supports proxy connection to RDP, and jumpserver fortress v2.23.0 is released
[MySQL learning notes 14] DQL statement execution sequence
BM19 寻找峰值
JetPack compose 状态提升(二)







![[MySQL learning notes 19] multi table query](/img/e6/4cfb8772e14bc82c4fdc9ad313e58a.png)

