力扣——candy (分糖果) python实现
题目描述:
中文:
老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。
你需要按照以下要求,帮助老师给这些孩子分发糖果:
每个孩子至少分配到 1 个糖果。
相邻的孩子中,评分高的孩子必须获得更多的糖果。
那么这样下来,老师至少需要准备多少颗糖果呢?
英文:
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
class Solution(object):
def candy(self, ratings):
"""
:type ratings: List[int]
:rtype: int
"""
s = 0
n=len(ratings)
s+=n
tmp =[0]*n
for i in range(1,n):
if ratings[i]>ratings[i-1]:
tmp[i] = tmp[i-1]+1
for i in range(n-2,-1,-1):
if ratings[i]>ratings[i+1]:
tmp[i]=max(tmp[i],tmp[i+1]+1)
s+=sum(tmp)
return s
题目来源:力扣
力扣——candy (分糖果) python实现的更多相关文章
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [LintCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode(力扣)——Search in Rotated Sorted Array2 搜索旋转排序数组 python实现
题目描述: python实现 Search in Rotated Sorted Array2 搜索旋转排序数组 中文: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0 ...
- LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现
题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组 中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1 ...
- 力扣—Reorder List(重排链表)python实现
题目描述: 中文: 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点 ...
- 力扣 ——Linked List Cycle II(环形链表 II) python实现
题目描述: 中文: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). ...
- 力扣——Linked List Cycle(环形链表) python实现
题目描述: 中文: 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. ...
- 力扣——Copy List with Random Pointer(复制带随机指针的链表) python实现
题目描述: 中文: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. 示例: 输入:{"$id":" ...
随机推荐
- bzoj4036 [HAOI2015]按位或 状压DP + MinMax 容斥
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4036 题解 变成 \(2^n-1\) 的意思显然就是每一个数位都出现了. 那么通过 MinMa ...
- python tkinter的Label
from tkinter import * window=Tk() window.title("my first window") window.geometry("50 ...
- 【JVM】吞吐量与延迟关系
堆内存增大,gc一次能处理的数量变大,吞吐量大:但是gc一次的时间会变长,导致后面排队的线程等待时间变长: 向反,如果堆内存小,gc一次时间短,排队等待的线程等待时间变短,延迟减少,但一次请求的数量变 ...
- Git项目实战篇
版本控制器:Git # 达到多人协同开发的目的 安装 """1.下载对应版本:https://git-scm.com/download2.安装git:在选取安装路径的下一 ...
- java 节点流(字符流,字节流)和包装流(缓冲流,转换流)
结点流:直接对File类进行操作的文件流 package stream; import java.io.File; import java.io.FileNotFoundException; impo ...
- hdu 5181 numbers——思路+区间DP
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5181 题解:https://www.cnblogs.com/Miracevin/p/10960717.ht ...
- LOJ 3058 「HNOI2019」白兔之舞——单位根反演+MTT
题目:https://loj.ac/problem/3058 先考虑 n=1 怎么做.令 a 表示输入的 w[1][1] . \( ans_t = \sum\limits_{i=0}^{L}C_{L} ...
- HTTP的FormData和Payload的传输格式
FormData和Payload是浏览器传输给接口的两种格式,这两种方式浏览器是通过Content-Type来进行区分的(了解Content-Type),如果是 application/x-www-f ...
- Xcode cannot run using the selected device after upgrade
Please follow below step 1>Go to Project Build setting 2>Change compiler for c/c++/objective c ...
- Gym10198-Mediocre String Problem-2018南京ICPC现场赛
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog @ Problem:传送门 Portal 原题目描述在最下面. Solu ...