avatar
B*1
1
Give an unsorted array find count of pairs of numbers[a,b] where a > b and b
comes after a in the array.
Eg. {8,3,6,10,5}
the count of such numbers is 5. i.e. (8,3), (8,6), (8,5), (6,5) and (10,5)
觉得这题跟amazon的这个很像,大家有没有这个感觉啊?
You have an array like ar[]= {1,3,2,4,5,4,2}. You need to create another
array ar_low[] such that ar_low[i] = number of elements lower than or equal
to ar[i] in ar[i+1:n-1].
So the output of above should be {0,2,1,2,2,1,0}
Time complexity : O(n)
use of extra space allowed.
avatar
z*n
2
节日快乐!
avatar
m*t
3
就是这道题。只能 达到 O(nlgn)
http://stackoverflow.com/questions/3836767/interview-question-r

b
equal

【在 B*******1 的大作中提到】
: Give an unsorted array find count of pairs of numbers[a,b] where a > b and b
: comes after a in the array.
: Eg. {8,3,6,10,5}
: the count of such numbers is 5. i.e. (8,3), (8,6), (8,5), (6,5) and (10,5)
: 觉得这题跟amazon的这个很像,大家有没有这个感觉啊?
: You have an array like ar[]= {1,3,2,4,5,4,2}. You need to create another
: array ar_low[] such that ar_low[i] = number of elements lower than or equal
: to ar[i] in ar[i+1:n-1].
: So the output of above should be {0,2,1,2,2,1,0}
: Time complexity : O(n)

avatar
k*u
4
跟着排

节日快乐!

【在 z****n 的大作中提到】
: 节日快乐!
avatar
g*y
5
这个是求总和,比Amazon的简单。要是Amazon那个O(n)可解,这个也可以。但是这个有
O(n)解,不表明Amazon那个也有。

b
equal

【在 B*******1 的大作中提到】
: Give an unsorted array find count of pairs of numbers[a,b] where a > b and b
: comes after a in the array.
: Eg. {8,3,6,10,5}
: the count of such numbers is 5. i.e. (8,3), (8,6), (8,5), (6,5) and (10,5)
: 觉得这题跟amazon的这个很像,大家有没有这个感觉啊?
: You have an array like ar[]= {1,3,2,4,5,4,2}. You need to create another
: array ar_low[] such that ar_low[i] = number of elements lower than or equal
: to ar[i] in ar[i+1:n-1].
: So the output of above should be {0,2,1,2,2,1,0}
: Time complexity : O(n)

avatar
d*d
6
use a bst, each node contain an extra field which hold the number of nodes
in the tree.
avatar
h*6
7
用二叉索引树很容易的吧。
avatar
d*d
8
不一样,但是差不多.
avatar
g*y
9
O(nlogn)不难,但是肯定没有O(n)的解吗?

【在 h**6 的大作中提到】
: 用二叉索引树很容易的吧。
avatar
r*g
10
hi
你们的思路是,对unsorted array做bst?然后呢?

【在 g**********y 的大作中提到】
: O(nlogn)不难,但是肯定没有O(n)的解吗?
avatar
g*y
11
CLRS习题2.4-d
public class InversionCount {
public int count(int[] a) {
return mergeSort(a, 0, a.length - 1);
}

private int mergeSort(int[] a, int p, int r) {
if (p >= r) return 0;
int q = (p + r) / 2;
int c1 = mergeSort(a, p, q);
int c2 = mergeSort(a, q+1, r);
return c1 + c2 + merge(a, p, q, r);
}

private int merge(int[] a, int p, int q, int r) {
int[] t = new int[r - p + 1];
int count = 0;

int i = p;
int j = q+1;
int k = 0;
while (i<=q && j<=r) {
if (a[i] <= a[j]) {
t[k++] = a[i++];
}
else {
t[k++] = a[j++];
count += q-i+1;
}
}

while (i<=q) t[k++] = a[i++];
while (j<=r) t[k++] = a[j++];

for (i=p; i<=r; i++) a[i] = t[i-p];
return count;
}
}

【在 r*******g 的大作中提到】
: hi
: 你们的思路是,对unsorted array做bst?然后呢?

avatar
s*f
12
一个unsorted array建立BST的worst time complexity是O(n^2)吧?
avatar
g*y
13
hans说的应该是B-tree, 不是bst.

【在 s***f 的大作中提到】
: 一个unsorted array建立BST的worst time complexity是O(n^2)吧?
avatar
r*g
14
man,我是想问思路啊,你给我的是mergesort的实现。
mergesort和这个题什么关系?

【在 g**********y 的大作中提到】
: CLRS习题2.4-d
: public class InversionCount {
: public int count(int[] a) {
: return mergeSort(a, 0, a.length - 1);
: }
:
: private int mergeSort(int[] a, int p, int r) {
: if (p >= r) return 0;
: int q = (p + r) / 2;
: int c1 = mergeSort(a, p, q);

avatar
g*y
15
无语,你不能读读程序吗?如果不愿意读,你运行一下程序也行啊。

【在 r*******g 的大作中提到】
: man,我是想问思路啊,你给我的是mergesort的实现。
: mergesort和这个题什么关系?

avatar
r*g
16
哈哈,不好意思,刚才看的太不仔细了,你是对的,原来就是mergesort的时候不断求
和就行了,这个想法很好。thanks

【在 g**********y 的大作中提到】
: 无语,你不能读读程序吗?如果不愿意读,你运行一下程序也行啊。
avatar
d*y
17
看了你的程序想起来以前做过这题。
可能是放在merge sort后面的练习里面的。
忘了可以这么做这个题了。

【在 g**********y 的大作中提到】
: 无语,你不能读读程序吗?如果不愿意读,你运行一下程序也行啊。
avatar
g*i
18
bst怎么做? unbalance bst的话10和5在两个不同的tree里,怎么找到这对pair?

【在 d*******d 的大作中提到】
: use a bst, each node contain an extra field which hold the number of nodes
: in the tree.

avatar
d*d
19
struct Node{
int v;
int count;
Node * left;
Node * right;
Node(int _v):v(_v), count(1), left(0), right(0){}
};
void insert(Node * & root, int v, int & c){
if( root == 0){
root = new Node(v);
return;
}
root->count++;
if( root->v == v){
if(root->right)
c = c + root->right->count;
}else if( v < root->v){
insert(root->left, v, c);
c = c + root->count - root->left->count;
}else{
insert(root->right, v, c);
}
return;
}
int find(int * a, int n){
Node * root = 0;
int c = 0;
for(int i = 0; i< n; i++){
insert( root, a[i], c);
}
return c;
}

【在 g*****i 的大作中提到】
: bst怎么做? unbalance bst的话10和5在两个不同的tree里,怎么找到这对pair?
avatar
B*1
20
这个worse case 还是o(n2)吧?
avatar
l*s
21
google jobs:
http://jobguiding.com/it-jobs/it-companies/google.html

b
equal

【在 B*******1 的大作中提到】
: Give an unsorted array find count of pairs of numbers[a,b] where a > b and b
: comes after a in the array.
: Eg. {8,3,6,10,5}
: the count of such numbers is 5. i.e. (8,3), (8,6), (8,5), (6,5) and (10,5)
: 觉得这题跟amazon的这个很像,大家有没有这个感觉啊?
: You have an array like ar[]= {1,3,2,4,5,4,2}. You need to create another
: array ar_low[] such that ar_low[i] = number of elements lower than or equal
: to ar[i] in ar[i+1:n-1].
: So the output of above should be {0,2,1,2,2,1,0}
: Time complexity : O(n)

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