Leetcode 每日一题C 语言版 -- 274 H-index

Leetcode 每日一题C 语言版 -- 274 H-index

日拱一卒,功不唐捐。欢迎诸君评论区打卡同行共勉!!

问题描述

https://leetcode.com/problems/h-index/description/?envType=study-plan-v2\&envId=top-interview-150

复制代码
Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return the researcher's h-index.

According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.

 

Example 1:

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.
Example 2:

Input: citations = [1,3,1]
Output: 1
 

Constraints:

n == citations.length
1 <= n <= 5000
0 <= citations[i] <= 1000

解法一

先排序,引用次数从小到大排序,例如[3, 0, 6, 1, 5]

-> [0, 1, 3, 5, 6], 遍历数组

引用X大于等于0 的有Y个, 5 - 0 = 5个

引用X大于等于1 的有Y个, 5 - 1 = 4个

引用X大于等于3 的有Y个, 5 - 2 = 3个

引用X大于等于5 的有Y个, 5 - 3 = 2个

引用X大于等于6 的有Y个, 5 - 4 = 1个

X>=Y时, Y即为H-index

c 复制代码
#include <stdlib.h>

#define max(x, y) ((x) > (y) ? (x) : (y))

static int compare(const void *a, const void *b)
{
    int __a = *(const int *)a;
    int __b = *(const int *)b;

    return (__a > __b) - (__a < __b);
}

int hIndex(int* citations, int citationsSize) {
    uint32_t i = 0;

    qsort(citations, citationsSize, sizeof(int), compare);
    for (i = 0; i < citationsSize; i++) {
        if (citations[i] >= citationsSize - i)
            return citationsSize - i;
    }

    return 0;
}
相关推荐
0 0 03 小时前
CCF-CSP 36-3 缓存模拟(cache)【C++】
开发语言·c++·算法
蒟蒻小袁3 小时前
Hot100--找到字符串中所有字母异位词
java·算法·leetcode·面试
kingmax542120083 小时前
高中数学教师资格面试试讲稿:《直线的位置关系(例2)》
线性代数·算法·面试·矩阵·教师资格
吃着火锅x唱着歌3 小时前
LeetCode 2909.元素和最小的山形三元组II
数据结构·算法·leetcode
小O的算法实验室3 小时前
2026年SEVC SCI2区,基于k均值聚类和自适应双群策略的粒子群算法,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
程序员-King.3 小时前
day115—同向双指针—将x减到0的最小操作数(LeetCode-1658)
算法·leetcode·双指针
全栈工程师修炼指南3 小时前
Nginx | 负载均衡策略:一致性哈希算法实践
运维·算法·nginx·负载均衡·哈希算法
Jerryhut4 小时前
sklearn函数总结六——特征降维 压缩数据 - 特征提取(PCA&LDA)
人工智能·算法·机器学习·scikit-learn·sklearn
啊森要自信4 小时前
【C++的奇迹之旅】map与set应用
c语言·开发语言·c++