# LEETCODE 1535. Find the Winner of an Array Game 解题思路分析

```输入：arr = [2,1,3,5,4,6,7], k = 2

Round |       arr       | winner | win_count
1   | [2,1,3,5,4,6,7] | 2      | 1
2   | [2,3,5,4,6,7,1] | 3      | 1
3   | [3,5,4,6,7,1,2] | 5      | 1
4   | [5,4,6,7,1,2,3] | 5      | 2

```输入：arr = [3,2,1], k = 10

```输入：arr = [1,9,8,2,3,7,6,4,5], k = 7

```输入：arr = [1,11,22,33,44,55,66,77,88,99], k = 1000000000

```public int getWinner(int[] arr, int k) {
// 当数组长度为2或者k值为1时，那么获胜的数字一定是前两个数字中的较大者
if(arr.length==2||k==1) return Math.max(arr[0],arr[1]);
// 当前最大值
int max=Math.max(arr[0],arr[1]);
int win=1; // 连胜次数
for(int i=2;i<arr.length;i++){ // 从第三个数向后循环
if(arr[i]<max){ // 当前数字小于max
win++; // max连胜次数加一
if(win==k) return max; // 如果连胜次数等于k，返回max
}else if(arr[i]>max){ // 当前数字大于max
max=arr[i]; // 更新max
win=1; // 连胜次数重置为1。
}
}
return max;
}```

Runtime: 0 ms, faster than 100.00% of Java online submissions for Find the Winner of an Array Game.

Memory Usage: 51.9 MB, less than 100.00% of Java online submissions for Find the Winner of an Array Game.