My LeetCode grinding. Trying to do a problem a day.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

problem.txt 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules:
  2. In the beginning, you have the permutation P=[1,2,3,...,m].
  3. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i].
  4. Return an array containing the result for the given queries.
  5. Example 1:
  6. Input: queries = [3,1,2,1], m = 5
  7. Output: [2,1,2,1]
  8. Explanation: The queries are processed as follow:
  9. For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5].
  10. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5].
  11. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5].
  12. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5].
  13. Therefore, the array containing the result is [2,1,2,1].
  14. Example 2:
  15. Input: queries = [4,1,2,2], m = 4
  16. Output: [3,1,2,0]
  17. Example 3:
  18. Input: queries = [7,5,5,8,3], m = 8
  19. Output: [6,5,0,7,5]
  20. Constraints:
  21. 1 <= m <= 10^3
  22. 1 <= queries.length <= m
  23. 1 <= queries[i] <= m