leetcode :Binary Search: H-Index IIe(275)

原创 2016年08月28日 16:16:36

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

Expected runtime complexity is in O(log n) and the input is sorted.


class Solution {
public:
    // binary search - O(log(n))
    int hIndex(vector<int>& citations) {
        int n = citations.size();
        int low = 0, high = n-1;

        while( low <= high ) {
            int mid = low + (high-low)/2;
            if (citations[mid] == n - mid) {
                return n - mid;
            }else if (citations[mid] > n-mid){
                high = mid - 1;
            }else {
                low = mid + 1;
            }
        }
        return n-low;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

<LeetCode OJ> 274 / 275 H-Index(I / II)

Total Accepted: 37719 Total Submissions: 125936 Difficulty: Medium Given an array of citations ...

Leetcode 274. H-Index & 275. H-Index II

274. H-Index Total Accepted: 49757 Total Submissions: 159122 Difficulty: Medium Given an a...

【LeetCode】99. Recover Binary Search Tree 分析、解法、注释、中序遍历、递归

99. Recover Binary Search Tree Total Accepted: 50739 Total Submissions: 192395 Difficulty: Hard...

《leetCode》:Binary Search Tree Iterator

Binary Search Tree Iterator题目如下:Implement an iterator over a binary search tree (BST). Your iterator...

leetcode98---Validate Binary Search Tree

问题描述:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is define...
  • will130
  • will130
  • 2015年12月26日 13:15
  • 207

[leetcode] 96. Unique Binary Search Trees 解题报告

题目链接:https://leetcode.com/problems/unique-binary-search-trees/ Given n, how many structurally uniq...

LeetCode-501. Find Mode in Binary Search Tree(Java)

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred...
  • NOadu
  • NOadu
  • 2017年07月19日 16:52
  • 156

leetcode -- Recover Binary Search Tree -- 重点

https://leetcode.com/problems/recover-binary-search-tree/这里有两种思路思路1就是inorder遍历,把value 和 node都分别用list...
  • xyqzki
  • xyqzki
  • 2016年01月08日 21:06
  • 416

LeetCode_Convert Sorted List to Binary Search Tree

LeetCode_Convert Sorted List to Binary Search Tree 解题思路

[leetcode]convert-sorted-list-to-binary-search-tree

题目Given a singly linked list where elements are sorted in ascending order, convert it to a height ba...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode :Binary Search: H-Index IIe(275)
举报原因:
原因补充:

(最多只允许输入30个字)