由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - a problem: find local maxima in sublinear time
相关主题
请教个面试题问道老题
问一道题什么情况下用dynamic programming?
提供一个full time面经吧,小公司面试比大公司虐多了请教一道产生随机数的问题
贡献一个 一个L家的店面题目问一道题k Sum
问个careercup上的老题目,看不懂答案发一道G家的onsite题及教训,顺便求linkedin和twitter内推
做题来个bit题
请教一个问题的答案,好像以前有人讨论过问一道CareerCup里的题目
算法题:两列找共同元素有O(n)的算法吗?hackerrank weekly contest problem 2, How many Matrices
相关话题的讨论汇总
话题: maxima话题: integer话题: local话题: sublinear话题: find
进入JobHunting版参与讨论
1 (共1页)
l***i
发帖数: 1309
1
Given an array of distinct integers, a[0] to a[n-1]
a[i] is local maxima if a[i] > a[i-1] and a[i] > a[i+1] provided the
neighbor exists.
Find any local maxima in sublinear time.
e****e
发帖数: 418
2
Any hidden trick?
Here is my Java code.
Integer[] findLocalMaxima(int[] a) {
if ( a == null || a.length == 0 )
throw new RuntimeException();

List list = new ArrayList();
for ( int i = 1; i < a.length - 1; i++ ) {
if ( a[i] > a[i-1] && a[i] > a[i+1] )
list.add( a[i] );
}
return (Integer[])list.toArray(new Integer[]{});
}
z********i
发帖数: 568
3
可能吗?假设a[0]..a[i]是递增,a[i+1]..a[-1]是递增。
如果a[i+1] But i could be anywhere in 0..n-1.

【在 l***i 的大作中提到】
: Given an array of distinct integers, a[0] to a[n-1]
: a[i] is local maxima if a[i] > a[i-1] and a[i] > a[i+1] provided the
: neighbor exists.
: Find any local maxima in sublinear time.

l***i
发帖数: 1309
4
The solution will find a[i] in log(n) time in your case, and in general.

【在 z********i 的大作中提到】
: 可能吗?假设a[0]..a[i]是递增,a[i+1]..a[-1]是递增。
: 如果a[i+1]: But i could be anywhere in 0..n-1.

z********i
发帖数: 568
5
洗耳恭听。

【在 l***i 的大作中提到】
: The solution will find a[i] in log(n) time in your case, and in general.
l***i
发帖数: 1309
6
I learned from someone else.
The idea is binary search.
Take a[n/2], then check its left and right, if a[n/2] is max among the three
, then we get a[n/2] as local maxima.
Otherwise w.l.o.g we can assume a[n/2 - 1] < a[n/2] < a[n/2 + 1]
Now there has to be a local maxima in a[n/2+1] to a[n-1]. So you recurse on
the right half of the array.
p*****2
发帖数: 21240
7
能不能给几个例子?
这题可能没有maxima, 也可能有多个maxima吗?
C***U
发帖数: 2406
8
这谁教你的?

three
on

【在 l***i 的大作中提到】
: I learned from someone else.
: The idea is binary search.
: Take a[n/2], then check its left and right, if a[n/2] is max among the three
: , then we get a[n/2] as local maxima.
: Otherwise w.l.o.g we can assume a[n/2 - 1] < a[n/2] < a[n/2 + 1]
: Now there has to be a local maxima in a[n/2+1] to a[n-1]. So you recurse on
: the right half of the array.

c********t
发帖数: 5706
9
请问什么是sublinear time?和linear time有什么区别?wiki上的解释没看懂。

【在 l***i 的大作中提到】
: Given an array of distinct integers, a[0] to a[n-1]
: a[i] is local maxima if a[i] > a[i-1] and a[i] > a[i+1] provided the
: neighbor exists.
: Find any local maxima in sublinear time.

l*****i
发帖数: 136
10
O(n^p), 0
【在 c********t 的大作中提到】
: 请问什么是sublinear time?和linear time有什么区别?wiki上的解释没看懂。
相关主题
做题问道老题
请教一个问题的答案,好像以前有人讨论过什么情况下用dynamic programming?
算法题:两列找共同元素有O(n)的算法吗?请教一道产生随机数的问题
进入JobHunting版参与讨论
z********i
发帖数: 568
11
In the example: a[0]..a[i] increasing order, a[i+1]..a[n-1] increasing order.
1) i>n/2, the assumption to further search the right half is correct.
2) i
three
on

【在 l***i 的大作中提到】
: I learned from someone else.
: The idea is binary search.
: Take a[n/2], then check its left and right, if a[n/2] is max among the three
: , then we get a[n/2] as local maxima.
: Otherwise w.l.o.g we can assume a[n/2 - 1] < a[n/2] < a[n/2 + 1]
: Now there has to be a local maxima in a[n/2+1] to a[n-1]. So you recurse on
: the right half of the array.

l***i
发帖数: 1309
12
if a[i+1]..a[n-1] is in increasing order, then a[n-1] is a local maxima. So
search right half is correct regardless of i > n/2 or i < n/2.
this is a homework problem from MIT algorithm class.

order.

【在 z********i 的大作中提到】
: In the example: a[0]..a[i] increasing order, a[i+1]..a[n-1] increasing order.
: 1) i>n/2, the assumption to further search the right half is correct.
: 2) i:
: three
: on

z********i
发帖数: 568
13
If a[n-1] is a maximal when a[n-1]>a[n-2], it is correct.

So

【在 l***i 的大作中提到】
: if a[i+1]..a[n-1] is in increasing order, then a[n-1] is a local maxima. So
: search right half is correct regardless of i > n/2 or i < n/2.
: this is a homework problem from MIT algorithm class.
:
: order.

l***i
发帖数: 1309
14
Yes, the problem says a[i] has to be bigger than its left and right neighbor
, provided such neighbor exists.
d***n
发帖数: 65
15
如果这样定义local maxima那么binary search自然可以

So

【在 l***i 的大作中提到】
: if a[i+1]..a[n-1] is in increasing order, then a[n-1] is a local maxima. So
: search right half is correct regardless of i > n/2 or i < n/2.
: this is a homework problem from MIT algorithm class.
:
: order.

1 (共1页)
进入JobHunting版参与讨论
相关主题
hackerrank weekly contest problem 2, How many Matrices问个careercup上的老题目,看不懂答案
小结可以应用二分查找的面试题做题
find all longest increasing subsequence 谁有源码?请教一个问题的答案,好像以前有人讨论过
请教一个数论的问题算法题:两列找共同元素有O(n)的算法吗?
请教个面试题问道老题
问一道题什么情况下用dynamic programming?
提供一个full time面经吧,小公司面试比大公司虐多了请教一道产生随机数的问题
贡献一个 一个L家的店面题目问一道题k Sum
相关话题的讨论汇总
话题: maxima话题: integer话题: local话题: sublinear话题: find