Redian新闻
>
Ask a google interview question(2)
avatar
Ask a google interview question(2)# JobHunting - 待字闺中
B*1
1
Given an array of integers A, give an algorithm to find the longest
Arithmetic progression in it, i.e find a sequence i1 < i2 < … < ik, such
that
A[i1], A[i2], …, A[ik] forms an arithmetic progression, and k is the
largest possible.
The sequence S1, S2, …, Sk is called an arithmetic progression if
Sj+1 – Sj is a constant
should be DP, but could not figure out the dp function.
avatar
B*1
2
顶。
avatar
q*x
3
注意了一下,你贴的都是些怪题。我怀疑是否真题。
浪费在这些题目上意思不大。还是把基本题搞熟为好。

【在 B*******1 的大作中提到】
: 顶。
avatar
B*1
4
不是我贴怪题,而是大部分题都会做了,这些是不会的。都是careercup上面看到的。
这题不是怪题阿,你如果google "longest arithmetic progression"就可以看到这篇
paper里面http://theory.cs.uiuc.edu/~jeffe/pubs/pdf/arith.pdf 一开始前言说则么用dp了,但是我没有完全看懂里面某些部分。所以上来问问这里有没有高人有更加好的方法
我想到了一个,把这个array转化成2维空间的点,然后求每2个点的斜率,longest
arithmetic progression 就是一条直线上面的点,最后找出出现最多的斜率就好了。
似乎复杂度跟dp一样。
avatar
r*g
5
这个题昨天我睡觉时候想了下,就是dp,每个点维护一个hashtable,这个table维护的
key是从这个点向前可能的间隔,value是连续的sequence个数,每个节点构建自己
hashtable的时候和它前面所有节点比较。
这里关键就是,假设节点2,5,10。10这个点和前面的interval只能是5或者8,那么就
要求前面的点向前也必须contain5或者8的key,所以每个节点的hashtable的size只是O
(n),算法存储和效率都是O(n^2)。
求斜率的话复杂度也是O(n^2)吧。
avatar
j*w
6
google interview experience:
http://myguiding.com/viewforum.php?f=32

【在 B*******1 的大作中提到】
: Given an array of integers A, give an algorithm to find the longest
: Arithmetic progression in it, i.e find a sequence i1 < i2 < … < ik, such
: that
: A[i1], A[i2], …, A[ik] forms an arithmetic progression, and k is the
: largest possible.
: The sequence S1, S2, …, Sk is called an arithmetic progression if
: Sj+1 – Sj is a constant
: should be DP, but could not figure out the dp function.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。