Tried to coast on the wind of TwoSumII, but it didn’t quite work:
Haven’t figured out what’s going wrong here yet. I DID notice I wasn’t really checking if i, j and k were all different values, which the definition asks for. Here I am attempting to fix this:
But this still doesn’t work. I’m going to try from the top - I feel that I should be able to get this to work.
I experimented a bit with console.logging the crap out of it:
And this helped somehow. What I discovered was that I WAS actually getting all the right sets, it’s just that I was getting duplicate sets. So I’m gonna make sure that’s not happening anymore.
YES!!! I DID IT!!!
Claude didn’t think it was possible! Or at least never really suggested it the way I was doing it, no matter how I prodded her/him/it/our sacred AI lord and savior who art in heaven.
It did fail this one test case out of 313 test cases:
Hmm. Wonder why that is. Yeah Claude really doesn’t like that I’m using this map, but I feel like a map makes so much sense here and is actually really cool.
But supposedly it’s not very efficient. Here’s a leetcode solution someone provided:
function threeSum(nums: number[]): number[][] {
nums = nums.sort((a, b) => a - b)
const ans: number[][] = []
for (let left = 0; left < nums.length - 2; left++) {
if (nums[left] > 0) break
if (left > 0 && nums[left] === nums[left - 1]) continue
let middle = left + 1
let right = nums.length - 1
while (middle < right) {
const sum = nums[left] + nums[middle] + nums[right]
if (sum === 0) {
ans.push([nums[left], nums[middle], nums[right]])
while (middle < right && nums[middle] === nums[middle + 1]) middle++
while (middle < right && nums[right] === nums[right - 1]) right--
middle++
right--
} else if (sum > 0) {
right--
} else {
middle++
}
}
}
return ans
}
So verbose! I don’t understand if (left > 0 && nums[left] === nums[left - 1]) continue because i is supposed to !=k, etc., as in the indexes providing values for each triplet are supposed to be unique, yet this seems to enforce uniqueness of the values which I’m 95% sure is not the goal here. I have to get some rest though, i’ll return to this.