MediumNo. 1535
Find the Winner of an Array Game — LeetCode 1535 Python Solution
- Topics
- Array, Simulation
- Reading time
- 3 min
- Source
- leetcode.com
The 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.
Example
- Input
- arr = [2,1,3,5,4,6,7], k = 2
- Output
- 5
- Explanation
- Let's see the rounds of the game:
Python solution
class Solution:
def getWinner(self, arr: List[int], k: int) -> int:
mx = arr[0]
cnt = 0
for x in arr[1:]:
if mx < x:
mx = x
cnt = 1
else:
cnt += 1
if cnt == k:
break
return mxComplexity
- Time
- O(n)where n is the length of the array
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0495 | Teemo Attacking | Easy |
| 0985 | Sum of Even Numbers After Queries | Medium |
| 1389 | Create Target Array in the Given Order | Easy |
| 1409 | Queries on a Permutation With Key | Medium |
| 1503 | Last Moment Before All Ants Fall Out of a Plank | Medium |
| 1560 | Most Visited Sector in a Circular Track | Easy |
Frequently asked questions
- How hard is LeetCode 1535. Find the Winner of an Array Game?
LeetCode 1535. Find the Winner of an Array Game is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1535. Find the Winner of an Array Game?
The Python solution on this page runs in O(n), where n is the length of the array.
- What is the space complexity of LeetCode 1535. Find the Winner of an Array Game?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1535. Find the Winner of an Array Game cover?
LeetCode 1535. Find the Winner of an Array Game is tagged Array and Simulation on LeetCode.