Skip to content

Commit

Permalink
daily: May19, array traverse [E]
Browse files Browse the repository at this point in the history
simply traverse the array
  • Loading branch information
aucker committed May 19, 2024
1 parent b5a9afb commit 681ec7f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions daily/May19.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
/**
* @brief Get the Winner object, LC: 1535
* Time: O(N) traverse once
* Space: O(1)
*
* @param arr, k
* @return int
*/
int getWinner(vector<int>& arr, int k) {
int cur = arr[0], win = 0;
for (int i = 1; i < arr.size(); i++) {
if (arr[i] > cur) {
cur = arr[i];
win = 0;
}

if (++win == k) {
break;
}
}

return cur;
}
};

0 comments on commit 681ec7f

Please sign in to comment.