Solving the TwoSum Algorithm Like A G
Tags: codequality, codenewbie, javascript
Published: 2020-02-24 03:37:56

Alight so I realized that a lot of these blogs that be "teaching" about algorithms really don't actually be teaching shit. I'm finna start teaching you guys how to do these shits properly and actually making sure y'all learn something. 

Let set it off now! So what we working on today is a problem seen on LeetCode that's not too hard. We just gunna be getting our feet wet on some algos. 

//Two Sum:
//Given an array of integers, return indices of the two numbers such that //they add up to a specific target.

//You may assume that each input would have exactly one solution, and you //may not use the same element twice.

//Example:

//Given nums = [2, 7, 11, 15], target = 9,

//Because nums[0] + nums[1] = 2 + 7 = 9,
//return [0, 1].

So there's a couple of ways to solve this jawn. Imma hit y'all with one of the more common scenarios and then break down a better way. 

Bet, so we got a target number and we have to find two numbers in the array that add up to our target. 

Basically, we gotta find the difference of the target number and each number in the array and then look to see if that difference is in the array. 

// function twoSums(array, target){
//   let result = [];
//   for(let i =0; i<array.length; i++){
//     let diff = target - array[i];
//     for(let j =0; j<array.length; j++){
//       if(array[j] === diff){
//       result.push(array.indexOf(array[j]));
//     }
//   }
// }
//    return result;

So what we doing here is we making an empty array called result to catch our values, word to Odell.

Then we gotta loop through the array and subtract every value from the target so we do that after our first for loop with the variable diff. 

Next, we gotta loop through the array again and find if the variable diff is in the array and if it is we push that index into our result. 

So that wad the first way I showed y'all how to solve this problem and this shit takes too much time. We're adding space by creating an array and we got a for loop within a for loop. That shit kinda wild.

In this next example we're gunna store our values in objects and cut our run time because arrays make that shit take longer. So let's fuckin work!!

// function twoSum(array, target){
//   let numbsObject = {};
//   for(let i = 0; i<array.length; i++){
//     let currentValue = array[i];
//     numbsObject[currentValue] = i;
//   }
//   for(let i =0; i<array.length; i++){
//     let diff = target - array[i];
//     if(numbsObject.hasOwnProperty(diff) && diff !== i){
//       return [i, numbsObject[diff]]
//     }
//   }
// }

Ight bet, so we got an empty object and we're going to loop through the array and set every index to be the key value of the object. So that's essentially what's happening in our first loop. We loop through our array and then we put every value in the array into our object.

Our numbsObject[currentValue] = i; assigns the given array value to be the key value of the hash.

Next we loop through our array again and we do the same thing we did in our first solution and find the difference between the target and every array value. Then we look for that value in our numbObject with the object method hasOwnProperty() which looks for a specified value in an object. 

When we find that shit we then return it in an array. 

That's how the fuck you do that shit like a real a fuckin real OG. Hopefully y'all learned some shit and I hope shit was clear for y'all if y'all need anything else explain LMK, Imma hold y'all the fuck down.