-
Notifications
You must be signed in to change notification settings - Fork 396
/
Copy pathproductOfArrayExceptItself.java
77 lines (51 loc) · 1.84 KB
/
productOfArrayExceptItself.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Given an array nums of n integers where n > 1, return an array output such that output[i]
is equal to the product of all the elements of nums except nums[i].
Example:
Input: [1,2,3,4]
Output: [24,12,8,6]
Note: Please solve it without division and in O(n).
//KEY, is to realize it is just product of left * product of right,
//TC: O(N) to go through nums array and populate L,R,ans
//SC: O(N) for space for L and r
class Solution {
public int[] productExceptSelf(int[] nums) {
int length = nums.length;
int[] ans = new int[length];
int [] L = new int[length];
int [] R = new int[length];
L[0] = 1;
for(int i=1; i<length; i++)
{
L[i] = nums[i-1] * L[i-1];
}
R[length-1] = 1;
for(int i=length-2; i>=0; i--)
{
R[i] = nums[i+1] * R[i+1];
}
for(int i=0; i<length; i++)
{
ans[i] = L[i] * R[i];
}
return ans;
}
}
OPTIMIZED, O(1) space!!!
NOTE, THIS IS ASSUMING OUTPUT ARRAY DOES NOT COUNT AS ADDITIONAL SPACE!!!
//TC: O(N) to run through all elements in array
//SC: O(1) because output array doesn't count as extra space in this problem satement
class Solution {
public int[] productExceptSelf(int[] nums) {
int [] ans = new int[nums.length];
ans[0] = 1; //FOR THE FIRST ELEMENT, there is nothing to left, so set val to 1
for(int i=1; i<nums.length; i++){
ans[i] = ans[i-1] * nums[i-1];
}
int R = 1; //FOR LASt ELEMENT, there is nothing to right, so set val to 1
for(int i=nums.length-1; i>=0; i--){
ans[i] = ans[i] * R;
R = R * nums[i];
}
return ans;
}
}