Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

  1. class Solution(object):
  2. def hIndex(self, citations):
  3. """
  4. :type citations: List[int]
  5. :rtype: int
  6. """
  7.  
  8. sort_c = citations[::-1]
  9. for i in xrange(len(sort_c)):
  10. if i>= sort_c[i]:
  11. return i
  12. return len(citations)

========================

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 thanh 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.

  1. class Solution(object):
  2. def hIndex(self, citations):
  3. """
  4. :type citations: List[int]
  5. :rtype: int
  6. """
  7. if len(citations)<=0:return 0
  8.  
  9. sort_c = sorted(citations,reverse=True)
  10. for i in xrange(len(sort_c)):
  11. if i>=sort_c[i]:
  12. return i
  13. return len(citations)

H-Index II @python的更多相关文章

  1. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  2. Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵

    H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...

  3. Leetcode之二分法专题-275. H指数 II(H-Index II)

    Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...

  4. Java实现 LeetCode 275 H指数 II

    275. H指数 II 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. h 指数的定义: "h 代表"高 ...

  5. [Swift]LeetCode275. H指数 II | H-Index II

    Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a ...

  6. LeetCode Pascal's Triangle && Pascal's Triangle II Python

    Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...

  7. 119. Pascal's Triangle II@python

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  8. nyoj 103-A+B Problem II (python 大数相加)

    103-A+B Problem II 内存限制:64MB 时间限制:3000ms 特判: No 通过数:10 提交数:45 难度:3 题目描述: I have a very simple proble ...

  9. [LeetCode] 275. H-Index II H指数 II

    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...

随机推荐

  1. 玩转X-CTR100 l STM32F4 l DRV8825 A4988 步进电机控制

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器控制步进电机 ...

  2. log4j的配置详解(转)

    转自:http://blog.sina.com.cn/s/blog_5ed94d710101go3u.html 最近使用log4j写log时候发现网上的写的都是千篇一律,写的好的嘛不全,写的全一点的嘛 ...

  3. Beta阶段第1周/共2周 Scrum立会报告+燃尽图 05

    作业要求与 [https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284] 相同 版本控制:https://git.coding.net/li ...

  4. jQuery之阻止默认事件以及解除阻止

    大家都知道e.preventDefault()可以阻止默认时间,例如提交功能,但是怎么解除呢?以下参考于网络: 可以使用removeEventListener来移除.但是条件是addEventList ...

  5. Cisco ASA(8.4)端口映射设定(ASDM)

    1.进入到Configuration→firewall→NAT Rules画面. 2.点“services”添加服务端口,此案例添加TCP 1443和UDP 1443端口映射 3.添加“Network ...

  6. 2016 ACM/ICPC Asia Regional Qingdao Online 1001 I Count Two Three(打表+二分搜索)

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  7. iOS-----使用NSURLConnection

    使用NSURLConnection 如果只是为了读取HTTP等服务器数据,或向服务器提交数据,iOS还提供了NSURLConnection类,NSURLConnection使用NSURLRequest ...

  8. Android App data write as file data with synchronous Demo

    package com.android.utils; import java.io.File; import java.io.IOException; import java.io.RandomAcc ...

  9. convert CAN frame

    前言 最近了解了一些socket can的知识点,本文主要介绍如何将数据转换为CAN报文,前提是已经确定CAN的传输协议. 本文使用的CAN报文共有22条,这些报文共用一个can id,每条报文使用序 ...

  10. 如何快速切换目录cd-linux

    前言 cd命令是linux系统中的基本命令行,表示改变工作目录.本文主要介绍几个常用的cd命令. 系统环境 OS:ubuntu16.04. 操作过程 cd www 表示切换到www目录: cd .. ...