avatar
请教个面试题# JobHunting - 待字闺中
w*n
1
Write a function that is given an array of integers. It should return true
if and only if there are two distinct indices i and j into the array such
that the difference between arr[i] and arr[j] is at most l and the
difference between i and j is at most k.
存在 O(n) 的解法么?我现在能想到的就是maintain一个 l 大小的sorted list 然后
一路扫过去。每次删头和加尾需要log l。然后判断新插入的数和相邻数是否满足 dif
< k。 就是O(n log l)。
avatar
h*d
2
Sorted可以做到O(n)
没sorted应该做不到

★ 发自iPhone App: ChineseWeb 7.8

【在 w******n 的大作中提到】
: Write a function that is given an array of integers. It should return true
: if and only if there are two distinct indices i and j into the array such
: that the difference between arr[i] and arr[j] is at most l and the
: difference between i and j is at most k.
: 存在 O(n) 的解法么?我现在能想到的就是maintain一个 l 大小的sorted list 然后
: 一路扫过去。每次删头和加尾需要log l。然后判断新插入的数和相邻数是否满足 dif
: < k。 就是O(n log l)。

avatar
i*e
3
sliding window min/max.
if anyone window has max-min>l, then return false, else true.
avatar
c*w
4
public boolean findCloseNumbers(int[] A, int K, int L) {
Map map = new HashMap<>();
for (int i=0; iif (i > K) map.remove(A[i-K-1]/L);
int key = A[i]/L;
if ( map.containsKey(key)
|| map.containsKey(key+1) && map.get(key+1) - A[i] <= L
|| map.containsKey(key-1) && A[i] - map.get(key-1) <= L )
return true;
map.put(key,A[i]);
}
return false;
}
avatar
m*8
5
Nice solution!
我有个小问题, 当做key的时候,是不是应该对L+1做?
就是那个 int key = A[i]/(L+1) ?
谢谢。

【在 c******w 的大作中提到】
: public boolean findCloseNumbers(int[] A, int K, int L) {
: Map map = new HashMap<>();
: for (int i=0; i: if (i > K) map.remove(A[i-K-1]/L);
: int key = A[i]/L;
: if ( map.containsKey(key)
: || map.containsKey(key+1) && map.get(key+1) - A[i] <= L
: || map.containsKey(key-1) && A[i] - map.get(key-1) <= L )
: return true;
: map.put(key,A[i]);

avatar
c*w
6
哦!好像是应该用L+1做

【在 m********8 的大作中提到】
: Nice solution!
: 我有个小问题, 当做key的时候,是不是应该对L+1做?
: 就是那个 int key = A[i]/(L+1) ?
: 谢谢。

avatar
s*d
7
why devided by L?

【在 c******w 的大作中提到】
: public boolean findCloseNumbers(int[] A, int K, int L) {
: Map map = new HashMap<>();
: for (int i=0; i: if (i > K) map.remove(A[i-K-1]/L);
: int key = A[i]/L;
: if ( map.containsKey(key)
: || map.containsKey(key+1) && map.get(key+1) - A[i] <= L
: || map.containsKey(key-1) && A[i] - map.get(key-1) <= L )
: return true;
: map.put(key,A[i]);

avatar
l*n
8
好牛逼的解法!!!佩服

【在 c******w 的大作中提到】
: public boolean findCloseNumbers(int[] A, int K, int L) {
: Map map = new HashMap<>();
: for (int i=0; i: if (i > K) map.remove(A[i-K-1]/L);
: int key = A[i]/L;
: if ( map.containsKey(key)
: || map.containsKey(key+1) && map.get(key+1) - A[i] <= L
: || map.containsKey(key-1) && A[i] - map.get(key-1) <= L )
: return true;
: map.put(key,A[i]);

avatar
l*n
9
好牛逼的解法!!!佩服

【在 c******w 的大作中提到】
: public boolean findCloseNumbers(int[] A, int K, int L) {
: Map map = new HashMap<>();
: for (int i=0; i: if (i > K) map.remove(A[i-K-1]/L);
: int key = A[i]/L;
: if ( map.containsKey(key)
: || map.containsKey(key+1) && map.get(key+1) - A[i] <= L
: || map.containsKey(key-1) && A[i] - map.get(key-1) <= L )
: return true;
: map.put(key,A[i]);

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