package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/find-the-winner-of-an-array-game/
 */
public class Problem1535 {
    public int getWinner(int[] arr, int k) {
        int answer;
        int first = 0;
        int last = arr.length - 1;
        Map<Integer, Integer> map = new HashMap<>();
        while (true) {
            int second = (first + 1) % arr.length;
            if (arr[first] > arr[second]) {
                int count = map.getOrDefault(arr[first], 0) + 1;
                if (count == k) {
                    answer = arr[first];
                    break;
                }
                map.put(arr[first], count);
                int tmp = arr[second];
                arr[second] = arr[first];
                arr[first] = tmp;
            } else {
                int count = map.getOrDefault(arr[second], 0) + 1;
                if (count == k) {
                    answer = arr[first];
                    break;
                }
                map.put(arr[second], count);

            }
            first = (first + 1) % arr.length;
            last = (last + 1) % arr.length;
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1535 prob = new Problem1535();
//        System.out.println(prob.getWinner(new int[]{2,1,3,5,4,6,7}, 2)); // 5
//        System.out.println(prob.getWinner(new int[]{3,2,1}, 10)); // 3
//        System.out.println(prob.getWinner(new int[]{1,9,8,2,3,7,6,4,5}, 7)); // 9
        System.out.println(prob.getWinner(new int[]{1,11,22,33,44,55,66,77,88,99}, 1000000000)); // 99
    }
}

@@ -59,16 +59,51 @@
 til.Map;
+%0Aimport java.util.stream.IntStream;
 %0A%0A/**%0A *
@@ -247,28 +247,8 @@
 ) %7B%0A
-        int answer;%0A
     
@@ -366,19 +366,43 @@
     
-while (true
+for (int i = 0; i %3C arr.length; i++
 ) %7B%0A
@@ -611,32 +611,30 @@
             
-answer =
+return
  arr%5Bfirst%5D;
@@ -638,35 +638,8 @@
 t%5D;%0A
-                    break;%0A
     
@@ -956,54 +956,26 @@
     
-answer = arr%5Bfirst%5D;%0A                    break
+return arr%5Bsecond%5D
 ;%0A  
@@ -1035,17 +1035,16 @@
 count);%0A
-%0A
         
@@ -1168,453 +1168,43 @@
 urn 
-answer;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1535 prob = new Problem1535();%0A//        System.out.println(prob.getWinner(new int%5B%5D%7B2,1,3,5,4,6,7%7D, 2)); // 5%0A//        System.out.println(prob.getWinner(new int%5B%5D%7B3,2,1%7D, 10)); // 3%0A//        System.out.println(prob.getWinner(new int%5B%5D%7B1,9,8,2,3,7,6,4,5%7D, 7)); // 9%0A        System.out.println(prob.getWinner(new int%5B%5D%7B1,11,22,33,44,55,66,77,88,99%7D, 1000000000)); // 99
+IntStream.of(arr).max().getAsInt();
 %0A   

Solve problem 1535