Problem
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 (i.e. arr[0]
and arr[1]
). In each 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.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - Simulation
To solve this problem, we need to simulate the game rounds, track the current winning streak, and check if any integer wins k
consecutive rounds.
Code
|
|
|
|
Complexity
- Time:
O(n)
- Space:
O(1)
Method 2 - Deque
We can use a Deque
(double-ended queue) for more efficient operations. Here is the improved code:
Here is the approach:
- Initialization:
- Initialize a deque with all elements of the array and set
currentWinner
to the first element. - Store the original value of
k
inoriginalK
and calculate the maximum elementmaxElement
in the array.
- Initialize a deque with all elements of the array and set
- Edge Case:
- If
k
is greater than or equal to the array size, the maximum element will naturally win. Therefore, returnmaxElement
.
- If
- Game Loop:
- Continue the game while the number of consecutive wins by
currentWinner
is less thank
. - In each iteration, dequeue the next element.
- Compare
currentWinner
with the next element:- If
currentWinner
is greater, increment theconsecutiveWins
counter and enqueue the next element to the end of the deque. - If the next element is greater, reset the
consecutiveWins
to 1, enqueuecurrentWinner
, and updatecurrentWinner
to the next element.
- If
- Continue the game while the number of consecutive wins by
- Return Result:
- Once an element wins
k
consecutive rounds, returncurrentWinner
as the winner.
- Once an element wins
Code
|
|
|
|
Complexity
- Time:
O(n + m)
, wheren
is the length of the array andm
is the number of operations required to find the winner. In the worst case, it can beO(n * k)
. - Space:
O(n)
, due to the space required to store the elements in the deque.
Method 3 - Two Pointer and Hashtable
Here is the approach:
- Initialization:
- Initialize a dictionary
wins
to keep track of the number of wins for each integer. - Initialize two pointers
i
andj
to traverse the array starting from positions0
and1
, respectively.
- Initialize a dictionary
- Game Loop:
- Continue the loop as long as both
i
andj
are within the bounds of the array. - Compare
arr[i]
andarr[j]
:- If
arr[i]
is greater, increment the win count forarr[i]
in thewins
dictionary and move to the next element by incrementingj
. - If
arr[j]
is greater, increment the win count forarr[j]
, seti
toj
, and move to the next element by incrementingj
.
- If
- If the win count for
arr[i]
equalsk
, returnarr[i]
as the winner.
- Continue the loop as long as both
- Edge Case:
- If no integer wins
k
consecutive rounds within the loop, find and return the maximum integer in the array as the winner.
- If no integer wins
- Return the Winner:
- The element with
k
consecutive wins is returned as the winner. - If no such element is found within the loop, return the maximum element in the array.
- The element with
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, wheren
is the length of the array, because each element is processed at most twice. - 🧺 Space complexity:
O(n)
, due to the dictionary used to store the number of wins for each integer.