generated from filipe1309/shubcogen-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
solution.ts
81 lines (70 loc) · 2.34 KB
/
solution.ts
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
77
78
79
80
81
// Test: make test t=array-of-products
function arrayOfProducts(array: number[]): number[] {
// return mySolution1(array); // time O(n^2) | space O(n)
// return solution1(array); // time O(n^2) | space O(n)
// return solution2(array); // time O(n) | space O(n)
return solution2(array); // time O(n) | space O(n)
}
// Brute force approach
// Complexity (worst-case): time O(n^2) | space O(n)
function mySolution1(array: number[]): number[] {
let result: number[] = [];
for (let i = 0; i < array.length; i++) {
const tmp = array[i];
array[i] = 1;
result.push(array.reduce((prev, curr) => prev*curr));
array[i] = tmp;
}
return result;
}
// Brute force approach
// Complexity (worst-case): time O(n^2) | space O(n)
function solution1(array: number[]): number[] {
let products: number[] = Array(array.length).fill(1);
for (let i = 0; i < array.length; i++) {
let runningProduct = 1;
for (let j = 0; j < array.length; j++) {
if (i !== j) runningProduct *= array[j];
}
products[i] = runningProduct;
}
return products;
}
// Left/Right approach
// Complexity (worst-case): time O(n^2) | space O(n)
function solution2(array: number[]): number[] {
let products: number[] = Array(array.length).fill(1);
let leftProducts: number[] = Array(array.length).fill(1);
let rightProducts: number[] = Array(array.length).fill(1);
let leftRunningProducts = 1;
for (let i = 0; i < array.length; i++) {
leftProducts[i] = leftRunningProducts
leftRunningProducts *= array[i];
}
let rightRunningProducts = 1;
for (let i = array.length - 1; i >= 0; i--) {
rightProducts[i] = rightRunningProducts
rightRunningProducts *= array[i];
}
for (let i = 0; i < array.length; i++) {
products[i] = leftProducts[i] * rightProducts[i];
}
return products;
}
// Left/Right Optimized approach
// Complexity (worst-case): time O(n^2) | space O(n)
function solution3(array: number[]): number[] {
let products: number[] = Array(array.length).fill(1);
let leftRunningProducts = 1;
for (let i = 0; i < array.length; i++) {
products[i] = leftRunningProducts
leftRunningProducts *= array[i];
}
let rightRunningProducts = 1;
for (let i = array.length - 1; i >= 0; i--) {
products[i] *= rightRunningProducts
rightRunningProducts *= array[i];
}
return products;
}
export default arrayOfProducts;