【leetcode】1046. Last Stone Weight
题目如下:
We have a collection of rocks, each rock has a positive integer weight.
Each turn, we choose the two heaviest rocks and smash them together. Suppose the stones have weights
xandywithx <= y. The result of this smash is:
- If
x == y, both stones are totally destroyed;- If
x != y, the stone of weightxis totally destroyed, and the stone of weightyhas new weighty-x.At the end, there is at most 1 stone left. Return the weight of this stone (or 0 if there are no stones left.)
Example 1:
Input: [2,7,4,1,8,1]
Output: 1
Explanation:
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone.Note:
1 <= stones.length <= 301 <= stones[i] <= 1000
解题思路:非常简单的题目,每次取最大的两个数,如果存在多个最大的两个数,随机取其中两个即可。
代码如下:
class Solution(object):
def lastStoneWeight(self, stones):
"""
:type stones: List[int]
:rtype: int
"""
while len(stones) > 1:
max_index = 0
second_max_index = 0
ol = sorted(stones)
for i in range(len(stones)):
if ol[-1] == stones[i]:
max_index = i
elif ol[-2] == stones[i] and max_index != i:
second_max_index = i
if stones[max_index] == stones[second_max_index]:
if max_index > second_max_index:
del stones[max_index]
del stones[second_max_index]
else:
stones[max_index] -= stones[second_max_index]
del stones[second_max_index]
#print stones
return 0 if len(stones) == 0 else stones[0]
【leetcode】1046. Last Stone Weight的更多相关文章
- 【LeetCode】1046. Last Stone Weight 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆 日期 题目地址:https://leetco ...
- 【Leetcode_easy】1046. Last Stone Weight
problem 1046. Last Stone Weight 参考 1. Leetcode_easy_1046. Last Stone Weight; 完
- 【leetcode】339. Nested List Weight Sum
原题 Given a nested list of integers, return the sum of all integers in the list weighted by their dep ...
- 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】339. Nested List Weight Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 dfs 日期 题目地址:https://leetcod ...
- LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50
1046. 最后一块石头的重量 1046. Last Stone Weight 题目描述 每日一算法2019/6/22Day 50LeetCode1046. Last Stone Weight Jav ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】486. Predict the Winner 解题报告(Python)
[LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
随机推荐
- linux sed 替换,使用变量,变量中含有特殊字符的处理
文件 test.sh 内容如下: #!/bin/bash export JAVA_HOME=/data/jdk_1.9.0 echo $JAVA_HOME 想把 JAVA_HOME = 后面的内容替换 ...
- r hive
w r只能处理有限量的数据 pdf 467
- 算法所产生的性能改进已经超过了硬件所带来的性能提升 The future is algorithms, not code
w 大数据的发展,伴随的将是软件工程师的渐退,算法工程师的崛起 http://mp.weixin.qq.com/s/XTz2HnzwlgTD3g5zU2u5Tg
- JS-Array.prototype 中的方法的坑
fill() 今天刷 HackerRank 的题遇到需要创建链表数组(一维数组的每一项是个链表)的题. 众所周知 JS 中的数组可以当链表用,我就用如下代码进行创建 let seqs = (new A ...
- day58—JavaScript面向对象(一)
转行学开发,代码100天——2018-05-13 “面向对象”对于学习过C++及JAVA的开发者来说并不陌生.有意思的是面向对象的思路可以用于面对或解决生活工作中的其他问题,简单地说就是“只关注功能, ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_10_字节输入流一次读取一个字节的原理
原理解析 创建一个字节流,指向读取文件的第一个字节. read找jvm,jvm找os.os去读取硬盘.,读取后指正向后移动一位
- lombok 简化 get set toString hash equals等方法
1.lombok 在项目中使用Lombok可以减少很多重复代码的书写.比如说getter/setter/toString等方法的编写. 2.安装 下载 https://projectlombok.or ...
- PHP上传文件到阿里云OSS,nginx代理访问
1. 阿里云OSS创建存储空间Bucket(读写权限为:公共读) 2. 拿到相关配置 accessKeyId:********* accessKeySecret:********* endpoint: ...
- 使用autofac的一些问题
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on ...
- Poj 3268 Silver cow party 迪杰斯特拉+反向矩阵
Silver cow party 迪杰斯特拉+反向 题意 有n个农场,编号1到n,每个农场都有一头牛.他们想要举行一个party,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...