leetcode-1535-找出数组游戏的赢家

题目描述:

 

 

 

 

 

 方法一:双指针 O(N) O(1)

class Solution:
    def getWinner(self, arr: List[int], k: int) -> int:
        strike = 0
        cur, i = 0, 1
        while i < len(arr):
            if arr[i] < arr[cur]:
                i += 1
                strike += 1
                if strike == k:
                    return arr[cur]
            else:
                cur = i
                i += 1
                strike = 1
                if strike == k:
                    return arr[cur]
        return arr[cur]

 

posted @ 2020-08-04 23:35  oldby  阅读(115)  评论(0编辑  收藏  举报