274. H-Index

LeetCode medium original: C# #array #csharp #leetcode #math #medium #search #sort
Le texte du problème est traduit du russe pour la langue sélectionnée. Le code reste inchangé.

Дан tableau целых чисел citations, где citations[i] — количество цитирований, которое исследователь получил за свою i-ю статью. return h-индекс исследователя.

Согласно определению h-индекса на Википедии: h-индекс определяется как максимальное значение h, такое что данный исследователь опубликовал по крайней мере h статей, каждая из которых была процитирована как минимум h раз.

Exemple:

Input: citations = [3,0,6,1,5]

Output: 3

Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.

Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their h-index is 3.

C# solution

correspondant/original
public class Solution {
    public int HIndex(int[] citations) {
        int n = citations.Length;
        int[] papers = new int[n + 1];
        foreach (int c in citations)
            papers[Math.Min(n, c)]++;
        int k = n;
        for (int s = papers[n]; k > s; s += papers[k])
            k--;
        return k;
    }
}

C++ solution

brouillon automatique, à relire avant soumission
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public int HIndex(vector<int>& citations) {
        int n = citations.size();
        vector<int>& papers = new int[n + 1];
        foreach (int c in citations)
            papers[min(n, c)]++;
        int k = n;
        for (int s = papers[n]; k > s; s += papers[k])
            k--;
        return k;
    }
}

Java solution

correspondant/original
public class Solution {
    public int hIndex(int[] citations) {
        int n = citations.length;
        int[] papers = new int[n + 1];
        for (int c : citations)
            papers[Math.min(n, c)]++;
        int k = n;
        for (int s = papers[n]; k > s; s += papers[k])
            k--;
        return k;
    }
}

JavaScript solution

correspondant/original
var hIndex = function(citations) {
    let n = citations.length;
    let papers = new Array(n + 1).fill(0);
    for (let c of citations) {
        papers[Math.min(n, c)]++;
    }
    let k = n;
    for (let s = papers[n]; k > s; s += papers[k]) {
        k--;
    }
    return k;
};

Python solution

correspondant/original
class Solution:
    def hIndex(self, citations: List[int]) -> int:
        n = len(citations)
        papers = [0] * (n + 1)
        for c in citations:
            papers[min(n, c)] += 1
        k = n
        s = papers[n]
        while k > s:
            k -= 1
            s += papers[k]
        return k

Go solution

correspondant/original
func hIndex(citations []int) int {
    n := len(citations)
    papers := make([]int, n+1)
    for _, c := range citations {
        if c >= n {
            papers[n]++
        } else {
            papers[c]++
        }
    }
    k := n
    s := papers[n]
    for k > s {
        k--
        s += papers[k]
    }
    return k
}

Algorithm

1️⃣

Отсортировать tableau цитирований по убыванию:

Отсортировать tableau citations в порядке убывания, чтобы наибольшее количество цитирований было в начале tableauа.

2️⃣

find наибольшее значение i, для которого citations[i] > i:

Пройтись по отсортированному tableauу и find наибольшее значение i, для которого выполняется Énoncé citations[i] > i.

Это значение будет индексом, при котором количество цитирований статьи больше индекса.

3️⃣

Рассчитать h-индекс:

h-индекс будет равен i + 1, где i - наибольшее значение, найденное на предыдущем шаге.

😎

Vacancies for this task

offres actives with overlapping task tags are affichés.

Toutes les offres
Il n'y a pas encore d'offres actives.