We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bcc80bf commit e0b6989Copy full SHA for e0b6989
Medium/Find the Winner of an Array Game.java
@@ -1,17 +1,16 @@
1
class Solution {
2
- public int getWinner(int[] arr, int k) {
3
- int currentElement = arr[0];
4
- int n = arr.length;
5
- int currentWinCount = 0;
6
- for (int i = 1; i < n; i++) {
7
- if (arr[i] > currentElement) {
8
- currentElement = arr[i];
9
- currentWinCount = 0;
10
- }
11
- if (++currentWinCount == k) {
12
- break;
13
+ public int getWinner(int[] arr, int k) {
+ int curr = arr[0];
+ int streak = 0;
+ for (int i = 1; i < arr.length; i++) {
+ if (curr < arr[i]) {
+ curr = arr[i];
+ streak = 0;
+ }
+ if (++streak == k) {
+ break;
14
+ return curr;
15
}
- return currentElement;
16
17
0 commit comments