aboutsummaryrefslogtreecommitdiff
path: root/find_winner_arr_game.py
blob: 33ffa39d9953ea94b597f1ef0508c9e61163b304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 1535. Find the Winner of an Array Game

"""
given an integer array 'arr' of distinct integers and an integer 'k', a game
will be played between the first two elements of the array (ie. 'arr[0]' and
'arr[1]'). in eah round of the game, we compare 'arr[0]' with 'arr[1]', the
larger integer wins and remains at position 0, and the smaller integer moves
to the end of the array. the game ends when an integer wins 'k' consecutive
rounds. return the integer which will win the game. it is guaranteed that
there will be a winner of the game.
"""


class Solution(object):
    def getWinner(self, arr, k):
        curr = arr[0]
        win = 0
        for i in range(1, len(arr)):
            if arr[i] > curr:
                curr = arr[i]
                win = 0
            win += 1
            if win == k:
                break
        return curr


if __name__ == "__main__":
    obj = Solution()
    print(obj.getWinner([2, 1, 3, 5, 4, 6, 7], 2))  # expect: 5
    print(obj.getWinner([3, 2, 1], 2))  # expect: 3