Advertisement
hoangreal

Count candidates with engagement and relevance score are greater than K others

Oct 21st, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. """
  2. A candidate has two sets of score - engagement and relevance score (es and rs respectively).
  3. Find how many candidates who's engagement and relevance score are greater than K other candidates.
  4.  
  5. i.e for candidate i --> there must be k candidates where es[i] > es[j] and rs[i] > rs[j]
  6.  
  7. e.g
  8. es = [1, 2, 3, 4, 5]
  9. rs = [1, 2, 3, 4, 5]
  10.  
  11. K = 1
  12. Output: 4 since all candidates except the 0th one have at least one other candidate where its engagement and relevance score is greater than another candidate
  13.  
  14. Implementation should be in O(n log n)
  15. """
  16.  
  17. def solve(es, rs, K):
  18.     """
  19.    Solve the candidate ranking problem using a Binary Indexed Tree.
  20.    
  21.    Time Complexity: O(n log n)
  22.    Space Complexity: O(n)
  23.    """
  24.     n = len(es)
  25.    
  26.     # Coordinate compression
  27.     es_rank = {v: i+1 for i, v in enumerate(sorted(set(es)))}
  28.     rs_rank = {v: i+1 for i, v in enumerate(sorted(set(rs)))}
  29.     candidates = [(es_rank[e], rs_rank[r]) for e, r in zip(es, rs)]
  30.    
  31.     # Sort candidates: es ascending, rs descending
  32.     candidates.sort(key=lambda x: (x[0], -x[1]))
  33.    
  34.     # Initialize Binary Indexed Tree
  35.     bit_size = len(rs_rank) + 1
  36.     BIT = [0] * bit_size
  37.    
  38.     def update(i):
  39.         # BIT property: update all ranges that include index i
  40.         while i < bit_size:
  41.             BIT[i] += 1
  42.             i += i & -i
  43.    
  44.     def query(i):
  45.         # BIT property: sum all ranges up to index i
  46.         res = 0
  47.         while i > 0:
  48.             res += BIT[i]
  49.             i -= i & -i
  50.         return res
  51.    
  52.     result = 0
  53.     i = 0
  54.     while i < n:
  55.         es_val = candidates[i][0]
  56.         same_es = []
  57.        
  58.         # Group candidates with the same es value
  59.         while i < n and candidates[i][0] == es_val:
  60.             same_es.append(candidates[i])
  61.             i += 1
  62.        
  63.         # Count candidates with lower rs using BIT
  64.         counts = [query(rs - 1) for _, rs in same_es]
  65.        
  66.         # Update BIT and count qualifying candidates
  67.         for idx, (_, rs) in enumerate(same_es):
  68.             update(rs)
  69.             if counts[idx] >= K:
  70.                 result += 1
  71.    
  72.     return result
  73.  
  74.  
  75. es = [1, 2, 3, 4, 5]
  76. rs = [1, 2, 3, 4, 5]
  77.  
  78. K = 1
  79.  
  80. print(solve(es,rs,K)) # 4
  81.  
  82. es = [5, 4, 3, 2, 1]
  83. rs = [1, 2, 3, 4, 5]
  84.  
  85. K = 1
  86.  
  87. print(solve(es,rs,K)) # 0
  88.  
  89. es = [1, 2, 2, 3, 3, 4, 5]
  90. rs = [1, 2, 7, 2, 3, 4, 5]
  91.  
  92. K = 3
  93. print(solve(es,rs,3)) # 2
  94.  
  95. es = [5, 0, 5]
  96. rs = [10, 5, 10]
  97. print(solve(es, rs, 1)) # 2
  98.  
  99. es = [1, 2, 3, 4, 5]
  100. rs = [5, 4, 3, 2, 1]
  101. print(solve(es, rs, 1)) # 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement