-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
274 H-Index.swift
42 lines (34 loc) 路 1013 Bytes
/
274 H-Index.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// 274 H-Index.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 11/08/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/h-index/
class Solution {
/// Computes the researcher's h-index.
///
/// - Parameter citations: An array of citations of a researcher.
/// - Returns: H-index.
///
/// - Complexity:
/// - time: O(n), where n is the length of citations.
/// - space: O(n), where n is the length of citations.
func hIndex(_ citations: [Int]) -> Int {
guard !citations.isEmpty else { return 0 }
let n = citations.count
var papers = [Int](repeating: 0, count: n+1)
for citation in citations {
papers[min(n, citation)] += 1
}
var ans = 0
for i in (0...n).reversed() {
ans += papers[i]
guard ans >= i else { continue }
return i
}
return 0
}
}