1
1
// https://leetcode.com/problems/product-of-array-except-self/
2
+
2
3
import 'package:test/test.dart' ;
3
4
4
5
void main () {
@@ -11,7 +12,8 @@ void main() {
11
12
}
12
13
13
14
class Solution {
14
- ////! Enhance Prefix Sum And Better Solution
15
+ ////! Enhance Prefix Product And Better Solution
16
+ ////! TC: O(2n) = O(n) --- SC: O(n)
15
17
List <int > productExceptSelf (List <int > nums) {
16
18
List <int > result = List .filled (nums.length, 1 );
17
19
int total = 1 ;
@@ -30,7 +32,27 @@ class Solution {
30
32
return result;
31
33
}
32
34
33
- ////! Under Standing using prefix sum
35
+ ////! Prefix Product From Udemy Video
36
+ // List<int> productExceptSelf(List<int> nums) {
37
+ // List<int> prefixProduct = List.filled(nums.length, 1);
38
+ // prefixProduct[0] = 1;
39
+ // int zerosCount = 0;
40
+ // for (int i = 1; i < nums.length; i++) {
41
+ // prefixProduct[i] = prefixProduct[i - 1] * nums[i - 1];
42
+ // if (nums[i] == 0) zerosCount++;
43
+ // if (zerosCount >= 2) return List.filled(nums.length, 0);
44
+ // }
45
+ // int suffixProductTotal = 1;
46
+
47
+ // for (int i = nums.length - 1; i >= 0; i--) {
48
+ // prefixProduct[i] = prefixProduct[i] * suffixProductTotal;
49
+ // suffixProductTotal *= nums[i];
50
+ // }
51
+
52
+ // return prefixProduct;
53
+ // }
54
+
55
+ ////! Under Standing using prefix Product
34
56
// List<int> productExceptSelf(List<int> nums) {
35
57
// List<int> right = List.filled(nums.length, 1);
36
58
// List<int> left = List.filled(nums.length, 1);
@@ -53,7 +75,8 @@ class Solution {
53
75
// return result;
54
76
// }
55
77
56
- ////! Accepted From the first time. TC: O(2n) = O(n)
78
+ ////! Accepted From the first time. TC: O(2n) = O(n) --- SC: O(n)
79
+ ////! Bad Solution With
57
80
// List<int> productExceptSelf(List<int> nums) {
58
81
// List<int> result = List.filled(nums.length, 0);
59
82
// int total = 1;
0 commit comments