Grind 75 Question 1: Two Sum
The beginning of my Zero to Google journey, which I haven’t written about yet, but will…eventually.
Two posts in two days! What’s the occasion! How lucky you are, dear reader.
This won’t be much of a post, but yesterday I cracked open (figuratively) Grind75, and so I’m going to pretend-whiteboard my answers to each question here, as a way to measure my progress and hopefully keep myself accountable. So without further ado, here’s what I have so far for Two Sum:
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
let pointerOne = nums[0]
let pointerTwo = nums[nums.length - 1]
if (pointerOne + pointerTwo > target) {
pointerTwo = nums[pointerTwo.IndexOf - 1];
}
else if (pointerOne + pointerTwo < target) {
pointerOne = nums[pointerOne.IndexOf + 1];
}
else if (pointerOne + pointerTwo == target) {
return [pointerOne.IndexOf, pointerTwo.IndexOf]
}
return [pointerOne.IndexOf, pointerTwo.IndexOf];
}
Okay. So that’s my start. I know this won’t work, because I don’t quite know how to execute a Two Pointer function. In fact, Two Pointer is something I literally just learned about today while starting to learn about the 14 Leetcode Patterns.
I also don’t know if my code works in general, but this is whiteboarding, and it’s exactly what I’ll be expected to do in a Technical Phone Screen and (if, God willing, I make it past that) in the on-site interviews. So, it’s good practice. Let me upload this sucker to Leetcode and see what happens.
…
No bueno. But I’ve fixed it: this runs, but returns the wrong output.
var twoSum = function(nums, target) {
let pointerOne = nums[0];
let pointerTwo = nums[nums.length -1];
if (pointerOne + pointerTwo > target) {
pointerTwo = nums[nums.indexOf(pointerTwo) -1];
} else if (pointerOne + pointerTwo < target) {
pointerOne = nums[nums.indexOf(pointerOne) + 1];
} else if (pointerOne + pointerTwo == target) {
return [nums.indexOf(pointerOne), nums.indexOf(pointerTwo)]
}
return [nums.indexOf(pointerOne), nums.indexOf(pointerTwo)]
};
So now, let’s figure out how to do a loop. (Do while? Maybe?)
After some tinkering, I came up with this while loop (my first!):
var twoSum = function(nums, target) {
let pointerOne = nums[0];
let pointerTwo = nums[nums.length -1];
while (pointerOne + pointerTwo != target) {
if (pointerOne + pointerTwo > target) {
pointerTwo = nums[nums.indexOf(pointerTwo) -1];
} else if (pointerOne + pointerTwo < target) {
pointerOne = nums[nums.indexOf(pointerOne) + 1];
}
}
return [nums.indexOf(pointerOne), nums.indexOf(pointerTwo)]
}
And, miracle of miracles, it returns the proper answer! Or…it returned the proper answer until the nums input was [3, 3] and the target was 6. Then it returns [0, 0] because 3 is at position 0 in the array, and nums.indexOf(pointerTwo) returns the index number of the first instance of 3 it happens upon. Which is 0.
Now is the point at which I, ultimate beginner n00b wannabe coder that I am, need to turn to outside help. How do I return the proper index of pointerTwo?
The Leetcode forums provide the answer: use a for loop, where i is the index of the number being visited. Duh! I think I’ve even done this type of thing before. Hopefully it’s a lesson learned. Now to code it up properly.
…
It took me an embarrassingly long time to do this, but y’all. I finally got it. Here’s my working, accepted solution:
var twoSum = function(nums, target) {
let previousNumbers = new Map();
for (let i = 0; i < nums.length; i++) {
if (previousNumbers.has((target - nums[i]))) {
return [previousNumbers.get(target - nums[i]), i];
}
else previousNumbers.set(nums[i], i);
}
};
It ain’t pretty. It wasn’t pretty. But I learned something, which, hey. Wouldn’t that be a nice feature for Future Master Coder No Longer N00b Em to revisit (and perhaps laugh her head off at, hi FMCNLN Em!!)? So here we go:
What I Learned
- I learned the value of the i-loop (I’m sure it has a much fancier name, but that’s what I’ll call it) versus the…syntax for loop? Which Catalyte preferred us using. But with the i-loop, I was able to keep track of the index I was at, and use i in place of the index for the output. Yay!
- I made my very first hashmap! At least, I think it’s a hashmap. Javascript just calls it a Map() but I believe it behaves the same: it’s a key-value pair, so I can call the key (the actual number in the array) and am returned the value (the index I need for the output).
- I also executed my first While loop. I mean, it turned out I didn’t need it but…future reference, right?
This one darn easy question took me a day and a half to work through. But I’ll get better — that’s exactly why I’m doing this, after all. Baby steps. Keep on truckin’. Onward to my second Grind75 question, because I’m behind at this point. Heh.
mins
read