avatar
a*l
1
There is an array A[N] of N numbers. You have to compose an array Output[N]
such that Output[i] will be equal to multiplication of all the elements of A
[N] except A[i]. For example Output[0] will be multiplication of A[1] to A[N
-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1].
Solve it without division operator and in O(n).
那位达人给说一个solution?
avatar
s*x
3
unit model? let x=10^k greater than the multiplication of all A[i], then
compute (x-A[1])(x^2-A[2])(x^4-A[3])....(x^(2^N/2)-A[N]) and some co-
efficient give the Output array.

]
A
[N

【在 a****l 的大作中提到】
: There is an array A[N] of N numbers. You have to compose an array Output[N]
: such that Output[i] will be equal to multiplication of all the elements of A
: [N] except A[i]. For example Output[0] will be multiplication of A[1] to A[N
: -1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1].
: Solve it without division operator and in O(n).
: 那位达人给说一个solution?

avatar
a*l
5
怎么觉得不对呢

【在 s*x 的大作中提到】
: unit model? let x=10^k greater than the multiplication of all A[i], then
: compute (x-A[1])(x^2-A[2])(x^4-A[3])....(x^(2^N/2)-A[N]) and some co-
: efficient give the Output array.
:
: ]
: A
: [N

avatar
w*i
6
那就努力减肥

【在 J********i 的大作中提到】
: 吨位不及我
avatar
c*g
7
make two arrays L[i] and R[i]
where L[i] = Product of all elements left to i in A
So, A = [4,3,2,1,2]
Then, L = [1,4,12,24,24]
Similarly, R[i] is product of all elements to the right of i in A
R = [12,4,2,2,1]
Note that L and R can be calculated in O(n)
So, B[i] = L[i]*R[i] can be calculated in O(n)
B = [12, 16, 24, 48, 24]
avatar
J*i
8
ff说我这样手感刚好

【在 w***i 的大作中提到】
: 那就努力减肥
avatar
c*g
9
Note that L and R can be calculated in O(n)
So, B[i] = L[i]*R[i] can be calculated in O(n)
avatar
w*i
10




【在 J********i 的大作中提到】
: ff说我这样手感刚好
avatar
m*f
11
这是DP的思想阿, 需要O(n) space. 原题有说space要求吗? lz你的O(n)是指space还是time还是both...
可以有很多方法模拟division operator得,比如用左移法
1. 算出 N = A[0]*A[1]...A[n]
2. 要算output[i]
3. 循环把A[i] << 1直到大于N, 然后相减, 然后重复, 最后得出结果
这里的复杂度是一个常数 log(N)

【在 c********g 的大作中提到】
: make two arrays L[i] and R[i]
: where L[i] = Product of all elements left to i in A
: So, A = [4,3,2,1,2]
: Then, L = [1,4,12,24,24]
: Similarly, R[i] is product of all elements to the right of i in A
: R = [12,4,2,2,1]
: Note that L and R can be calculated in O(n)
: So, B[i] = L[i]*R[i] can be calculated in O(n)
: B = [12, 16, 24, 48, 24]

avatar
g*y
13
空间不是问题,因为既然要生成新的Output数组,就利用这个空间就够了。

space还是time还是both...

【在 m*****f 的大作中提到】
: 这是DP的思想阿, 需要O(n) space. 原题有说space要求吗? lz你的O(n)是指space还是time还是both...
: 可以有很多方法模拟division operator得,比如用左移法
: 1. 算出 N = A[0]*A[1]...A[n]
: 2. 要算output[i]
: 3. 循环把A[i] << 1直到大于N, 然后相减, 然后重复, 最后得出结果
: 这里的复杂度是一个常数 log(N)

avatar
J*i
14
哪里有嫩草

【在 d********e 的大作中提到】
: 在啃嫩草?
avatar
m*f
15
上述算法需要2O(n)空间, 所以严格来说还是不够啊

【在 g*******y 的大作中提到】
: 空间不是问题,因为既然要生成新的Output数组,就利用这个空间就够了。
:
: space还是time还是both...

avatar
d*e
16
他叼着的

【在 J********i 的大作中提到】
: 哪里有嫩草
avatar
g*y
17
不用。
第一遍,在output里算left数组
第二遍,用一个temp变量来算那个right数组,再乘到output数组上。

【在 m*****f 的大作中提到】
: 上述算法需要2O(n)空间, 所以严格来说还是不够啊
avatar
J*i
18
玫瑰啊,大妹子

【在 d********e 的大作中提到】
: 他叼着的
avatar
c*g
19
2*O(N) =??
you might need to read "algothm analysis" textbook again.^_^
avatar
d*e
20
就看见几片叶子

【在 J********i 的大作中提到】
: 玫瑰啊,大妹子
avatar
c*g
21
>这里的复杂度是一个常数 log(N)
you might need to read "algothm analysis" textbook again also.^_^
avatar
J*i
22
可能是边上有个女人,就剪掉了

【在 d********e 的大作中提到】
: 就看见几片叶子
avatar
R*r
23
why not calculate the product of all elements in A, then output[i] = product
/ A[i], this seems much straight forward.
avatar
R*r
24
sorry, my bad, did not see no division allow.
avatar
m*f
25
N是乘积不是n,
看清楚点再说好么, 别动不动教训人, 最烦这样的

【在 c********g 的大作中提到】
: >这里的复杂度是一个常数 log(N)
: you might need to read "algothm analysis" textbook again also.^_^

avatar
m*f
26
题目就提供一个数组空间的话, 你生成两组数不就是不够了
你根本不明白我的意思. 前面已经说了, 要用一个数组和一个变量, 所以你的
算法不完全对, 讨论的很清楚了
最烦你这样自以为是, 动不动居高临下教训人的, 自己再去学学礼貌这本书吧

【在 c********g 的大作中提到】
: 2*O(N) =??
: you might need to read "algothm analysis" textbook again.^_^

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