当前位置:网站首页>寻找峰值[抽象二分练习]
寻找峰值[抽象二分练习]
2022-07-23 08:03:00 【REN_林森】
前言
不是死知识点的考察,这种题就很有难度,慢慢推出来也很有意思。这种题有抽象二分/贪心算法脑筋急转弯型。
一、寻找峰值

二、抽象二分
public int findPeakElement(int[] nums) {
// 一定存在峰值,说明不存在连续递增的情况,这和logN有什么联系?
int low = 0,high = nums.length - 1;
while(low < high){
int mid = low + (high - low >>> 1);
int midVal = nums[mid];
// 右边必有峰值。
if(midVal < nums[high]) low = mid + 1;
// 左边必有峰值。
else if(midVal < nums[low])high = mid - 1;
// nums[low] <= midVal >= nums[high]
/* 让high--,如果high是峰值,则必有比nums[high - 1] < nums[high], 而且nums[low] <= midVal,则nums[high - 1]之前必有峰值。 */
else --high;
}
return low;
}
总结
1)对于这种有难度/有巧劲的题,需要多理解题目的描述,挖挖里面的条件,找找和已学数据结构与算法的联系。如果不沉下心来利用已知条件分析,就会一点思路都没有,实在没有就看看题解,说明题练少了,看几个月也想不出来,浪费时间!
2)这种没有固定公式,在草稿纸上多举举例子,模拟模拟,找找规律和感觉,再运用数据结构把它表达出来,最后review慢慢沉淀一点套路。
3)最重要的还是多做做这类题目,多看看这类优秀的题解,开阔以下视野,分析起来才更加流畅,才能慢慢举一反三。
参考文献
[1] LeetCode 寻找峰值
边栏推荐
猜你喜欢

rtx3090ti什么水平 rtx3090ti显卡什么级别 rtx3090ti显卡怎么样

Rip experiment

Notes on the fifth day

STM32输出正弦波+cubeMX配置+HAL库

Kafka consumption reports an error coordinator unavailable Rediscovery will be attempt redisCovery

天玑820相当于骁龙多少处理器 天玑1100相当于骁龙多少 天玑820怎么样

338. 比特位计数

多重背包!

Remember that a vulnhub target plane exercise successfully won the root permission

MGRE comprehensive experiment
随机推荐
设计例化和连接
子序列 --- 编辑距离
How about the performance of Intel Celeron 7300? What level is it equivalent to
Design instantiation and connection
Uiscrollview (uicollectionview) prohibits horizontal and vertical sliding at the same time
Notes on the fifth day
Detailed introduction of RIP
Is there a big gap between core i5 12490f and i5 12600K
Process blocks and methods
How many processors is Tianji 1100 equivalent to snapdragon? How about Tianji 1100 equivalent to snapdragon
Stream stream is used for classification display.
shell跑的時候需要的需要了解命令
What is Tianji 920 equivalent to a snapdragon? How much is Tianji 920 equivalent to a snapdragon? How about Tianji 920
Day 10 notes
网络安全笔记1——Internet协议的安全性
iQOO 10 Pro和vivo X80 Pro区别 哪个好详细参数配置对比
JS to obtain age through ID card
太平洋大西洋水流问题
STM32输出SPWM波,HAL库,cubeMX配置,滤波后输出1KHz正弦波
Network security note 1 - Security of Internet Protocol