本文共 888 字,大约阅读时间需要 2 分钟。
为了解决这个问题,我们需要找到一个单调递增非负数组中满足特定条件的最大值。具体来说,我们需要找到最大的整数 h,使得数组中有至少 h 个元素大于等于 h。
我们可以使用二分查找来高效地解决这个问题。二分查找的时间复杂度是 O(log n),非常适合处理大型数组。
public class Solution { public int hIndex(int[] citations) { int l = 0, r = citations.length; while (l < r) { int m = l + (r - l + 1) / 2; if (citations[citations.length - m] >= m) { l = m; } else { r = m - 1; } } return l; }} 这种方法确保了在最少的时间内找到最大的 h 值,非常高效。
转载地址:http://flbs.baihongyu.com/