Redian新闻
>
fb面经里的这个题有优于O(n^2)的解法么?
avatar
fb面经里的这个题有优于O(n^2)的解法么?# JobHunting - 待字闺中
r*7
1
You're given an array of integers(eg [3,4,7,1,2,9,8]) Find the index of
values that satisfy A+B = C + D, where A,B,C & D are integers values in the
array.
Eg: Given [3,4,7,1,2,9,8] array
The following
3+7 = 1+ 9 satisfies A+B=C+D
so print (0,2,3,5)
avatar
l*n
2
No! if you just want to find out all the possible sums, it is O(n^2), it is
hard bottom.

the

【在 r****7 的大作中提到】
: You're given an array of integers(eg [3,4,7,1,2,9,8]) Find the index of
: values that satisfy A+B = C + D, where A,B,C & D are integers values in the
: array.
: Eg: Given [3,4,7,1,2,9,8] array
: The following
: 3+7 = 1+ 9 satisfies A+B=C+D
: so print (0,2,3,5)

avatar
d*i
3
如果数组允许重复怎么做
avatar
y*t
4
o(n)就行了...
提示:从左往右+从右往左
avatar
l*n
5
怎么个从左往右从右往左啊。。。怎么想都不对啊
这array也没说是sorted

【在 y****t 的大作中提到】
: o(n)就行了...
: 提示:从左往右+从右往左

avatar
r*7
6
我觉得不一定啊,感觉sort过之后是不是可以有的case给skip掉呢?

is

【在 l******n 的大作中提到】
: No! if you just want to find out all the possible sums, it is O(n^2), it is
: hard bottom.
:
: the

avatar
r*7
7
没有sort倒是可以sort,cheaper than n^2,然后用类似2sum的算法么?可是没有固定
的target,貌似也还是要遍历很多种可能。

【在 l*****n 的大作中提到】
: 怎么个从左往右从右往左啊。。。怎么想都不对啊
: 这array也没说是sorted

avatar
n*s
8
sort完了变成1234789, 然后找A+B=C+D并且满足和在5(1+4)和13(4+9)之间的?

【在 r****7 的大作中提到】
: 没有sort倒是可以sort,cheaper than n^2,然后用类似2sum的算法么?可是没有固定
: 的target,貌似也还是要遍历很多种可能。

avatar
l*n
9
如果数组有重复的,怎么办?

【在 n*******s 的大作中提到】
: sort完了变成1234789, 然后找A+B=C+D并且满足和在5(1+4)和13(4+9)之间的?
avatar
x*9
10
如果用指针来找的话,至少要确定三个指针才能推出第四个。所以我觉得即使数组是有
序的,用双(多)指针法也是不可行的。
等大神解答。
avatar
d*6
11
就算把范围cut了,还是要计算比如n/2或者n/4内所有数字的组合,结果还是O(n^2)

【在 n*******s 的大作中提到】
: sort完了变成1234789, 然后找A+B=C+D并且满足和在5(1+4)和13(4+9)之间的?
avatar
r*7
12
You're given an array of integers(eg [3,4,7,1,2,9,8]) Find the index of
values that satisfy A+B = C + D, where A,B,C & D are integers values in the
array.
Eg: Given [3,4,7,1,2,9,8] array
The following
3+7 = 1+ 9 satisfies A+B=C+D
so print (0,2,3,5)
avatar
l*n
13
No! if you just want to find out all the possible sums, it is O(n^2), it is
hard bottom.

the

【在 r****7 的大作中提到】
: You're given an array of integers(eg [3,4,7,1,2,9,8]) Find the index of
: values that satisfy A+B = C + D, where A,B,C & D are integers values in the
: array.
: Eg: Given [3,4,7,1,2,9,8] array
: The following
: 3+7 = 1+ 9 satisfies A+B=C+D
: so print (0,2,3,5)

avatar
d*i
14
如果数组允许重复怎么做
avatar
y*t
15
o(n)就行了...
提示:从左往右+从右往左
avatar
l*n
16
怎么个从左往右从右往左啊。。。怎么想都不对啊
这array也没说是sorted

【在 y****t 的大作中提到】
: o(n)就行了...
: 提示:从左往右+从右往左

avatar
r*7
17
我觉得不一定啊,感觉sort过之后是不是可以有的case给skip掉呢?

is

【在 l******n 的大作中提到】
: No! if you just want to find out all the possible sums, it is O(n^2), it is
: hard bottom.
:
: the

avatar
r*7
18
没有sort倒是可以sort,cheaper than n^2,然后用类似2sum的算法么?可是没有固定
的target,貌似也还是要遍历很多种可能。

【在 l*****n 的大作中提到】
: 怎么个从左往右从右往左啊。。。怎么想都不对啊
: 这array也没说是sorted

avatar
n*s
19
sort完了变成1234789, 然后找A+B=C+D并且满足和在5(1+4)和13(4+9)之间的?

【在 r****7 的大作中提到】
: 没有sort倒是可以sort,cheaper than n^2,然后用类似2sum的算法么?可是没有固定
: 的target,貌似也还是要遍历很多种可能。

avatar
l*n
20
如果数组有重复的,怎么办?

【在 n*******s 的大作中提到】
: sort完了变成1234789, 然后找A+B=C+D并且满足和在5(1+4)和13(4+9)之间的?
avatar
x*9
21
如果用指针来找的话,至少要确定三个指针才能推出第四个。所以我觉得即使数组是有
序的,用双(多)指针法也是不可行的。
等大神解答。
avatar
d*6
22
就算把范围cut了,还是要计算比如n/2或者n/4内所有数字的组合,结果还是O(n^2)

【在 n*******s 的大作中提到】
: sort完了变成1234789, 然后找A+B=C+D并且满足和在5(1+4)和13(4+9)之间的?
avatar
a*0
23
vector> 2sumSum(vector& a)
{
vector> res;
size_t n=a.size();
if(n<4) return res;

unordered_map>> umap;
for(size_t i=0;ifor(size_t j=i+1;jint sum=a[i]+a[j];
if(umap.find(sum)!=umap.end()) {
for(auto e: umap[sum]){
res.push_back({i,j, e.first, e.second});
}
}

umap[sum].push_back(make_pair(i, j));
}
}
return res;
}
avatar
p*m
24
两层循环是无论如何都省不了的,这就是O(n^2)了

the

【在 r****7 的大作中提到】
: You're given an array of integers(eg [3,4,7,1,2,9,8]) Find the index of
: values that satisfy A+B = C + D, where A,B,C & D are integers values in the
: array.
: Eg: Given [3,4,7,1,2,9,8] array
: The following
: 3+7 = 1+ 9 satisfies A+B=C+D
: so print (0,2,3,5)

avatar
h*3
25
举个极端的例子,如果array中所有元素都相同,那么n^2是无法避免的
avatar
m*n
26
不是类似4sum吗? A+B-C-D=0?
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。