package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/queries-on-a-permutation-with-key/
 */
public class Problem1409 {
    public int[] processQueries(int[] queries, int m) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem1409 prob = new Problem1409();
        System.out.println(Arrays.toString(prob.processQueries(new int[]{3,1,2,1}, 5))); // [2,1,2,1]
        System.out.println(Arrays.toString(prob.processQueries(new int[]{4,1,2,2}, 4))); // [3,1,2,0]
        System.out.println(Arrays.toString(prob.processQueries(new int[]{7,5,5,8,3}, 8))); // [6,5,0,7,5]
    }
}

@@ -29,22 +29,53 @@
 va.util.
-Arrays
+Iterator;%0Aimport java.util.LinkedList
 ;%0A%0A/**%0A 
@@ -240,444 +240,666 @@
     
-// TODO%0A        return null;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1409 prob = new Problem1409();%0A        System.out.println(Arrays.toString(prob.processQueries(new int%5B%5D%7B3,1,2,1%7D, 5))); // %5B2,1,2,1%5D%0A        System.out.println(Arrays.toString(prob.processQueries(new int%5B%5D%7B4,1,2,2%7D, 4))); // %5B3,1,2,0%5D%0A        System.out.println(Arrays.toString(prob.processQueries(new int%5B%5D%7B7,5,5,8,3%7D, 8))); // %5B6,5,0,7,5%5D
+LinkedList%3CInteger%3E list = new LinkedList%3C%3E();%0A        for (int i = 0; i %3C m; i++) %7B%0A            list.add(i + 1);%0A        %7D%0A        int%5B%5D answer = new int%5Bqueries.length%5D;%0A        int index = 0;%0A        for (int query : queries) %7B%0A            Iterator%3CInteger%3E iterator = list.iterator();%0A            int i = 0;%0A            while (iterator.hasNext()) %7B%0A                int n = iterator.next();%0A                if (n == query) %7B%0A                    iterator.remove();%0A                    list.addFirst(n);%0A                    answer%5Bindex++%5D = i;%0A                    break;%0A                %7D%0A                i++;%0A            %7D%0A        %7D%0A        return answer;
 %0A   

Solve problem 1409