跳到主要内容

1535.找出数组游戏的赢家

链接:1535.找出数组游戏的赢家
难度:Medium
标签:数组、模拟
简介:返回赢得比赛的整数。

题解 1 - python

  • 编辑时间:2024-05-19
  • 执行用时:80ms
  • 内存消耗:27.17MB
  • 编程语言:python
  • 解法介绍:遍历时记录当前最大值和最大次数。
class Solution:
def getWinner(self, arr: List[int], k: int) -> int:
n = len(arr)
nmax = max(arr)
if k >= n: return nmax
curIndex = 0
curCount = 0
while curCount < k:
# print(f'===> curIndex = {curIndex}, curCount = {curCount}, arr = {arr}')
if arr[curIndex] == nmax: return nmax
nextIndex = (curIndex + 1) % n
if arr[curIndex] > arr[nextIndex]:
arr[curIndex], arr[nextIndex] = arr[nextIndex], arr[curIndex]
else:
curCount = 0
curCount += 1
curIndex = nextIndex
# print(f'curIndex = {curIndex}, curCount = {curCount}, arr = {arr}')
return arr[curIndex]