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)


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