|
| 1 | +// https://leetcode.com/problems/product-of-array-except-self/ |
| 2 | +import 'package:test/test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + final stopwatch = Stopwatch()..start(); |
| 6 | + runTests(); |
| 7 | + |
| 8 | + /// |
| 9 | + stopwatch.stop(); |
| 10 | + print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro sec'); |
| 11 | +} |
| 12 | + |
| 13 | +class Solution { |
| 14 | + ////! Enhance Prefix Sum And Better Solution |
| 15 | + List<int> productExceptSelf(List<int> nums) { |
| 16 | + List<int> result = List.filled(nums.length, 1); |
| 17 | + int total = 1; |
| 18 | + int zerosCount = 0; |
| 19 | + for (int i = 1; i < nums.length; i++) { |
| 20 | + total *= nums[i - 1]; |
| 21 | + result[i] = total; |
| 22 | + if (nums[i] == 0) zerosCount++; |
| 23 | + if (zerosCount >= 2) return List.filled(nums.length, 0); |
| 24 | + } |
| 25 | + total = 1; |
| 26 | + for (int i = nums.length - 2; i >= 0; i--) { |
| 27 | + total *= nums[i + 1]; |
| 28 | + result[i] = total * result[i]; |
| 29 | + } |
| 30 | + return result; |
| 31 | + } |
| 32 | + |
| 33 | + ////! Under Standing using prefix sum |
| 34 | + // List<int> productExceptSelf(List<int> nums) { |
| 35 | + // List<int> right = List.filled(nums.length, 1); |
| 36 | + // List<int> left = List.filled(nums.length, 1); |
| 37 | + // List<int> result = List.filled(nums.length, 0); |
| 38 | + // int total = 1; |
| 39 | + // int zerosCount = 0; |
| 40 | + // for (int i = 1; i < nums.length; i++) { |
| 41 | + // total *= nums[i - 1]; |
| 42 | + // right[i] = total; |
| 43 | + // result[i] = right[i]; |
| 44 | + // if (nums[i] == 0) zerosCount++; |
| 45 | + // if (zerosCount >= 2) return List.filled(nums.length, 0); |
| 46 | + // } |
| 47 | + // total = 1; |
| 48 | + // for (int i = nums.length - 2; i >= 0; i--) { |
| 49 | + // total *= nums[i + 1]; |
| 50 | + // left[i] = total; |
| 51 | + // result[i] = left[i] * right[i]; |
| 52 | + // } |
| 53 | + // return result; |
| 54 | + // } |
| 55 | + |
| 56 | + ////! Accepted From the first time. TC: O(2n) = O(n) |
| 57 | + // List<int> productExceptSelf(List<int> nums) { |
| 58 | + // List<int> result = List.filled(nums.length, 0); |
| 59 | + // int total = 1; |
| 60 | + // int zerosCount = 0; |
| 61 | + // for (int i = 0; i < nums.length; i++) { |
| 62 | + // if (nums[i] != 0) { |
| 63 | + // total *= nums[i]; |
| 64 | + // } else { |
| 65 | + // zerosCount++; |
| 66 | + // if (zerosCount >= 2) break; |
| 67 | + // } |
| 68 | + // } |
| 69 | + // if (zerosCount >= 2) return result; |
| 70 | + // for (int i = 0; i < nums.length; i++) { |
| 71 | + // if (nums[i] != 0 && zerosCount == 1) { |
| 72 | + // result[i] = 0; |
| 73 | + // } else if (nums[i] == 0 && zerosCount == 1) { |
| 74 | + // result[i] = total; |
| 75 | + // } else if (nums[i] != 0 && zerosCount != 1) { |
| 76 | + // result[i] = total ~/ nums[i]; |
| 77 | + // } |
| 78 | + // } |
| 79 | + // return result; |
| 80 | + // } |
| 81 | +} |
| 82 | + |
| 83 | +void runTests() { |
| 84 | + final Solution s = Solution(); |
| 85 | + |
| 86 | + group('Product of Array Except Self', () { |
| 87 | + // Basic examples from problem statement |
| 88 | + test('Example 1: [1,2,3,4] → [24,12,8,6]', () { |
| 89 | + expect(s.productExceptSelf([1, 2, 3, 4]), equals([24, 12, 8, 6])); |
| 90 | + }); |
| 91 | + |
| 92 | + test('Example 2: [-1,1,0,-3,3] → [0,0,9,0,0]', () { |
| 93 | + expect(s.productExceptSelf([-1, 1, 0, -3, 3]), equals([0, 0, 9, 0, 0])); |
| 94 | + }); |
| 95 | + |
| 96 | + // Edge cases |
| 97 | + test('Two elements: [3,5] → [5,3]', () { |
| 98 | + expect(s.productExceptSelf([3, 5]), equals([5, 3])); |
| 99 | + }); |
| 100 | + |
| 101 | + test('Two elements with zero: [0,4] → [4,0]', () { |
| 102 | + expect(s.productExceptSelf([0, 4]), equals([4, 0])); |
| 103 | + }); |
| 104 | + |
| 105 | + // Zero cases |
| 106 | + test('Single zero: [1,0,3] → [0,3,0]', () { |
| 107 | + expect(s.productExceptSelf([1, 0, 3]), equals([0, 3, 0])); |
| 108 | + }); |
| 109 | + |
| 110 | + test('Multiple zeros: [0,2,0,4] → [0,0,0,0]', () { |
| 111 | + expect(s.productExceptSelf([0, 2, 0, 4]), equals([0, 0, 0, 0])); |
| 112 | + }); |
| 113 | + |
| 114 | + // Negative numbers |
| 115 | + test('All negatives: [-2,-3,-4] → [12,8,6]', () { |
| 116 | + expect(s.productExceptSelf([-2, -3, -4]), equals([12, 8, 6])); |
| 117 | + }); |
| 118 | + |
| 119 | + test('Mixed positives and negatives: [2,-1,3,-4] → [12,-24,8,-6]', () { |
| 120 | + expect(s.productExceptSelf([2, -1, 3, -4]), equals([12, -24, 8, -6])); |
| 121 | + }); |
| 122 | + |
| 123 | + // Large numbers |
| 124 | + test('Large numbers: [10,20,30] → [600,300,200]', () { |
| 125 | + expect(s.productExceptSelf([10, 20, 30]), equals([600, 300, 200])); |
| 126 | + }); |
| 127 | + |
| 128 | + test('With maximum constraint value: [30,30] → [30,30]', () { |
| 129 | + expect(s.productExceptSelf([30, 30]), equals([30, 30])); |
| 130 | + }); |
| 131 | + |
| 132 | + // Minimum length case |
| 133 | + test('Minimum length array: [2,3] → [3,2]', () { |
| 134 | + expect(s.productExceptSelf([2, 3]), equals([3, 2])); |
| 135 | + }); |
| 136 | + |
| 137 | + // Large array |
| 138 | + test('Large array with alternating 1s and 2s', () { |
| 139 | + final nums = List.generate(100000, (i) => i.isEven ? 1 : 2); |
| 140 | + final expected = |
| 141 | + List.generate(100000, (i) => i.isEven ? 1 << 99999 : 1 << 99998); |
| 142 | + expect(s.productExceptSelf(nums), equals(expected)); |
| 143 | + }); |
| 144 | + |
| 145 | + // Special cases |
| 146 | + test('All ones: [1,1,1,1] → [1,1,1,1]', () { |
| 147 | + expect(s.productExceptSelf([1, 1, 1, 1]), equals([1, 1, 1, 1])); |
| 148 | + }); |
| 149 | + |
| 150 | + test('One element zero others one: [1,0,1,1] → [0,1,0,0]', () { |
| 151 | + expect(s.productExceptSelf([1, 0, 1, 1]), equals([0, 1, 0, 0])); |
| 152 | + }); |
| 153 | + |
| 154 | + // Random cases |
| 155 | + test('Random case 1: [2,3,5,7] → [105,70,42,30]', () { |
| 156 | + expect(s.productExceptSelf([2, 3, 5, 7]), equals([105, 70, 42, 30])); |
| 157 | + }); |
| 158 | + |
| 159 | + test('Random case 2: [1,2,3,0,5] → [0,0,0,30,0]', () { |
| 160 | + expect(s.productExceptSelf([1, 2, 3, 0, 5]), equals([0, 0, 0, 30, 0])); |
| 161 | + }); |
| 162 | + }); |
| 163 | +} |
0 commit comments