Title: Given an array a[0,1,..., n-1], build an array b[0,1,..., n-1], where the elements in B b[i]=a[0]*a[1]*...*a[i-1]*a[i+1]*...*a[n-1]. You cannot use division.
Analysis: This problem, the intuitive solution is: Set a loop (from 0 to n-1), calculate b[i], ignore the a[i] item, the array A in all the other items are multiplied, that is b[i]=a[0]*a[1]*...*a[i-1]*a[i+1]*...*a[n-1]. Thus, after a loop, you can get all the values in array B without division. So, what is the time complexity and space complexity of this solution? Time complexity: Because the loop is 0~n-1, O (n), in each loop, the n-1 multiplication is performed, so the time complexity of the solution is O (n) *o (n-1) =o (N2) and the space complexity is O (1).
Obviously, the above solution has a high time complexity, then there is no O (n) solution? We may as well analyze the problem of the above solution to find a breakthrough that can be optimized. The complexity of the above consists of two parts, the first part: loop by 0~n-1, obviously we need to calculate every b[i], anyway, we have no way to remove such a basic cycle, that is, this part brings time complexity O (n) can not be optimized; Part Two: every calculation b[i] All need to do n-1 multiplication, that is, it is necessary to calculate the number of n n-1 multiplied, careful people can be found that there are many multiplication is repeated, it is because of this part of the multiplication of repeated calculations caused by our time complexity is very high. So, is there a way to only calculate the number of such n-1 multiplied? We can define two intermediate arrays to store the results of the multiplication that has been computed, so that when the next B[i] calculation is done, we only need to complete a multiplication to get the results of b[i]. In this way, the complexity of Time becomes O (1), and the entire algorithm time complexity is reduced to O (n). Specific analysis ideas are as follows:
First, we can represent the array B as a matrix in the following form:
| B[0] |
1 |
A[1] |
A[2] |
... |
A[n-2] |
A[N-1] |
| B[1] |
A[0] |
1 |
A[2] |
... |
A[n-2] |
A[N-1] |
| B[2] |
A[0] |
A[1] |
1 |
... |
A[n-2] |
A[N-1] |
| ... |
... |
... |
... |
1 |
... |
... |
| B[n-2] |
A[0] |
A[1] |
A[2] |
... |
1 |
A[N-1] |
| B[N-1] |
A[0] |
A[1] |
A[2] |
... |
A[n-2] |
1 |
As shown, each row of the matrix represents an element of array B, from top to bottom, followed by b[0],b[1],... B[n-2],b[n-1]. So, we can consider each b[i] as two parts, respectively, with C[i] and d[i]. where c[i] = a[0]*a[1]*...*a[i-1],d[i] = a[i+1]*...*a[n-2]*a[n-1], so, b[i]=c[i]*1*d[i]. That is, we can update the array c[i] and d[i], that is, c[i]=c[i-1]*a[i-1] and d[i]=d[i+1]*a[i+1] (it should be explained that the update here C[i] is followed from the go, while the d[i] is from the back forward), This saves us a lot of repetitive multiplication calculations, and the time complexity is reduced to O (n).
int[] Multiply (int[] A) { if(a==NULL|| a.length<=0)//boundary conditions, preferably accompanied by the return NULL; intn =a.length; int[] B =New int[n]; b[0]=1; /*update C[i], where we do not define an array, we will directly store the results of c[i] in b[i], so that the result of D[i] is directly multiplied by b[i] (at this time b[i] equals c[i]), the final b[i], obviously for us and save a lot of space storage. */ for(inti=1;i<n;i++) {B[i]=b[i-1]*a[i-1]; } intTemp =1; //update D[i], here to start from N-2, because B[n-1] has got the final result for(intj=n-2;j>=0;j--) {Temp*=a[j+1]; B[J]*=Temp }
return B;}
Obviously, the time complexity of the above solution is: 2*o (n) *2=o (n).
Building a product array--java