imashutosh51

Top k numbers in a stream

Aug 9th, 2022 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. /*
  2. Time Complexity: O(N * K)
  3. Space Complexity: O(K + number of distinct elements)
  4.  
  5. Idea:
  6. We process the array one element at a time (streaming).
  7.  
  8. We maintain:
  9. 1) A frequency map -> stores how many times each number has appeared so far.
  10. 2) A small array "top" of size K + 1:
  11.    - top[0] to top[K-1] always store the current top K most frequent numbers.
  12.    - top[K] is a temporary place to insert the current number.
  13.  
  14. Process for each element:
  15. 1) Increase the frequency of the current number in the map.
  16. 2) Put the current number at position top[K].
  17. 3) Find where this number already exists in top[0..K-1] (if present).
  18.  
  19. Since only the current number’s frequency changed,
  20. only this number can move to the left in the top list.
  21.  
  22. 4) Move this number left by swapping while:
  23.    - Its frequency is greater than the left neighbor, or
  24.    - Frequency is same but the number is smaller.
  25.  
  26. All other elements are already in correct order, so no full sorting is needed.
  27.  
  28. After adjustment, top[0..K-1] always contains the current top K frequent numbers
  29. in correct order.
  30. */
  31.  
  32. class Solution:
  33.     def kTop(self, a, N, K):
  34.         freq = {}
  35.         top = [0] * (K + 1)
  36.         result = []
  37.        
  38.         for i in range(N):
  39.             x = a[i]
  40.            
  41.             # update frequency
  42.             if x in freq:
  43.                 freq[x] += 1
  44.             else:
  45.                 freq[x] = 1
  46.            
  47.             # put current element at last position
  48.             top[K] = x
  49.            
  50.             # find first position of x in top[0..K-1]
  51.             j = 0
  52.             while j < K:
  53.                 if top[j] == x:
  54.                     break
  55.                 j += 1
  56.            
  57.             # move left while needed
  58.             while j > 0:
  59.                 # if left side is empty (0), just swap
  60.                 if top[j-1] == 0:
  61.                     top[j], top[j-1] = top[j-1], top[j]
  62.                
  63.                 # higher frequency first
  64.                 elif freq[top[j]] > freq[top[j-1]]:
  65.                     top[j], top[j-1] = top[j-1], top[j]
  66.                
  67.                 # same frequency, smaller number first
  68.                 elif freq[top[j]] == freq[top[j-1]] and top[j] < top[j-1]:
  69.                     top[j], top[j-1] = top[j-1], top[j]
  70.                
  71.                 else:
  72.                     break
  73.                
  74.                 j -= 1
  75.            
  76.             # collect current top K instead of printing
  77.             current = []
  78.             for l in range(K):
  79.                 if top[l] != 0:
  80.                     current.append(top[l])
  81.            
  82.             result.append(current)
  83.        
  84.         return result
  85.  
Advertisement
Add Comment
Please, Sign In to add comment