imashutosh51

Find H-Index

Mar 23rd, 2026
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. """
  2. Intuition for H-Index (Step-by-Step Breakdown)
  3.  
  4. Step 1: Identify the Problem Type
  5. ---------------------------------
  6. The H-Index measures the impact of a researcher. An H-Index of 'h'
  7. means at least 'h' papers have 'h' or more citations. While we could
  8. solve this by sorting (O(n log n)), we can use a 'Counting Sort'
  9. style approach to achieve O(n) time.
  10.  
  11. Step 2: Defining the Constraints (The 'Cap' Logic)
  12. --------------------------------------------------
  13. The maximum possible H-Index can be the N(lenght of the array of count of research papers) even for eg. [8,8,8,8] still H-index would be 4 only, maximum ie. all the research paper have more than 4 citations for 5 it won't fit.
  14. - Therefore, any citation count greater than 'n' can be treated
  15.  exactly the same as 'n'.
  16.  
  17. Step 3: Creating the Frequency Map (The Bucket)
  18. -----------------------------------------------
  19. 1. Create a 'buckets' array of size n+1.
  20. 2. Iterate through the citations:
  21.   - If citation count >= n: Increment buckets[n].
  22.   - Otherwise: Increment buckets[citation_count].
  23. This bucket array now tells us: "How many papers have exactly X citations?"
  24.  
  25. Step 4: The Cumulative Counting (Reverse Thinking)
  26. --------------------------------------------------
  27. To find 'h' papers with at least 'h' citations, it's easier to look
  28. from the highest possible 'h' down to 0.
  29. - Start a 'count' variable at 0.
  30. - Iterate through the buckets array from index 'n' down to 0.
  31.  
  32. Step 5: The Decision Moment
  33. ---------------------------
  34. As we iterate backwards from i = n down to 0:
  35. 1. Add the number of papers in the current bucket to our 'count'.
  36.   (count += buckets[i])
  37. 2. The 'count' now represents: "How many papers have AT LEAST 'i'
  38.   citations?"
  39. 3. If this 'count' >= 'i', then 'i' is our H-Index!
  40.   Since we are moving from largest to smallest, the first 'i' that
  41.   satisfies this is the MAXIMUM H-Index.
  42.  
  43. Step 6: Final Result
  44. --------------------
  45. Return 'i'. If no such 'i' is found (though it always will be for
  46. non-negative citations), return 0.
  47.  
  48. Step 7: Complexity
  49. ------------------
  50. - Time: O(n) - Two linear passes (one to fill buckets, one to find h).
  51. - Space: O(n) - To store the buckets array.
  52. """
  53.  
  54. class Solution:
  55.     def hIndex(self, citations: List[int]) -> int:
  56.         arr=[0]*(len(citations)+1)
  57.         n=len(citations)
  58.         for i in citations:
  59.             if i>=len(citations):
  60.                 arr[n]+=1
  61.             else:
  62.                 arr[i]+=1
  63.         print(arr)
  64.         n=len(citations)
  65.         prev=0
  66.         while n>=0:
  67.             if prev + arr[n]>=n:
  68.                 return n
  69.             prev+=arr[n]
  70.             n-=1
  71.         return 0
  72.            
Advertisement
Add Comment
Please, Sign In to add comment