leetcode_1095. Find in Mountain Array_[Binary Search]
https://leetcode.com/problems/find-in-mountain-array/
题意:给定一个MountainArray(定义见题目),找到其中最早出现的target值的下标。
MountainArray.get() 函数调用不能超过100次。
解法:首先使用Binary Search找到mountain的peak,将array分为一个严格递增和一个严格递减的array,然后使用Binary Search。
class Solution
{
public:
int find_peak(MountainArray &mountainArr)
{
int l=, r=mountainArr.length()-;
while(l<=r)
{
int m=(l+r)/;
int m_val=mountainArr.get(m);
int m_r_val=mountainArr.get(m+);
if(m_val<m_r_val)
l=m+;
else
r=m-;
}
return l;
}
int findInMountainArray(int target, MountainArray &mountainArr)
{
int peak=find_peak(mountainArr);
int l=, r=peak;
if(mountainArr.get(l)<=target&&mountainArr.get(r)>=target)
while(l<=r)
{
int m=(l+r)/;
int val=mountainArr.get(m);
if(val==target)
return m;
else if(val<target)
l=m+;
else
r=m-;
}
l=peak;
r=mountainArr.length()-;
if(mountainArr.get(r)<=target&&mountainArr.get(l)>=target)
while(l<=r)
{
int m=(l+r)/;
int val=mountainArr.get(m);
if(val==target)
return m;
else if(val<target)
r=m-;
else
l=m+;
}
return -;
}
};
leetcode_1095. Find in Mountain Array_[Binary Search]的更多相关文章
- Binary Search 二分法方法总结
Binary Search 二分法方法总结 code教你做人:二分法核心思想是把一个大的问题拆成若干个小问题,最重要的是去掉一半或者选择一半. 二分法模板: public int BinarySear ...
- [LeetCode] questions conclusion_ Binary Search
Binary Search T(n) = T(n/2) + O(1) => T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
随机推荐
- SqlServer2012——表
1.数据类型 数字类型:int,smallint, 高精度:decimal,numeric 日期和时间:datetime,smalldatetime 二进制:binary,varbinary,imag ...
- 《剑指offer》面试题1:为类CMyString添加赋值运算符函数——C++拷贝构造函数与赋值函数
题中已给出CMyString的类定义,要求写赋值运算符函数. #include<iostream> #include<cstring> using namespace std; ...
- BZOJ3289【莫队算法+树状数组+离散化】
思路: 区间逆序数即是交换次数. 逆序数,可以用树状数组吧. 怎么处理区间变换的时候求逆序数啊.. 这里分成左边的增/删,右边的增/删 因为是按时序插入, 所以左边增,增一个数,计算:ans+=sun ...
- Linux 一些问题
终端以root账号执行 su - root
- java多线程中用到的方法详细解析
在多线程学习的过程中涉及的方法和接口特别多,本文就详细讲解下经常使用方法的作用和使用场景. 1.sleep()方法. 当线程对象调用sleep(time)方法后,当前线程会等待指定的时间(t ...
- NPOI用WorkbookFactory读写 2007以上格式文件(xlsx)
//我用的最新的2.2.1版本 //第一步:引用DLL,5个全导入,包括ICSHARP.ZIP,是个开源压缩工具包.XLSX是压缩格式,需要它来解压 //第二部: using NPOI.SS.User ...
- nginx+vue实现项目动静分离
一般的企业都会采用前后端分离的方式来开发.部署项目,这样做的好处是更好的让前后台各司其职.另外也由于nginx是一个轻量级的静态资源服务器,其高并发也是其优点之一.这样可以减轻双方服务器的压力,同时又 ...
- mysql8必知必会6 外键约束 增加 查询 删除 MySQL注释
- [題解]luogu_P1120小木棍(搜索)
好久以前抄的題解,現在重新抄題解做一下 1.對所有木棍從大到小排序,後用小的比較靈活 2.限制加入的木棍單調遞減,因為先/后用長/短木棍等價,反正就是那兩根 3.預處理出重複木棍的位置,防止重複搜索相 ...
- linux资源性能指标
1.cpu Running:正在运行的进程 Waiting:已准备就绪,等待运行的进程 Blocked:因为等待某些事件完成而阻塞的进程,通常在等待I/O 命令获取数据: vmstat 1其中: u ...