Skip to content

Commit 30d4dda

Browse files
authored
Create find-the-winner-of-an-array-game.cpp
1 parent ba317d0 commit 30d4dda

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int getWinner(vector<int>& arr, int k) {
7+
int result = arr[0];
8+
for (int i = 1, count = 0; i < arr.size(); ++i) {
9+
if (arr[i] > result) {
10+
result = arr[i];
11+
count = 0;
12+
}
13+
++count;
14+
if (count == k) {
15+
break;
16+
}
17+
}
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)