原题地址:https://oj.leetcode.com/problems/candy/

题意:

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?

解题思路:求最少的蛋糕数。先从前到后扫描一遍数组,如果序列递增,就+1;然后从后到前扫描一遍数组,序列递增,+1。保证最低谷(ratings最小)永远是1就可以了。

代码:

class Solution:
# @param ratings, a list of integer
# @return an integer
def candy(self, ratings):
candynum = [1 for i in range(len(ratings))]
for i in range(1, len(ratings)):
if ratings[i] > ratings[i-1]:
candynum[i] = candynum[i-1] + 1
for i in range(len(ratings)-2, -1, -1):
if ratings[i+1] < ratings[i] and candynum[i+1] >= candynum[i]:
candynum[i] = candynum[i+1] + 1
return sum(candynum)

[leetcode]Candy @ Python的更多相关文章

  1. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  2. LeetCode专题-Python实现之第27题:Remove Element

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  3. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  4. LeetCode专题-Python实现之第21题:Merge Two Sorted Lists

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  5. LeetCode专题-Python实现之第20题:Valid Parentheses

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  6. LeetCode专题-Python实现之第9题:Palindrome Number

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  8. LeetCode专题-Python实现之第13题:Roman to Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. LeetCode专题-Python实现之第7题:Reverse Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

随机推荐

  1. SCTF2018-Event easiest web - phpmyadmin

    6月19日的SCTF的web送分题. 打开链接是一个phpmyadmin的登陆界面,尝试用默认账号:root  密码:root登陆 于是直接进去了,首先看下数据库,除了些初始化的库以外,abc这个库比 ...

  2. 初识thinkphp(2)

    thinkphp的url路径的表示格式为 http://ip/tp/public/index.php/模块/控制器/操作 这里url最后的操作就是类里面的函数. 0x01:url访问格式 官方文档中有 ...

  3. Android-Selector不起作用

    Android-Selector不起作用 Overview 今天在做项目的时候,使用了一些 Selector 来给ImageView设置不同的Drawable,但是无论怎么设置ImageView的属性 ...

  4. RAR压缩包审计工具unrar-nofree

    RAR压缩包审计工具unrar-nofree   RAR是常见的一种压缩包格式,广泛应用于Windows系统下.Kali Linux提供一款专用的审计工具unrar-nofree.该工具由WinRAR ...

  5. vue组件之间通信传值

    原文链接:https://blog.csdn.net/lander_xiong/article/details/79018737 我认为这位博主的这篇文章写的非常详细,通俗易懂, 我们这次的vue项目 ...

  6. android Service oncreate 在UI线程 何时用service,何时用thread

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 服务的生命周期 各个方法 都是在主线程中的. 这里的操作可以导致主线程阻塞. 这些方法, ...

  7. 牛客网某比赛 I 小乐乐学博弈 博弈论

    题目大意: 有两堆石子\(n\)和\(m\),每次可以拿\(1 \sim k\)个 \(k >= |n - m|\) 问先手必胜? 把限制条件去掉才有意思 首先考虑两堆相等,那么先手怎么操作,后 ...

  8. spring-boot 速成(9) druid+mybatis 多数据源及读写分离的处理

    按上节继续学习,稍微复杂的业务系统,一般会将数据库按业务拆开,比如产品系统的数据库放在product db中,订单系统的数据库放在order db中...,然后,如果量大了,可能每个库还要考虑做读.写 ...

  9. There are no packages available for install

    解决方法: ·删除sublime Text 安装目录下Data->Packages目录下的Package Control(如果没有,略过此步骤). ·下载Package Control,下载路径 ...

  10. SOC 与 ARM

    SOC是指片上系统,意思是一个芯片就构成一个包括了存储.CPU.甚至还有AD.UART等等其他资源的系统!而ARM只是CPU的一种,有的片上系统是51.nios.PIC.等等不一而是!特别是nios, ...