由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教个面试题
相关主题
Google电话面试题目问道面试题
请教一道题目问个关于排序的面试题
请教一个问题的答案,好像以前有人讨论过算法题:两列找共同元素有O(n)的算法吗?
google 面试题疑问A家面试题
面试题求解:remove first duplicate number from an array一个小公司面经
有些面试题是够扯蛋的[算法] unsorted array
问道题,谁给个效率高点的解法问一道老题
[合集] Google电话面试题目a[i] + b[j] = c[k] 的题有靠谱的答案不?
相关话题的讨论汇总
话题: key话题: int话题: return话题: integer话题: arr
进入JobHunting版参与讨论
1 (共1页)
w******n
发帖数: 61
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)。
h**d
发帖数: 630
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)。

i*********e
发帖数: 21
3
sliding window min/max.
if anyone window has max-min>l, then return false, else true.
c******w
发帖数: 1108
4
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]);
}
return false;
}
m********8
发帖数: 36
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]);

c******w
发帖数: 1108
6
哦!好像是应该用L+1做

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

s******d
发帖数: 9806
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]);

l*****n
发帖数: 246
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]);

l*****n
发帖数: 246
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]);

1 (共1页)
进入JobHunting版参与讨论
相关主题
a[i] + b[j] = c[k] 的题有靠谱的答案不?面试题求解:remove first duplicate number from an array
问几个老算法题的最佳解法有些面试题是够扯蛋的
一道算法题问道题,谁给个效率高点的解法
Given an int array and an int value. Find all pairs in arr[合集] Google电话面试题目
Google电话面试题目问道面试题
请教一道题目问个关于排序的面试题
请教一个问题的答案,好像以前有人讨论过算法题:两列找共同元素有O(n)的算法吗?
google 面试题疑问A家面试题
相关话题的讨论汇总
话题: key话题: int话题: return话题: integer话题: arr