H-Index
LeetCode 274 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్త్రూ
Sorted descending, h = first i where citations[i] ≤ i.
నేను H-Index (LeetCode 274) ను ఇలా పరిష్కరించాను — Counting Sort ప్యాటర్న్లోని ఒక మధ్యస్థం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.
కింది వివరణ ఇంగ్లీష్లో ఉంది.
Intuition
Sort citations from most to least. Walking down the list, paper i (0-based) still supports an h of i+1 as long as it has more than i citations; the first paper that doesn't stops the count.
Approach
- 1
Sort descending.
- 2
Advance i while citations[i] > i.
- 3
Return i.
నేను ఎలా పరిష్కరించాను
Sort descendingPythonO(n log n) time
class Solution:
def hIndex(self, citations: List[int]) -> int:
citations.sort(reverse=True)
i=0
while i<len(citations):
if not citations[i]>i:
#6,5,3,1,0 -->citations
#0,1,2,3,0 -->index
break
i+=1
return iH-Index — తరచూ అడిగే ప్రశ్నలు
H-Index వెనుక ఉన్న intuition ఏమిటి?
Sort citations from most to least. Walking down the list, paper i (0-based) still supports an h of i+1 as long as it has more than i citations; the first paper that doesn't stops the count.
H-Index (LeetCode 274) ను ఎలా పరిష్కరించాలి?
1. Sort descending. 2. Advance i while citations[i] > i. 3. Return i.
H-Index యొక్క time, space complexity ఎంత?
Time: O(n log n). Space: O(1). (Accepted approach: Sort descending.)
H-Index లో సాధారణ పొరపాట్లు ఏమిటి?
A counting-sort version gives O(n).