欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > Leetcode 3175. Find The First Player to win K Games in a Row

Leetcode 3175. Find The First Player to win K Games in a Row

2025/9/25 15:55:02 来源:https://blog.csdn.net/codename_cys/article/details/139565893  浏览:    关键词:Leetcode 3175. Find The First Player to win K Games in a Row
  • Leetcode 3175. Find The First Player to win K Games in a Row
    • 1. 解题思路
    • 2. 代码实现
  • 题目链接:3175. Find The First Player to win K Games in a Row

1. 解题思路

这一题我的解答比较暴力,基本就是暴力解答,唯一优化的就是对于特殊情况进行了一下剪枝,具体来说的话,如果k大于长度n,那么显然最后首先达到胜利条件的一定是最大的那个元素,而对于其他的情况,我就暴力求解了。

2. 代码实现

给出python代码实现如下:

class Solution:def findWinningPlayer(self, skills: List[int], k: int) -> int:players = [[skill, i, 0] for i, skill in enumerate(skills)]if k >= len(players):return max(players)[1]while players[0][2] < k:if players[0][0] < players[1][0]:players[1][2] += 1players.append(players.pop(0))else:players[0][2] += 1players.append(players.pop(1))return players[0][1]

提交代码评测得到:耗时7429ms,占用内存37.3MB。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词