由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 求教 合并两数组 并排除重复
相关主题
问一个merge k sorted array的问题Amazon 面试题
两个Sorted Array,找K smallest element2SUM, unsorted, print all index including duplicates 能O(nlogn)解决么
divide array into two, sum of difference is min in O(N)昨天有人讲过的啥de啥的是怎么回事有人知道么
re: 面试归来,上面经回馈各位战友一个特别的inplace merge two sorted arrays
一道面试题再问一个算法题。
merge k个数组怎样的方法好?请教一道面试题
请问两道题amazon tel interview
find sum of three number in arrayFB电面
相关话题的讨论汇总
话题: int话题: array话题: max话题: time话题: merge
进入JobHunting版参与讨论
1 (共1页)
v********a
发帖数: 14
1
Merge two unsorted array. Each array has unique values, but there are
dupliates between two arrays. Remove the duplicates and merge them. Time
complexity must be better than O(nlogn). You shouldn't use hash table.
q****x
发帖数: 7404
2
no way, unless count sort applicable.

【在 v********a 的大作中提到】
: Merge two unsorted array. Each array has unique values, but there are
: dupliates between two arrays. Remove the duplicates and merge them. Time
: complexity must be better than O(nlogn). You shouldn't use hash table.

c**v
发帖数: 9
3
ding....
q********c
发帖数: 1774
4
what's the data type? string, int, double, ...?
s***n
发帖数: 373
5
这个考古前面bloomberg的题吧
记得后面跟贴说,一个array远长于另外一个
这样sort short array first, O(MlogM)
search in short array N*log(M)
if N>>M, close to O(N)

【在 v********a 的大作中提到】
: Merge two unsorted array. Each array has unique values, but there are
: dupliates between two arrays. Remove the duplicates and merge them. Time
: complexity must be better than O(nlogn). You shouldn't use hash table.

q********c
发帖数: 1774
6
For integers, can do it in O(n) for sure.
r****t
发帖数: 10904
7
"no hash table" means literally no hash table, or any space cost is allowed?

【在 v********a 的大作中提到】
: Merge two unsorted array. Each array has unique values, but there are
: dupliates between two arrays. Remove the duplicates and merge them. Time
: complexity must be better than O(nlogn). You shouldn't use hash table.

b*****e
发帖数: 131
8
Time: O((m+n)logn)
Space: O(1)
using namespace std;
void union(int* a, int m, int* b, int n, int* res, int &len)
{
if(m < n) {
swap(a, b);
swap(m, n);
}
sort(b, b+n);
int* p;
for(int i = 0; i < m; i++) {
p = find(b, b+n, a[i]);
if(p != b+n)
*p = INT_MIN;
}
copy(a, a+m, res);
len = m;
for(int i = 0; i < n; i++)
if(b[i] != INT_MIN)
res[len++] = b[i];
}
v********a
发帖数: 14
9
how to do it?
please specify, please.

【在 q********c 的大作中提到】
: For integers, can do it in O(n) for sure.
q********c
发帖数: 1774
10
Basic idea is use bit map.
Assume the max of the two arrays is Max. If it's not given, it can be
found
in linear time.
int* merge(int* a, int len1, int* b, int len2)
{
int[] array = new int[Max];
for(int i = 0; i < len1; ++i)
{
array[a[i]] = 1;
}
for(int i = 0; i < len2; ++i)
{
if(array[b[i]] != 1)
{ array[b[i]] = 1; }
}
int num = 0;
for(int i = 0; i < Max; ++i)
{
if(array[i] == 1)
{ num++; }
}
int[] list = new list[num];
int j = 0;
for(int i = 0; i < Max; ++i)
{
if(array[i] == 1)
{
list[j++] = i;
}
}
return list;
}
相关主题
merge k个数组怎样的方法好?Amazon 面试题
请问两道题2SUM, unsorted, print all index including duplicates 能O(nlogn)解决么
find sum of three number in array昨天有人讲过的啥de啥的是怎么回事有人知道么
进入JobHunting版参与讨论
m*********0
发帖数: 46
11
这个方法不错,觉得本质上和hash table一个思路。如果integer是long的话,extra
space可能会非常大。
有个简单的想法,先分别sort两个数组,O(nlogn) + O(mlogm). 然后merge,需要O(m+
n).

found

【在 q********c 的大作中提到】
: Basic idea is use bit map.
: Assume the max of the two arrays is Max. If it's not given, it can be
: found
: in linear time.
: int* merge(int* a, int len1, int* b, int len2)
: {
: int[] array = new int[Max];
: for(int i = 0; i < len1; ++i)
: {
: array[a[i]] = 1;

q********c
发帖数: 1774
12
Unique values should be a trigger to use bit map. It's the simplest form of
hashtable, but nobody would say it's a real hashtable.
q****x
发帖数: 7404
13
not O(n) + O(1).

found

【在 q********c 的大作中提到】
: Basic idea is use bit map.
: Assume the max of the two arrays is Max. If it's not given, it can be
: found
: in linear time.
: int* merge(int* a, int len1, int* b, int len2)
: {
: int[] array = new int[Max];
: for(int i = 0; i < len1; ++i)
: {
: array[a[i]] = 1;

q********c
发帖数: 1774
14
What are you trying to say quantx? Are you saying linear time and no extra
space? If so, no-extra space is not required in the problem.
q****x
发帖数: 7404
15
what you use is hash table.

【在 q********c 的大作中提到】
: What are you trying to say quantx? Are you saying linear time and no extra
: space? If so, no-extra space is not required in the problem.

q********c
发帖数: 1774
16
I already said, it's NOT a real hashtable. Although theoretically you could
call bit map a hashtable, nobody would talk like this in real coding.
q****x
发帖数: 7404
17
check clrs. your bit map is actually direct addressing, which is the
primitive of hash table.

could

【在 q********c 的大作中提到】
: I already said, it's NOT a real hashtable. Although theoretically you could
: call bit map a hashtable, nobody would talk like this in real coding.

r****t
发帖数: 10904
18
面官估计水平没到这个地步,别争了,以前也有类似状况过。

【在 q****x 的大作中提到】
: check clrs. your bit map is actually direct addressing, which is the
: primitive of hash table.
:
: could

1 (共1页)
进入JobHunting版参与讨论
相关主题
FB电面一道面试题
Bloomberg onsite interviewmerge k个数组怎样的方法好?
讨论一道两个linked list的题目请问两道题
专家们,find the longest common substring of two stringsfind sum of three number in array
问一个merge k sorted array的问题Amazon 面试题
两个Sorted Array,找K smallest element2SUM, unsorted, print all index including duplicates 能O(nlogn)解决么
divide array into two, sum of difference is min in O(N)昨天有人讲过的啥de啥的是怎么回事有人知道么
re: 面试归来,上面经回馈各位战友一个特别的inplace merge two sorted arrays
相关话题的讨论汇总
话题: int话题: array话题: max话题: time话题: merge