<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Find Peak Element ### Source - leetcode: [Find Peak Element | LeetCode OJ](https://leetcode.com/problems/find-peak-element/) - lintcode: [(75) Find Peak Element](http://www.lintcode.com/en/problem/find-peak-element/) ### Problem A peak element is an element that is greater than its neighbors. Given an input array where `num[i] ≠ num[i+1]`, find a peak element and returnits index. The array may contain multiple peaks, in that case return the index to any oneof the peaks is fine. You may imagine that `num[-1] = num[n] = -∞`. For example, in array `[1, 2, 3, 1]`, 3 is a peak element and your functionshould return the index number 2. #### Note: Your solution should be in logarithmic complexity. #### Credits: Special thanks to [@ts](https://oj.leetcode.com/discuss/user/ts) for addingthis problem and creating all test cases. ### 題解1 由時間復雜度的暗示可知應使用二分搜索。首先分析若使用傳統的二分搜索,若`A[mid] > A[mid - 1] && A[mid] < A[mid + 1]`,則找到一個peak為A[mid];若`A[mid - 1] > A[mid]`,則A[mid]左側必定存在一個peak,可用反證法證明:若左側不存在peak,則A[mid]左側元素必滿足`A[0] > A[1] > ... > A[mid -1] > A[mid]`,與已知`A[0] < A[1]`矛盾,證畢。同理可得若`A[mid + 1] > A[mid]`,則A[mid]右側必定存在一個peak。如此迭代即可得解。由于題中假設端點外側的值均為負無窮大,即`num[-1] < num[0] && num[n-1] > num[n]`, 那么問題來了,這樣一來就不能確定峰值一定存在了,因為給定數組為單調序列的話就咩有峰值了,但是實際情況是——題中有負無窮的假設,也就是說在單調序列的情況下,峰值為數組首部或者尾部元素,誰大就是誰了。 備注:如果本題是找 first/last peak,就不能用二分法了。 ### Python ~~~ class Solution: #@param A: An integers list. #@return: return any of peek positions. def findPeak(self, A): if not A: return -1 l, r = 0, len(A) - 1 while l + 1 < r: mid = l + (r - l) / 2 if A[mid] < A[mid - 1]: r = mid elif A[mid] < A[mid + 1]: l = mid else: return mid mid = l if A[l] > A[r] else r return mid ~~~ ### C++ ~~~ class Solution { public: /** * @param A: An integers array. * @return: return any of peek positions. */ int findPeak(vector<int> A) { if (A.size() == 0) return -1; int l = 0, r = A.size() - 1; while (l + 1 < r) { int mid = l + (r - l) / 2; if (A[mid] < A[mid - 1]) { r = mid; } else if (A[mid] < A[mid + 1]) { l = mid; } else { return mid; } } int mid = A[l] > A[r] ? l : r; return mid; } }; ~~~ ### Java ~~~ class Solution { /** * @param A: An integers array. * @return: return any of peek positions. */ public int findPeak(int[] A) { if (A == null || A.length == 0) return -1; int lb = 0, ub = A.length - 1; while (lb + 1 < ub) { int mid = lb + (ub - lb) / 2; if (A[mid] < A[mid + 1]) { lb = mid; } else if (A[mid] < A[mid - 1]){ ub = mid; } else { // find a peak return mid; } } // return a larger number return A[lb] > A[ub] ? lb : ub; } } ~~~ ### 源碼分析 典型的二分法模板應用,需要注意的是需要考慮單調序列的特殊情況。當然也可使用緊湊一點的實現如改寫循環條件為`l < r`,這樣就不用考慮單調序列了,見實現2. ### 復雜度分析 二分法,時間復雜度 O(logn)O(\log n)O(logn). #### Java - compact implementation[leetcode_discussion](#) ~~~ public class Solution { public int findPeakElement(int[] nums) { if (nums == null || nums.length == 0) { return -1; } int start = 0, end = nums.length - 1, mid = end / 2; while (start < end) { if (nums[mid] < nums[mid + 1]) { // 1 peak at least in the right side start = mid + 1; } else { // 1 peak at least in the left side end = mid; } mid = start + (end - start) / 2; } return start; } } ~~~ C++ 的代碼可參考 Java 或者 @xuewei4d 的實現。 ****> leetcode 和 lintcode 上給的方法名不一樣,leetcode 上的為`findPeakElement`而 lintcode 上為`findPeak`,弄混的話會編譯錯誤。 ### Reference - leetcode_discussion > . [Java - Binary-Search Solution - Leetcode Discuss](https://leetcode.com/discuss/23840/java-binary-search-solution)[ ?](# "Jump back to footnote [leetcode_discussion] in the text.")
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看