Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Intuition for H-Index (Step-by-Step Breakdown)
- Step 1: Identify the Problem Type
- ---------------------------------
- The H-Index measures the impact of a researcher. An H-Index of 'h'
- means at least 'h' papers have 'h' or more citations. While we could
- solve this by sorting (O(n log n)), we can use a 'Counting Sort'
- style approach to achieve O(n) time.
- Step 2: Defining the Constraints (The 'Cap' Logic)
- --------------------------------------------------
- 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.
- - Therefore, any citation count greater than 'n' can be treated
- exactly the same as 'n'.
- Step 3: Creating the Frequency Map (The Bucket)
- -----------------------------------------------
- 1. Create a 'buckets' array of size n+1.
- 2. Iterate through the citations:
- - If citation count >= n: Increment buckets[n].
- - Otherwise: Increment buckets[citation_count].
- This bucket array now tells us: "How many papers have exactly X citations?"
- Step 4: The Cumulative Counting (Reverse Thinking)
- --------------------------------------------------
- To find 'h' papers with at least 'h' citations, it's easier to look
- from the highest possible 'h' down to 0.
- - Start a 'count' variable at 0.
- - Iterate through the buckets array from index 'n' down to 0.
- Step 5: The Decision Moment
- ---------------------------
- As we iterate backwards from i = n down to 0:
- 1. Add the number of papers in the current bucket to our 'count'.
- (count += buckets[i])
- 2. The 'count' now represents: "How many papers have AT LEAST 'i'
- citations?"
- 3. If this 'count' >= 'i', then 'i' is our H-Index!
- Since we are moving from largest to smallest, the first 'i' that
- satisfies this is the MAXIMUM H-Index.
- Step 6: Final Result
- --------------------
- Return 'i'. If no such 'i' is found (though it always will be for
- non-negative citations), return 0.
- Step 7: Complexity
- ------------------
- - Time: O(n) - Two linear passes (one to fill buckets, one to find h).
- - Space: O(n) - To store the buckets array.
- """
- class Solution:
- def hIndex(self, citations: List[int]) -> int:
- arr=[0]*(len(citations)+1)
- n=len(citations)
- for i in citations:
- if i>=len(citations):
- arr[n]+=1
- else:
- arr[i]+=1
- print(arr)
- n=len(citations)
- prev=0
- while n>=0:
- if prev + arr[n]>=n:
- return n
- prev+=arr[n]
- n-=1
- return 0
Advertisement
Add Comment
Please, Sign In to add comment