zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python 刷Leetcode题库,顺带学英语单词(38)

PythonLeetCode 38 题库 英语单词 顺带
2023-09-14 09:01:29 时间

Find Peak Element

A peak element is an element that is greater than its neighbors.
Given an input array nums , where nums[i] ≠ nums[i+1] , find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that nums[-1] = nums[n] = -∞ .    [#162]

Examples:
Input: nums = [1,2,3,1]
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.

Input: nums = [1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.

Note:
Your solution should be in logarithmic complexity.


Maximum Gap

Given an unsorted array, find the maximum difference between the successive elements in its sorted form.    [#164]

Return 0 if the array contains less than 2 elements.

Example 1:
Input: [3,6,9,1]
Output: 3
Explanation: The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.
Example 2:
Input: [10]
Output: 0
Explanation: The array contains less than 2 elements, therefore return 0.

Note:
You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
Try to solve it in linear time/space.