当前位置: 首页 > news >正文

LeetCode--H-Index

题目描述:
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.


According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."


For example, given citations = [3, 0, 6, 1, 5], which 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, his h-index is 3.


Note: If there are several possible values for h, the maximum one is taken as the h-index.


本题是说,对于一个数组arr中的每一个数arr[i],其中i∈[0,n-1] ,找到元素个数k,其中0<k<=n 。arr中有k个元素大于等于k,而arr中的其余n-k个元素均小于等于k。


思路:
对于数组arr,如果进行倒序排序再遍历的话,对于i和arr[i]关系,由于i是递增的,而arr[i]递减,只要找到第一个i>=arr[i],那么后面第n-i个元素一定满足i>=arr[i]。因此,在遍历过程中,只要统计所有满足i<arr[i]的元素个数即可。


因此本题可以转换为:对数组arr进行倒序排序,统计index < arr[index]的个数。例如对于


3,5,7,1,2,3来说,排序后为: 7,5,3,3,2,1
0 < 7 => count ++
1 < 5 => count ++
2 < 3 => count ++
3 = 3
4 > 2
5 > 1


因此H-index 为count = 3




实现代码:


public class Solution {
    public int HIndex(int[] citations) {
            if(citations.Length == 0){
        		return 0;
        	}
        
           var sorted = citations.OrderByDescending(x=>x).ToList();
           var sum = 0;
           for(var i = 0;i < sorted.Count; i++){
               if(i < sorted[i]){
        	   		sum ++;
        	   }
           }
           
           return sum;
    }
}


相关文章:

  • Leetcode--Lowest Common Ancestor of a Binary Search Tree
  • 参加Tibco的SOA应用及2009 IT架构趋势研讨会记
  • Leet 题目整理归类 - 快速通道 (持续更新)
  • 设计模式的阴谋论
  • c# mongodb driver 插入重复记录
  • 中国移动通信信息资源站实体与互联网短消息网关(ISMG)
  • MongoDB C# Driver 使用示例 (2.2)
  • C# GetHashCode 的实现方式
  • SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY的区别比较
  • Dynamic Language Runtime (DLR) 初深
  • 2009 Webware 100 名单揭晓
  • DLR之 ExpandoObject和DynamicObject的使用示例
  • asp.net常用正则表达式
  • .Net 中Partitioner static与dynamic的性能对比
  • c# 中的 for vs foreach
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • Android组件 - 收藏集 - 掘金
  • centos安装java运行环境jdk+tomcat
  • dva中组件的懒加载
  • node-sass 安装卡在 node scripts/install.js 解决办法
  • Python进阶细节
  • tensorflow学习笔记3——MNIST应用篇
  • weex踩坑之旅第一弹 ~ 搭建具有入口文件的weex脚手架
  • windows-nginx-https-本地配置
  • 初识 beanstalkd
  • 初识 webpack
  • 从setTimeout-setInterval看JS线程
  • 第2章 网络文档
  • 技术:超级实用的电脑小技巧
  • 力扣(LeetCode)965
  • 聊聊hikari连接池的leakDetectionThreshold
  • 悄悄地说一个bug
  • 详解移动APP与web APP的区别
  • 再谈express与koa的对比
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • Java总结 - String - 这篇请使劲喷我
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • 宾利慕尚创始人典藏版国内首秀,2025年前实现全系车型电动化 | 2019上海车展 ...
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • ​iOS安全加固方法及实现
  • ​力扣解法汇总946-验证栈序列
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • !$boo在php中什么意思,php前戏
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • #if 1...#endif
  • #stm32整理(一)flash读写
  • #微信小程序(布局、渲染层基础知识)
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (145)光线追踪距离场柔和阴影
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (6)STL算法之转换
  • (6)添加vue-cookie
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (k8s中)docker netty OOM问题记录
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别