leetcode 275. H-Index II

614 ワード

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