Codewars Vasya - Clerk–6 kyu–Python解法


Codewars 是一个跟LeetCode类似的结题网站。
Codewars题解专栏:Codewars题解


题目地址:Train: Vasya - Clerk | Codewars


The new “Avengers” movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 dollar bill. An “Avengers” ticket costs 25 dollars.

Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.

Can Vasya sell a ticket to every person and give change if he initially has no money and sells the tickets strictly in the order people queue?

Return YES, if Vasya can sell a ticket to every person and give change with the bills he has at hand at that moment. Otherwise return NO.

Examples:

tickets([25, 25, 50]) # => YES
tickets([25, 100]) # => NO. Vasya will not have enough money to give change to 100 dollars
tickets([25, 25, 50, 50, 100]) # => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)

这道题目是6kyu难度的,比较简单。
Python解法如下:

def tickets(people):l=[0,0]for i in people:if i==25:l[0]+=1elif i==50:if l[0]==0:return "NO"else:l[0]-=1l[1]+=1else:if l[1]>=1:if l[0]==0:return "NO"else:l[0]-=1l[1]-=1else:if l[0]<3:return "NO"else:l[0]-=3return "YES"

上面的解法是比较啰嗦的,简化后别人的解法参考如下:

def tickets(a):n25 = n50 = n100 = 0for e in a:if   e==25            : n25+=1elif e==50            : n25-=1; n50+=1elif e==100 and n50>0 : n25-=1; n50-=1elif e==100 and n50==0: n25-=3if n25<0 or n50<0:return 'NO'return 'YES'

Codewars Vasya - Clerk--6 kyu--Python解法相关推荐

  1. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

  2. LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法

    题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...

  3. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

  4. LeetCode 458. Poor Pigs--智力题「小白鼠试毒」--C++,Python解法

    题目地址:Poor Pigs - LeetCode There are 1000 buckets, one and only one of them is poisonous, while the r ...

  5. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  6. LeetCode 221. Maximal Square----动态规划--谷歌面试算法题--Python解法

    题目地址:Maximal Square - LeetCode Given a 2D binary matrix filled with 0's and 1's, find the largest sq ...

  7. LeetCode 148. Sort List--面试算法题--C++,Python解法

    LeetCode 148. Sort List–面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大 ...

  8. LeetCode 20. Valid Parentheses--笔试题--Python解法

    题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...

  9. LeetCode 145. Binary Tree Postorder Traversal--后序遍历--先序遍历反向输出--递归,迭代--C++,Python解法

    题目地址:Binary Tree Postorder Traversal - LeetCode Given a binary tree, return the postorder traversal ...

最新文章

  1. linux shell 字符串 文件内容 大小写 转换 替换
  2. 深度学习核心技术精讲100篇(三十五)-美团餐饮娱乐知识图谱——美团大脑揭秘
  3. [开发笔记]-winfom ListBox控件选中项上下移动排序
  4. python查找路径代码_Python搜索路径
  5. Mybatis获取已经赋值,没有问号的Sql
  6. 复变函数、运筹学与概率论知识总结
  7. 代理ip池的ip是重复利用的吗_爬虫被封怎么办?用Node.js构建一个私人IP代理池...
  8. 通信——CAN总线基础介绍
  9. 学术论文写作之引言(Introduction)怎么写
  10. win7网上邻居_win7网上邻居寻找教程
  11. winrar命令行加压解密
  12. 有吧友需要PDF的下载站点,好吧,我这边汇总一下
  13. 广东未来科技|民族科技助力立体显示终端产业集群崛起
  14. Linux内核自带SPI设备驱动测试程序分析:spidev_test.c
  15. IMP-00017: following statement failed with ORACLE error 20005:(表统计信息被锁)
  16. 特征值是否重根与特征向量及基础解系的关系
  17. 音视频开发之旅(40)-贝塞尔曲线和曲面
  18. 【周鸿祎】给创业者的三个建议:融…
  19. 智慧停车之 IOT选型-电信物联网
  20. Linux RS232/485/GPS 驱动实验(2)-UART 驱动分析

热门文章

  1. mysql存储引擎静态表_MySQL存储引擎(表类型)的选择
  2. 入门 Python GUI 开发的第一个坑
  3. R语言与数据分析(9)R与Excel
  4. Nature综述——真菌的多样性:真菌的高通量测序及鉴定
  5. QIIME1.9.1-2使用Docker运行QIIME
  6. 扩增子文献笔记2拟南芥根微生物组的结构和组成
  7. R语言层次聚类:通过内平方和WSS选择最优的聚类K值、可视化不同K下的BSS和WSS、通过Calinski-Harabasz指数(准则)与聚类簇个数的关系获取最优聚类簇的个数
  8. R语言可视化包ggplot2改变图例(legend)的标题(title)实战
  9. R语言筛选dataframe分组中值最大的行实战(Select the row with the maximum value in each group)
  10. R语言exists函数检查数据对象是否存在实战