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 ...
随机推荐
- error: templates may not be ‘virtual’
模板函数不能是虚函数,原因如下: 首先呢,模板函数并不是函数,他需要特定的类型去实例化成为函数.你定义一个函数模板,是不生成任何函数的,只有当你用代码去调用它时,才会根据你的类型去实例化成为特定 ...
- eclipse编译Jmeter源码
1.在apache官网下载源码和安装包 http://jmeter.apache.org/ 2. 解压 解压安装包和源码包, 将安装包apache-jmeter-3.3 里lib ...
- 洛谷 - P3033 - 牛的障碍Cow Steeplechase - 二分图最大独立集
https://www.luogu.org/fe/problem/P3033 二分图最大独立集 注意输入的时候控制x1,y1,x2,y2的相对大小. #include<bits/stdc++.h ...
- SourceTree配置BeyondCompare代码冲突解决工具
一.工具准备:SourceTree这个你得有.然后下载BeyondCompare(破解教程) 二.配置环境:SourceTree->工具->选项->比较,外部对比工具和合并工具都选择 ...
- Unity json
MiniJSON.cs using UnityEngine; using System; using System.Collections; using System.Collections.Gene ...
- 洛谷P2480 [SDOI2010]古代猪文(卢卡斯定理+中国剩余定理)
传送门 好吧我数学差的好像不是一点半点…… 题目求的是$G^{\sum_{d|n}C^d_n}mod\ 999911659$ 我们可以利用费马小定理$a^{k}\equiv a^{k\ mod\ (p ...
- 初次接触Service笔记
Service是后台的运行的小程序,分两种一种是StarService()另外一种是bindService(),这种可调用Service中的方法和返回结果等操作而StarService不能 他的生命周 ...
- 五粮液【线段树】By cellur925
题目传送门 考场上感觉的确是线段树,还要维护区间最值...最值怎么维护?还要区间修改?\(update\)的时候加一下就好了吧...之后怎么搞啊?\(qwqwq\)之后好像不太会了...果断删除几乎快 ...
- iOS开发 - CocoaPods的常见使用方式
1 CocoaPods 的安装 1.1 作用: 帮助管理和维护第三方框架,快速的搜索到第三方框架, 然后自动集成到工程里面来, 并编译成一个libPod.a的静态库给我们项目用 1.2 理解: 1. ...
- Bootstrap里的文件作用
Bootstrap里的文件分别表示什么?都有什么用? bootstrap.css bootstrap.min.css bootstrap-responsive.css bootstrap-respon ...