Leetcode 1535. Find the Winner of an Array Game (python)
程序员文章站
2022-03-23 18:31:59
...
题目
解法
用单个count来记录某个位置能赢的次数即可
class Solution:
def getWinner(self, arr: List[int], k: int) -> int:
n = len(arr)
if k>=n-1:
return max(arr)
if k==1:
return max(arr[0],arr[1])
curr_max = arr[0]
count = 0
for num in arr[1:]:
if num>curr_max:
curr_max = num
count = 1
else:
count += 1
if count==k:
return curr_max
return max(arr)