From b2e39cf0d3ea11b179e4ab1f251636daf359295e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=E1=BB=93=20V=C4=83n=20H=C3=B2a?= <56647826+hovanhoa@users.noreply.github.com> Date: Sun, 5 Nov 2023 21:17:55 +0700 Subject: [PATCH] Time: 531 ms (61.41%), Space: 29.5 MB (94.02%) - LeetHub --- .../1657-find-the-winner-of-an-array-game.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 1657-find-the-winner-of-an-array-game/1657-find-the-winner-of-an-array-game.py diff --git a/1657-find-the-winner-of-an-array-game/1657-find-the-winner-of-an-array-game.py b/1657-find-the-winner-of-an-array-game/1657-find-the-winner-of-an-array-game.py new file mode 100644 index 0000000..1210fc8 --- /dev/null +++ b/1657-find-the-winner-of-an-array-game/1657-find-the-winner-of-an-array-game.py @@ -0,0 +1,21 @@ +class Solution: + def getWinner(self, arr: List[int], k: int) -> int: + if k == 1: + return max(arr[0], arr[1]) + if k >= len(arr): + return max(arr) + + current_winner = arr[0] + consecutive_wins = 0 + + for i in range(1, len(arr)): + if current_winner > arr[i]: + consecutive_wins += 1 + else: + current_winner = arr[i] + consecutive_wins = 1 + + if consecutive_wins == k: + return current_winner + + return current_winner \ No newline at end of file