코테준비 - H-Index II

정상화·2023년 3월 5일

LeetCode

목록 보기
221/222

H-Index II

class Solution {
public:
    int hIndex(vector<int> &citations) {
        int len = citations.size();
        int s, m, e;
        s = 0;
        e = len - 1;

        while (s < e) {
            m = (s + e) / 2;
            if (isHIndex(m, len, citations) >= 0) {
                e = m;
            } else {
                s = m + 1;
            }
        }
        m = (s + e) / 2;

        return isHIndex(m, len, citations) >= 0 ? len - m : 0;
    }

    int isHIndex(int idx, int len, vector<int> &citations) {
        int h = len - idx;
        int num = citations.at(idx);
        return num == h ? 0 : num < h ? -1 : 1;
    }
};
profile
백엔드 희망

0개의 댓글