leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1535.cpp (528B)


0 class Solution { 1 public: 2 int getWinner(vector<int> &arr, int k) const { 3 const int n = arr.size(); 4 const int maxi = *max_element(begin(arr), end(arr)); 5 if (k >= n) return maxi; 6 7 int crnt = arr[0], count = 0; 8 for (int i = 1; i < n; i++) { 9 if (crnt > arr[i]) 10 count++; 11 else { 12 crnt = arr[i]; 13 count = 1; 14 } 15 if (count == k || crnt == maxi) return crnt; 16 } 17 18 return maxi; 19 } 20 };