← Home

1535. Find the Winner of an Array Game

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
153

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1535. Find the Winner of an Array Game, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are getWinner.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 276 ms, faster than 50.00% of C++ online submissions for Find the Winner of an Array Game.
02//Memory Usage: 63.1 MB, less than 100.00% of C++ online submissions for Find the Winner of an Array Game.
03class Solution {
04public:
05    int getWinner(vector<int>& arr, int k) {
06        int n = arr.size();
07        
08        if(k >= n) return *max_element(arr.begin(), arr.end());
09        
10        int win_count = 0;
11        int max = arr[0];
12        int cur = 1;
13        
14        while(cur < n && win_count < k){
15            if(max > arr[cur]){
16                ++win_count;
17            }else{
18                win_count = 1;
19                max = arr[cur];
20            }
21            ++cur;
22        }
23        
24        return max;
25    }
26};
27
28//https://leetcode.com/problems/find-the-winner-of-an-array-game/discuss/768007/JavaC%2B%2BPython-One-Pass-O(1)-Space
29//Runtime: 268 ms, faster than 60.00% of C++ online submissions for Find the Winner of an Array Game.
30//Memory Usage: 63.1 MB, less than 100.00% of C++ online submissions for Find the Winner of an Array Game.
31class Solution {
32public:
33    int getWinner(vector<int>& arr, int k) {
34        int n = arr.size();
35        
36        if(k >= n) return *max_element(arr.begin(), arr.end());
37        
38        int winner = arr[0], win_count = 0;
39        for(int i = 1; i < n && win_count < k; ++i){
40            if(winner < arr[i]){
41                winner = arr[i];
42                win_count = 0;
43            }
44            /*
45            either the winner remains the same or changes,
46            we need to increase win_count!!
47            */
48            ++win_count;
49        }
50        
51        return winner;
52    }
53};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.