leetcode 275. H-Index II

来源:互联网 发布:网络电视在线观看 编辑:程序博客网 时间:2024/06/08 07:19

What if the citations array is sorted in ascending order? Could you optimize your algorithm?



int hIndex(int* citations, int citationsSize) {if (citations == NULL || citationsSize == 0)return 0;int k = citationsSize - 1;int max = -1;int curcount = 0;while (k >= 0){int cur = citations[k];int kk = k;while (kk >= 0 && citations[kk] == cur){curcount++;kk--;}int h = cur < curcount ? cur : curcount;if (h>max)max = h;if (cur <= max)return max;k = kk;}return max;}

accepted


0 0
原创粉丝点击