当前位置:网站首页>【LeetCode-162】寻找峰值
【LeetCode-162】寻找峰值
2022-08-11 05:30:00 【Ring*】
10.6 寻找峰值【162】
10.6.1 题目描述
峰值元素是指其值严格大于左右相邻值的元素。
给你一个整数数组 nums,找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回 任何一个峰值 所在位置即可。
你可以假设 nums[-1] = nums[n] = -∞ 。
你必须实现时间复杂度为 O(log n) 的算法来解决此问题。
10.6.2 方法一:寻找最大值
思路与算法
由于题目保证了 nums [ i ] ≠ nums [ i + 1 ] \textit{nums}[i] \neq \textit{nums}[i+1] nums[i]=nums[i+1],那么数组 nums 中最大值两侧的元素一定严格小于最大值本身。因此,最大值所在的位置就是一个可行的峰值位置。
我们对数组 nums 进行一次遍历,找到最大值对应的位置即可。
代码
class Solution {
public int findPeakElement(int[] nums) {
int idx = 0;
for (int i = 1; i < nums.length; ++i) {
if (nums[i] > nums[idx]) {
idx = i;
}
}
return idx;
}
}
复杂度分析
- 时间复杂度:O(n),其中 n 是数组 nums 的长度。
- 空间复杂度:O(1)。
10.6.3 方法二:迭代爬坡
思路与算法
代码
class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int idx = (int) (Math.random() * n);
while (!(compare(nums, idx - 1, idx) < 0 && compare(nums, idx, idx + 1) > 0)) {
if (compare(nums, idx, idx + 1) < 0) {
idx += 1;
} else {
idx -= 1;
}
}
return idx;
}
// 辅助函数,输入下标 i,返回一个二元组 (0/1, nums[i])
// 方便处理 nums[-1] 以及 nums[n] 的边界情况
public int[] get(int[] nums, int idx) {
// 返回的数组第一个为0则越界,越界的值赋为0,为1没有越界
if (idx == -1 || idx == nums.length) {
return new int[]{
0, 0};
}
return new int[]{
1, nums[idx]};
}
public int compare(int[] nums, int idx1, int idx2) {
int[] num1 = get(nums, idx1);
int[] num2 = get(nums, idx2);
if (num1[0] != num2[0]) {
// 有一个越界
return num1[0] > num2[0] ? 1 : -1;
}
if (num1[1] == num2[1]) {
return 0;
}
return num1[1] > num2[1] ? 1 : -1;
}
}
复杂度分析
- 时间复杂度:O(n),其中 n 是数组 nums 的长度。在最坏情况下,数组 nums 单调递增,并且我们随机到位置 0,这样就需要向右走到数组 nums 的最后一个位置。
- 空间复杂度:O(1)。
10.6.4 方法三:方法二的二分查找优化
思路与算法
代码
class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int left = 0, right = n - 1, ans = -1;
while (left <= right) {
int mid = (left + right) / 2;
if (compare(nums, mid - 1, mid) < 0 && compare(nums, mid, mid + 1) > 0) {
ans = mid;
break;
}
if (compare(nums, mid, mid + 1) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return ans;
}
// 辅助函数,输入下标 i,返回一个二元组 (0/1, nums[i])
// 方便处理 nums[-1] 以及 nums[n] 的边界情况
public int[] get(int[] nums, int idx) {
if (idx == -1 || idx == nums.length) {
return new int[]{
0, 0};
}
return new int[]{
1, nums[idx]};
}
public int compare(int[] nums, int idx1, int idx2) {
int[] num1 = get(nums, idx1);
int[] num2 = get(nums, idx2);
if (num1[0] != num2[0]) {
return num1[0] > num2[0] ? 1 : -1;
}
if (num1[1] == num2[1]) {
return 0;
}
return num1[1] > num2[1] ? 1 : -1;
}
}
复杂度分析
- 时间复杂度:O(logn),其中 n 是数组 nums 的长度。
- 空间复杂度:O(1)。
10.6.5 my answer—找最大值
class Solution {
public int findPeakElement(int[] nums) {
int i = 0;
int ans = 0;
int max = nums[i];
for(i = 1;i<nums.length;i++){
if(nums[i]>max){
max = nums[i];
ans = i;
}
}
return ans;
}
}
边栏推荐
- 厂商推送平台-华为接入
- C-自定义类型(结构体、枚举、联合)
- Interpretation of the paper: GAN and detection network multi-task/SOD-MTGAN: Small Object Detection via Multi-Task Generative Adversarial Network
- JS case exercise (classic case of teacher pink)
- 场景驱动的特征计算方式OpenMLDB,高效实现“现算先用”
- OpenMLDB Pulsar Connector: Efficiently connect real-time data to feature engineering
- 8-byte standard request parsing during USB enumeration
- Minutes of OpenMLDB Meetup No.2
- Pinyougou project combat notes
- 【无标题】
猜你喜欢

USB in NRZI to encode the data

第六届蓝帽杯 EscapeShellcode

OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights

Day 80

Real-time Feature Computing Platform Architecture Methodology and Practice Based on OpenMLDB

Jetpack's dataBinding

OpenMLDB Pulsar Connector:高效打通实时数据到特征工程

活动预告 | 4月23日,多场OpenMLDB精彩分享来袭,不负周末好时光

The third phase of the contributor task is wonderful

js learning advanced BOM part (pink teacher notes)
随机推荐
Event Preview | On April 23, a number of wonderful sharing sessions of OpenMLDB will come, which will live up to the good time of the weekend
Scene-driven feature calculation method OpenMLDB, efficient implementation of "calculate first use"
JS this关键字
OpenMLDB: Consistent production-level feature computing platform online and offline
Certificate of SearchGuard configuration
mk文件介绍
stack stack
Manufacturer Push Platform-Huawei Access
The role of the port
Jetpack use exception problem collection
The whole process of Tinker access --- configuration
three.js基础学习
ARM assembly instruction ADR and LDR
The whole process of Tinker access --- Compilation
Regular expression replacement for batch quick modification code
Day 68
开源之夏 2022 火热来袭 | 欢迎报名 OpenMLDB 社区项目~
精彩联动 | OpenMLDB Pulsar Connector原理和实操
127.0.0.1 已拒绝连接
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发