剩下最后的石头

该博客介绍了如何运用Java实现一个优先队列来解决LeetCode中的'最后石头的重量'问题。通过建立大根堆,每次选取两块最重的石头进行粉碎,并根据题目规则更新堆。最终返回堆中剩余的一块石头的重量,如果没有石头则返回0。此方法巧妙地利用了数据结构解决算法问题。
摘要由CSDN通过智能技术生成

有一堆石头,每块石头的重量都是正整数。

每一回合,从中选出两块 最重的 石头,然后将它们一起粉碎。假设石头的重量分别为 x 和 y,且 x <= y。那么粉碎的可能结果如下:

如果 x == y,那么两块石头都会被完全粉碎;
如果 x != y,那么重量为 x 的石头将会完全粉碎,而重量为 y 的石头新重量为 y-x。
最后,最多只会剩下一块石头。返回此石头的重量。如果没有石头剩下,就返回 0。

链接:https://leetcode-cn.com/problems/last-stone-weight

思路 :建立大根堆(利用比较器重写优先级队列的规则) ,然后每次依照题目的处理办法对堆顶和堆第二个元素进行处理,

注意:return queue.size()==1 ? queue.peek() : 0;

class Solution {
public int lastStoneWeight(int[] stones) {
if (stones.length==0) return -1;
Queue queue = new PriorityQueue<>(new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
for (int i = 0; i < stones.length; i++) {
queue.offer(stones[i]);
}
while (queue.size() > 1){
int tmp = queue.poll();
if (queue.isEmpty()) return tmp;
if (tmp == queue.peek()){
queue.poll();
}else if (tmp > queue.peek()){
queue.offer(tmp-queue.poll());
}
}
return queue.size()==1 ? queue.peek() : 0;
}
}

以下是实现代码: ```python import random def black_white_fist(players): """ 黑白配猜拳 """ white = '手心' black = '手背' while len(players) > 1: fists = [] for player in players: if len(player) > 1: # 工 choice = input(f"{player}请出拳('手心'或'手背'):") while choice not in [white, black]: choice = input(f"无效的选择,请重新出拳('手心'或'手背'):") fists.append(choice) else: # 剩下的人不再猜拳 fists.append(None) if len(fists) != len(set(fists)): # 出现相同的拳,继续猜拳 print("平局,继续出拳!") else: # 比较结果,淘汰一方 win_fist = white if fists.count(black) > fists.count(white) else black lose_player = [player for index, player in enumerate(players) if fists[index] != win_fist][0] print(f"{win_fist}胜利,{lose_player}被淘汰!") players.remove(lose_player) print(f"{players[0]}胜利!") def rock_scissors_paper(players, trash_cans): """ 石头剪刀布猜拳 """ while len(players) > 1: pairs = random.sample(players, 2) print(f"{pairs[0]}和{pairs[1]}进行石头剪刀布猜拳:") choice1 = input(f"{pairs[0]}请出拳('石头'、'剪刀'或'布'):") while choice1 not in ['石头', '剪刀', '布']: choice1 = input(f"无效的选择,请重新出拳('石头'、'剪刀'或'布'):") choice2 = input(f"{pairs[1]}请出拳('石头'、'剪刀'或'布'):") while choice2 not in ['石头', '剪刀', '布']: choice2 = input(f"无效的选择,请重新出拳('石头'、'剪刀'或'布'):") if (choice1 == '石头' and choice2 == '剪刀') or (choice1 == '剪刀' and choice2 == '布') or (choice1 == '布' and choice2 == '石头'): # pairs[0] 胜利 print(f"{pairs[0]}胜利!") players.remove(pairs[1]) elif choice1 == choice2: # 平局,继续猜拳 print("平局,继续猜拳!") else: # pairs[1] 胜利 print(f"{pairs[1]}胜利!") players.remove(pairs[0]) # 挑选倒垃圾的人 if len(trash_cans) == 2: print(f"{players[0]}和{players[1]}各负责一个垃圾桶!") else: while True: choice1 = input(f"{players[0]}请出拳('石头'、'剪刀'或'布'):") while choice1 not in ['石头', '剪刀', '布']: choice1 = input(f"无效的选择,请重新出拳('石头'、'剪刀'或'布'):") choice2 = input(f"{players[1]}请出拳('石头'、'剪刀'或'布'):") while choice2 not in ['石头', '剪刀', '布']: choice2 = input(f"无效的选择,请重新出拳('石头'、'剪刀'或'布'):") if (choice1 == '石头' and choice2 == '剪刀') or (choice1 == '剪刀' and choice2 == '布') or (choice1 == '布' and choice2 == '石头'): print(f"{players[1]}胜利,负责倒垃圾!") break elif choice1 == choice2: # 平局,继续猜拳 print("平局,继续猜拳!") else: print(f"{players[0]}胜利,负责倒垃圾!") break ``` 调用示例: ```python players = ['小明', '小红', '小刚', '小李'] trash_cans = ['垃圾桶1', '垃圾桶2'] if len(players) > 4: black_white_fist(players) elif len(players) == 4: rock_scissors_paper(players, trash_cans) elif len(players) == 3: players.append("随机选手") rock_scissors_paper(players, trash_cans) elif len(players) == 2: print(f"{players[0]}和{players[1]}各负责一个垃圾桶!") else: print("人数不足,无法进行猜拳!") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值