Four lines in java 8 using functional programming


  • 0
    F

    According to that wikipedia page, f maps index to value, we reverse sort results of f, then h-index (f) = max(min (f(i), i))

    public class Solution {
        public int hIndex(int[] c) {
            if (c == null) {
    			return 0;
    		}
    		List<Integer> rsorted = Arrays.stream(c).mapToObj(i->Integer.valueOf(i)).sorted(Collections.reverseOrder()).collect(Collectors.toList());		
    		return IntStream.range(0,rsorted.size()).reduce(0,(hindex,curindex)->Math.max(hindex,Math.min(curindex+1,rsorted.get(curindex))));
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.