Tensorum
YouTubeसीखना शुरू करें
LeetCode 274 · Counting Sort

H-Index

LeetCode 274 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

Sorted descending, h = first i where citations[i] ≤ i.

मध्यमArraySortingCounting Sort

मैंने 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. 1

    Sort descending.

  2. 2

    Advance i while citations[i] > i.

  3. 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 i
स्टेप-थ्रू लोड हो रहा है…

H-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).