|
| 1 | +// Approach 3 |
| 2 | +// 🗓️ 2025-04-12 |
| 3 | +// ⏳ Time Complexity: O(n log n) + O(n^2) = O(n^2) |
| 4 | +// 💾 Space Complexity: O(1) (excluding output) |
| 5 | + |
| 6 | +function threeSum(nums: number[]): number[][] { |
| 7 | + |
| 8 | + nums.sort((a, b) => a - b); // O(n log n) |
| 9 | + let result: number[][] = []; |
| 10 | + |
| 11 | + for (let i = 0; i < nums.length; i++) { |
| 12 | + |
| 13 | + if (i > 0 && nums[i - 1] == nums[i]) continue; // skip duplicate i |
| 14 | + |
| 15 | + let low = i + 1, high = nums.length - 1; |
| 16 | + // two-pointer scan (low and high) -> takes up to O(n) time per iteration |
| 17 | + while (low < high) { |
| 18 | + const threeSum = nums[i] + nums[low] + nums[high]; |
| 19 | + if (threeSum < 0) { |
| 20 | + low += 1; |
| 21 | + } else if (threeSum > 0) { |
| 22 | + high -= 1; |
| 23 | + } else { |
| 24 | + result.push([nums[i], nums[low], nums[high]]); |
| 25 | + |
| 26 | + while (low < high && nums[low] === nums[low + 1]) low += 1 // skip duplicate low |
| 27 | + while (low < high && nums[high] === nums[high - 1]) high -= 1 // skip duplicate high |
| 28 | + |
| 29 | + low += 1; |
| 30 | + high -= 1; |
| 31 | + |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + return result |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +// Approach 2 |
| 40 | +// 🗓️ 2025-04-11 |
| 41 | +// ❌ Time Limit Exceeded 313 / 314 testcases passed |
| 42 | +// ⏳ Time Complexity: O(n^2) |
| 43 | +// 💾 Space Complexity: O(n^2) |
| 44 | + |
| 45 | +// function threeSum(nums: number[]): number[][] { |
| 46 | +// const result: number[][] = []; |
| 47 | +// const triplets = new Set<string>(); |
| 48 | + |
| 49 | +// for (let i = 0; i < nums.length - 2; i++) { |
| 50 | +// const seen = new Set<number>(); |
| 51 | +// for (let j = i + 1; j < nums.length; j++) { |
| 52 | +// const twoSum = nums[i] + nums[j]; |
| 53 | +// if (seen.has(-twoSum)) { |
| 54 | +// const triplet = [nums[i], nums[j], -twoSum].sort((a, b) => a - b); // O(log 3) = O(1) |
| 55 | +// const key = triplet.join(","); |
| 56 | +// if (!triplets.has(key)) { |
| 57 | +// triplets.add(key); |
| 58 | +// result.push(triplet); |
| 59 | +// } |
| 60 | +// } |
| 61 | +// seen.add(nums[j]); |
| 62 | +// } |
| 63 | +// } |
| 64 | +// return result; |
| 65 | +// } |
| 66 | + |
| 67 | + |
| 68 | +// Approach 1 |
| 69 | +// 🗓️ 2025-04-11 |
| 70 | +// ❌ Time Limit Exceeded! |
| 71 | +// ⏳ Time Complexity: O(n^3) |
| 72 | +// 💾 Space Complexity: O(n^2) |
| 73 | + |
| 74 | +// function threeSum(nums: number[]): number[][] { |
| 75 | + |
| 76 | +// let triplets = new Set<string>(); |
| 77 | +// let result: number[][] = []; |
| 78 | + |
| 79 | +// // const set = new Set(); |
| 80 | +// // set.add([1, 2, 3]); |
| 81 | +// // set.add([1, 2, 3]); |
| 82 | +// // console.log(set); // contains BOTH arrays |
| 83 | +// // Set(2) { [ 1, 2, 3 ], [ 1, 2, 3 ] } |
| 84 | + |
| 85 | +// for (let i = 0; i < nums.length - 2; i++) { |
| 86 | +// for (let j = i + 1; j < nums.length - 1; j++) { |
| 87 | +// for (let k = j + 1; k < nums.length; k++) { |
| 88 | +// if (nums[i] + nums[j] + nums[k] === 0) { |
| 89 | +// const triplet = [nums[i], nums[j], nums[k]].sort((a, b) => a - b); |
| 90 | +// const key = triplet.join(","); |
| 91 | +// if (!triplets.has(key)) { |
| 92 | +// triplets.add(key); |
| 93 | +// result.push(triplet) |
| 94 | +// } |
| 95 | +// } |
| 96 | +// } |
| 97 | +// } |
| 98 | +// } |
| 99 | + |
| 100 | +// return result; |
| 101 | +// }; |
0 commit comments