Блог пользователя minasedx

Автор minasedx, 16 месяцев назад, По-английски

A. Good Pairs


Tutorial
Solution

B. Advantage


Tutorial
Solution

C. Planets


Tutorial
Solution

D. Two Arrays


Tutorial
Solution

E. Gravity Flip


Tutorial
Solution
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
16 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

why does the time complexity of sorting in problem E is just $$$O(n)$$$, isn't it $$$O(n log n)$$$?

  • »
    »
    16 месяцев назад, # ^ |
    Rev. 5   Проголосовать: нравится 0 Проголосовать: не нравится

    Disclaimer: I do not have permission to view the problem statement, but based on the wording of the editorial, I believe that this is what is happening.

    Normal sorting (for example merge sort) is $$$O(n \log n)$$$, but we can sort an array of $$$n$$$ integers, each having a value between $$$0$$$ and $$$n$$$ in $$$O(n)$$$ time using counting sort.

    The idea is to count how many times each element appears in the array using a separate array (usually called the frequency array). After that, we can easily retrieve the sorted array.

    It might be difficult to understand my explanation, but I hope the code should be clear:

    Code