{"id":2098,"date":"2018-03-15T09:33:25","date_gmt":"2018-03-15T16:33:25","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2098"},"modified":"2018-03-15T21:46:45","modified_gmt":"2018-03-16T04:46:45","slug":"leetcode-275-h-index-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-275-h-index-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 275. H-Index II"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u5df2\u6392\u5e8f\u7684\u5f15\u7528\u6b21\u6570\u7684\u6570\u7ec4\uff0c\u6c42h-index\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/h-index-ii\/description\/\">https:\/\/leetcode.com\/problems\/h-index-ii\/description\/<\/a><\/p>\n<p><b>Follow up<\/b>\u00a0for\u00a0<a href=\"https:\/\/leetcode.com\/problems\/h-index\/\" target=\"_blank\" rel=\"noopener\">H-Index<\/a>: What if the\u00a0<code>citations<\/code>\u00a0array is sorted in ascending order? Could you optimize your algorithm?<\/p>\n<p><strong>Solution 1: Linear Scan<\/strong><\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 33 ms\r\nclass Solution {\r\npublic:\r\n  int hIndex(vector&lt;int&gt;&amp; citations) {    \r\n    for (int i = 1; i &lt;= citations.size(); ++i)\r\n      if (*(citations.rbegin() + i - 1) &lt; i) return i - 1;\r\n    return citations.size();\r\n  }\r\n};<\/pre>\n<p><strong>Solution 2: Binary Search<\/strong><\/p>\n<p>Time Complexity: O(logn)<\/p>\n<p>Space Complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 9 ms\r\nclass Solution {\r\npublic:\r\n  int hIndex(vector&lt;int&gt;&amp; citations) {    \r\n    int n = citations.size();\r\n    int l = 0;\r\n    int r = n;    \r\n    while (l &lt; r) {\r\n      int m = l + (r - l) \/ 2;\r\n      if (citations[n - m - 1] &lt;= m)\r\n        r = m;\r\n      else\r\n        l = m + 1;\r\n    }\r\n    return l;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u5df2\u6392\u5e8f\u7684\u5f15\u7528\u6b21\u6570\u7684\u6570\u7ec4\uff0c\u6c42h-index\u3002 Problem: https:\/\/leetcode.com\/problems\/h-index-ii\/description\/ Follow up\u00a0for\u00a0H-Index: What if the\u00a0citations\u00a0array is sorted in ascending order? Could you optimize your algorithm? Solution 1: Linear Scan Time complexity: O(n)&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,177],"class_list":["post-2098","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2098","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2098"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2098\/revisions"}],"predecessor-version":[{"id":2103,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2098\/revisions\/2103"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}