当前位置:网站首页>BM95 分糖果问题
BM95 分糖果问题
2022-06-21 16:05:00 【程序员·小李】
一群孩子做游戏,现在请你根据游戏得分来发糖果,要求如下:
1. 每个孩子不管得分多少,起码分到一个糖果。
2. 任意两个相邻的孩子之间,得分较多的孩子必须拿多一些糖果。(若相同则无此限制)
给定一个数组 arrarr 代表得分数组,请返回最少需要多少糖果。
要求: 时间复杂度为 O(n)O(n) 空间复杂度为 O(n)O(n)
每个人至少一个。
从左向右遍历一次,满足右侧的人。(类似于传声筒,从左向右遍历时,只关心是不是比自己左侧的高)

从右向左遍历一次,满足左侧的人。(类似于传声筒,从右向左遍历时,只关心是不是比自己右侧的高)

public int candy (int[] arr) {
// write code here
int[] result = new int[arr.length];
// 每人至少一个
for (int i = 0 ; i < result.length; i++){
result[i] = 1;
}
// 右边的比左边的分值高,得多分一个
for (int i = 1 ; i < arr.length; i++){
if (arr[i] > arr[i-1]){
result[i] = result[i-1] + 1;
}
}
// 左边的比右边的分值高
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;
}注:从右向左遍历时,需要注意左侧分得的糖果已经比右侧大的可能性。
边栏推荐
- 3M mutual aid intelligent contract system development and construction technology
- 如何写好技术文档 Software Engineering at Google
- 还在用 Xshell ?试试这款炫酷的 SSH 终端工具吧,功能很强大!
- xlrd寻找指定内容所在行与行内容
- 第13周总结博客(校历第15周)动态规划总结
- Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales bull?
- 应用架构原则
- Template: p6114 [template] Lyndon Decomposition & runs (string)
- Function call model
- Sequence traversal of binary tree
猜你喜欢

Vector data download for mainland and global epidemic data, based on geo JSON to SHP

The next stop of Intelligent Manufacturing: cloud native + edge computing two wheel drive

PowerPoint tutorial, how to change page orientation and slide size in PowerPoint?

南京大学 静态软件分析(static program analyzes)-- introduction 学习笔记

Niuke network: verify the IP address

Redis6.0新特性(上)

module.exports指向问题

Notice on the third national operation research / data, model and decision-making course teaching seminar in 2022

Beaucoup de sociétés de logiciels sont en fait des "blagues"

牛客网:验证IP地址
随机推荐
What does container cloud mean? What is the difference with fortress machine?
Garbage collector
Hairui technology completed the pre-A round of financing of tens of millions of yuan to build the first artificial intelligent distribution Internet of things in China
如何写好技术文档 Software Engineering at Google
The fundamental task of Natural Science
即将步入大四,开始我最真情的告白
Leetcode 25: a group of K flipped linked lists
Not this year's 618?
List set map in kotlin
QT knowledge: using the qgraphicspixmapitem class
【Leetcode】297. Serialization and deserialization of binary tree (difficult)
[issue 349] Interviewer: how to gracefully customize the ThreadPoolExecutor thread pool?
Design and implementation of face verification system for floating population management
一些细节
Android kotlin 类委托 by,by lazy关键
NLog自定义Target之MQTT
3M mutual aid intelligent contract system development and construction technology
In the "roll out" era of Chinese games, how can small and medium-sized manufacturers solve the problem of going to sea?
Notice on the third national operation research / data, model and decision-making course teaching seminar in 2022
go corn定时任务简单应用