{"id":6216,"date":"2020-01-31T23:05:06","date_gmt":"2020-02-01T07:05:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6216"},"modified":"2020-01-31T23:05:07","modified_gmt":"2020-02-01T07:05:07","slug":"%e8%8a%b1%e8%8a%b1%e9%85%b1-leetcode-162-find-peak-element","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/%e8%8a%b1%e8%8a%b1%e9%85%b1-leetcode-162-find-peak-element\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 162. Find Peak Element"},"content":{"rendered":"\n<p>A peak element is an element that is greater than its neighbors.<\/p>\n\n\n\n<p>Given an input array&nbsp;<code>nums<\/code>, where&nbsp;<code>nums[i] \u2260 nums[i+1]<\/code>, find a peak element and return its index.<\/p>\n\n\n\n<p>The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.<\/p>\n\n\n\n<p>You may imagine that&nbsp;<code>nums[-1] = nums[n] = -\u221e<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> <strong>nums<\/strong> = <code>[1,2,3,1]<\/code>\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> 3 is a peak element and your function should return the index number 2.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> <strong>nums<\/strong> = <code>[<\/code>1,2,1,3,5,6,4]\n<strong>Output:<\/strong> 1 or 5 \n<strong>Explanation:<\/strong> Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int findPeakElement(const vector<int> &num) {\n    int l = 0, r = num.size() - 1; \/\/ preventing OOB\n    while (l < r) {\n      int m = l + (r - l) \/ 2;\n      \/\/ Find the first m s.t. num[m] > num[m + 1]\n      if (num[m] > num[m + 1])\n        r = m;\n      else\n        l = m + 1;\n    }\n    return l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A peak element is an element that is greater than its neighbors. Given an input array&nbsp;nums, where&nbsp;nums[i] \u2260 nums[i+1], find a peak element and return&#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,459],"class_list":["post-6216","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-medium","tag-ologn","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6216","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=6216"}],"version-history":[{"count":1,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6216\/revisions"}],"predecessor-version":[{"id":6217,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6216\/revisions\/6217"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}