Find the Winner of an Array Game Solutions in C++
Number 1535
Difficulty Medium
Acceptance 46.0%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/find-the-winner-of-an-array-game/// Author : Hao Chen// Date : 2020-10-02class Solution {public:int getWinner(vector<int>& arr, int k) {int left=0, right=1;int max = arr[left] > arr[right] ? arr[left] : arr[right];int winner;int win_times = 0;while( right < arr.size()) {//if left < right, the move the `left` to the `right`if ( arr[left] < arr[right] ) {left = right;}// move the `right` to next elementright++;//record current round winner.int w = arr[left];if (w == winner) {//if winner is same, count++win_times++;}else{// if winner is new number, reset the count.winner = w;win_times = 1;}// if the time of win equal K, return winner.if (win_times >= k) return winner;// find the max element of this array, if k > arr.size() then return thisif (max < arr[right]) max = arr[right];}return max;}};