[Leetcode] Sort, Hash -- 274. 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.

Solution:

  1. My idea is to sort the array in descending order

then iterate to compare the index  and value 

if the value < index i+1, then stop and return the index as the final answer

if value == index i,  return the index i+1

else it is the length of the array

 1         if len(citations) == 0 or citations is None:
 2             return 0
 3         citations = sorted(citations, reverse=True)
 4         #print (‘cita: ‘, citations)
 5         for i, c in enumerate(citations):
 6             if i+1 > c:
 7                 return i
 8             elif i+1 == c:
 9                 return i+1
10         return len(citations)

time complexity:  o(nlogn), space  complexity o(1)

2 nd method is use hashmap

time complexity o(n),  space complexity o(n)

get the array value count, when the value > len(array) L, count into the hashmap[L]

--reference

http://www.cnblogs.com/yrbbest/p/5031910.html

 1  if len(citations) == 0 or citations is None:
 2             return 0
 3         dicCount = {}
 4         for ci in citations:
 5             if ci > len(citations):
 6                 if len(citations) not in dicCount:
 7                     dicCount[len(citations)] = 1
 8                 else:
 9                     dicCount[len(citations)] += 1
10             else:
11                 if ci not in dicCount:
12                     dicCount[ci] = 1
13                 else:
14                     dicCount[ci] += 1
15         #print (‘ dicCount : ‘, dicCount)
16         sum = 0
17         for i in range(len(citations), -1, -1):
18             if i in dicCount:
19                 sum += dicCount[i]
20             if sum >= i:
21                 return i

时间: 2024-11-05 14:46:40

[Leetcode] Sort, Hash -- 274. H-Index的相关文章

LeetCode——Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

leetcode Sort Colors

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leetcode Sort Colors 计数排序 注: 题目的要求是将 A 数组重新排列成有序, 而不是将排序的序列输出 Sort Colors Given an array with n o

[leetcode]Sort List @ Python

原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度和空间复杂度要求比较高,所以查看了各种解法,最好的解法就是归并排序,由于链表在归并操作时并不需要像数组的归并操作那样分配一个临时数组空间,所以这样就是常数空间复杂度了,当然这里不考虑递归所产生的系统调用的栈.   这里涉及到一个链表常用的操作,即快慢指针的技巧.设置slow和fast指针,开始它们都

LeetCode[Sort]: Largest Number

LeetCode[Sort]: Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to

[leetcode]Sort Colors @ Python

原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integer

LeetCode: Sort Colors 题解

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

LeetCode::Sort List 详细分析

Sort a linked list in O(n log n) time using constant space complexity. 这道题目非常简短的一句话,给链表排序,看到nlogn,我们可以来简单复习一下排序.首先说一下这个nlogn的时间复杂度(根据决策树我们可以得出这个界限),是基于比较排序的最小上限,也就是说,对于没有一定范围情况的数据来说,最快的排序思路就是归并和快速排序了(当然具体的参数系数还是由更具体的设置决定的).对于数组的话,如果使用归并排序,不是in place的

LeetCode: Sort Colors [075]

[题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, a

[LeetCode] Sort Colors [23]

题目 Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and