{"id":1357,"date":"2017-12-29T21:24:30","date_gmt":"2017-12-30T05:24:30","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1357"},"modified":"2018-01-02T03:05:17","modified_gmt":"2018-01-02T11:05:17","slug":"leetcode-748-largest-number-at-least-twice-of-others","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/difficulty\/easy\/leetcode-748-largest-number-at-least-twice-of-others\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 748. Largest Number At Least Twice of Others"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\uff0c\u95ee\u4f60\u5176\u4e2d\u6700\u5927\u7684\u6570\u662f\u4e0d\u662f\u6bd4\u5269\u4e0b\u7684\u6570\u59272\u500d\u4ee5\u4e0a\uff0c\u8fd4\u56de\u8fd9\u6837\u7684\u6570\u7684\u7d22\u5f15\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de-1.<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>In a given integer array\u00a0<code>nums<\/code>, there is always exactly one largest element.<\/p>\n<p>Find whether the largest element in the array is at least twice as much as every other number in the array.<\/p>\n<p>If it is, return the\u00a0<b>index<\/b>\u00a0of the largest element, otherwise return -1.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: nums = [3, 6, 1, 0]\r\nOutput: 1\r\nExplanation: 6 is the largest integer, and for every other number in the array x,\r\n6 is more than twice as big as x.  The index of value 6 is 1, so we return 1.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: nums = [1, 2, 3, 4]\r\nOutput: -1\r\nExplanation: 4 isn't at least as big as twice the value of 3, so we return -1.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li><code>nums<\/code>\u00a0will have a length in the range\u00a0<code>[1, 50]<\/code>.<\/li>\n<li>Every\u00a0<code>nums[i]<\/code>\u00a0will be an integer in the range\u00a0<code>[0, 99]<\/code>.<\/li>\n<\/ol>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 6 ms\r\nclass Solution {\r\npublic:\r\n    int dominantIndex(vector&lt;int&gt;&amp; nums) {\r\n        auto it1 = max_element(nums.begin(), nums.end());\r\n        const int max1 = *it1;\r\n        *it1 = -1;\r\n        const int max2 = *max_element(nums.begin(), nums.end());\r\n        return max1 &gt;= max2 * 2 ? distance(nums.begin(), it1) : -1;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\uff0c\u95ee\u4f60\u5176\u4e2d\u6700\u5927\u7684\u6570\u662f\u4e0d\u662f\u6bd4\u5269\u4e0b\u7684\u6570\u59272\u500d\u4ee5\u4e0a\uff0c\u8fd4\u56de\u8fd9\u6837\u7684\u6570\u7684\u7d22\u5f15\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de-1. Problem: In a given integer array\u00a0nums, there is always exactly one largest element. Find whether the largest element in the array is at least&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184,163],"tags":[],"class_list":["post-1357","post","type-post","status-publish","format-standard","hentry","category-array","category-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1357","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=1357"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1357\/revisions"}],"predecessor-version":[{"id":1465,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1357\/revisions\/1465"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}