- μΈμ© νμ
citations[i]
λ μ΅λ 1000κ° μ΄λ―λ‘ counterλ₯Ό μ¬μ©νμ¬ μΈμ© νμλ³ ν΄λΉλλ λ
Όλ¬Έμ μcounter[k]
λ₯Ό μ
μ μλ€.
k
ν μ΄μ μΈμ©λ λ
Όλ¬Έμ μsum
λ 1000 λΆν° κ±°κΎΈλ‘ λν΄κ°λ©΄ ꡬν μ μκ³ sum β₯ k
λ₯Ό λ§μ‘±νλ μ΅λμ k
κ°μ΄ κ²°κ΅ h-indexκ° λλ€.
class Solution {
public:
int hIndex(vector<int>& citations) {
vector<int> counter(1001);
fill(counter.begin(), counter.end(), 0);
for (int c : citations) {
++counter[c];
}
int sum = 0;
for (int h = 1000; h >= 0; --h) {
sum += counter[h];
if (sum >= h) return h;
}
return 0;
}
};