This is super helpful as a bootcamp student. I also appreciate seeing the errors happen.
@ThinkFWD4 жыл бұрын
Thank you ! Hope this helps you out. As i was in the same shoes as you a few years ago ! keep it up
@humancyyborg4 жыл бұрын
you are amazing, the best leetcode walkthrough so far... so natural, not just pounding straight into problems
@ThinkFWD4 жыл бұрын
Thank you so much ! I will continue to make more. I was away and taking a break . but now i'm back :)
@sreekumarmenon4 жыл бұрын
if the array is sorted, we can use 2 pointers approach as well - function twoSum(nums, target){ let left =0; let right = nums.length-1; while(left < right){ const sum = nums[left] + nums[right]; if(sum == target){ return [left, right]; } else if(sum > target){ right--; } else { left++; } } return [-1,-1]; }
@Onomandah4 жыл бұрын
Another solution, perhaps a much easier one, would be to have 2 pointers on either end of the array. But this solution was new for me. Thank you.
@daveisdead3 жыл бұрын
Wouldn’t that be assuming the array is sorted?
@sreerags5818 Жыл бұрын
Only work for sorted array
@altayezekariyas92432 жыл бұрын
const twoSum = (nums, target) => { for(i of nums ){ a = target - i if (a in nums){ return [nums.indexOf(i), nums.indexOf(a)] } } };
@captainalpha4853 Жыл бұрын
I think the complexity of this algorithm is O(n^2). Since "in" operator has a loop implementation internally
@Amar111152 жыл бұрын
Thanks for the video! I am a beginner, Learned a lot for you.
@narayanareddy152 жыл бұрын
Thanks for the detailed explanation. Please continue this series 🔥
@SuperKojota4 жыл бұрын
Hi.Why you dont use classic if-else in this challenge? I am used to work with if(statement ) then { return something} then else{ return something}.
@hongkaihuang1503 Жыл бұрын
best explanation... You are a monster.
@pratiik374 жыл бұрын
You're doing a great job🔥🤗
@Simple_Otaku3 ай бұрын
설명이 깔끔합니다. 감사합니다😊
@islamibrahim81212 жыл бұрын
This was really helpful. (My attempt at interpreting the solution provided) Halfway through your introduction of the solution I thought you were going to do a first loop where each difference is stored in the map and then a second one where we check each element against the map. This is an interesting optimisation of this that uses the property that if two numbers add to a third, then removing one number will yield the other (which, based on the question, should be in the list. And no repetition so just one pass was sufficient). Good stuff!
@ДениИмагожев113 жыл бұрын
That's perfect, very detailed and helpful👍🏻👍🏻👍🏻 thanks
@TheSaltyCook2 жыл бұрын
this was really great. thank you for walking through each step of the logic.
@javascript_developer2 жыл бұрын
I find this same approach on other videos too. but thanks your explaination did helped me understand.
@PavanKumar-mr9td3 жыл бұрын
Wonderful explanation i loved it. KEEP DOING..
@beluga8230 Жыл бұрын
another way if you dont want like this method :- const nums = [1,2,3,4,5,6,7,8,9]; let target = 5; var countElements = function(nums ,target) { for(let i=0;i
@gabrieltavares82142 ай бұрын
That’s the “brute force” he said would not be the ideal solution
@DA_Karas4 жыл бұрын
I love your videos, thank you!
@TheILOVEYOUXD2 жыл бұрын
Thank you for content and understanding;)
@nehaprakash30733 жыл бұрын
This is Great!! Subscribed.. Thanks to you for this!
@keerthiseelan54574 жыл бұрын
Keep on going
@Canaano_011 ай бұрын
Nice video!!!
@tevinthuku5 жыл бұрын
This was neatly done
@ThinkFWD5 жыл бұрын
Tevin Thuku thanks!
@minhan21984 жыл бұрын
Hey ThinkFWD, which do you think is a better programming language is better for Algo: Java or Javascript?
@ThinkFWD4 жыл бұрын
Min Han whatever language your most comfortable with is the primary choice, but if you want less amount of code I would go python. But JavaScript works for me.
@jessesavoey1349 Жыл бұрын
For the ES6 syntax of the for of Loop where is the .entries coming from? Is this a new method from ES6?
@geek_24 Жыл бұрын
Hi if anyone having the same doubt the .entries is basically creating a new Array Iterator object that contains the key/value pairs for each index and its corresponding value in the array, because for of loops iterates over iterable objects, such as arrays, strings, or collections. Now nums is already an array right? so why do we need to explicitly write .entries()? So the answer is When using destructuring in the loop declaration, you need to enclose nums in the entries() method to iterate over the index-value pairs correctly. I hope it will help someone
@PeterPengUW5 жыл бұрын
Thx for the easy follow Along!
@ThinkFWD5 жыл бұрын
thank !
@jamesmeegan20662 жыл бұрын
Learned a lot, thanks.
@Ozoocats Жыл бұрын
I can't thank you enough
@JustThink20003 жыл бұрын
Definitely not the most efficient solution but this was nice to see as an addnl way to solve it.
@kiadyravel9753 жыл бұрын
Can you tel me your solution plz, i'm very curious, because i can't find another one
@whizzkee2 жыл бұрын
I wish this made sense
@gearsighted3 жыл бұрын
Great stuff, I'm still a little confused but significantly less so! Subscribed! :D
@ThinkFWD3 жыл бұрын
Awesome, check out my more recent vids, those should be slightly better :)
@MrWardo20092 жыл бұрын
Thank you for doing this!
@dylandupasquier2 жыл бұрын
huh, I've never seen .entries() used on an array. Is that the only way to enable the use of 2 declared indexes in the for loop?
@villacarlos4 жыл бұрын
Good stuff!
@pinkhairblackman81413 жыл бұрын
const twoSum=(nums, target) => { let storage = {}; for (let i = 0; i < nums.length; i++) { let num= nums[i]; if(storage[num] !== undefined) return([storage[num],i]) ; storage[target-nums[i]]=i; }
@justadev____72324 жыл бұрын
What does the time complexity equal on this one?
@ThinkFWD4 жыл бұрын
O(N) time with space of O(N) where N is the size of the input array.
@yashu64042 жыл бұрын
thank you so much sir
@angeloliwanag26194 жыл бұрын
This video is GOATED
@anastasiyat9116 Жыл бұрын
thank you!!❤
@edetmmekut8093 жыл бұрын
men u are the bomb
@simamorpho26412 жыл бұрын
Simpler to understand working answer: nums = [9, 0, 7, 2]; target = 9; function twoSum(array, target) { answer = []; for (let i = 0; i < array.length; i++) { for (let j = i + 1; j < array.length; j++) { let sum = array[i] + array[j]; if (sum === target) { if (array[i] === array[j]) continue; else { answer.push(i); answer.push(j); return answer; } } } } } console.log(twoSum(nums, target));
@rizwanshaik21162 жыл бұрын
It is a brute force approach ..to save time and space complexity maintain key, value pairs as mentioned in video
@simamorpho26412 жыл бұрын
@@rizwanshaik2116 Hi! What do you mean by "brute force" in this context?
@lamnguyenduc62162 жыл бұрын
@@simamorpho2641 running time is O(n2)
@ogbillity11 ай бұрын
Using a nested loop is not an efficient solution.
@okonkwo.ify182 жыл бұрын
Try to use if num in storage. It’s better
@rawstrife97623 жыл бұрын
Hi great video! I have a quick question, your object has the number as a string but you are checking object[7] instead of object["7"]. Do they just parse the same way? What about object.7?
@virajjoshi75693 жыл бұрын
In example he has used single quotes for object key but it will store in number data type.
@azikkii Жыл бұрын
You can’t do dot notation on variables or numbers. When you put 7 in the bracket notation using the variable it still looks for the key 7. In his example I don’t think JS would have automatically put quotes around the key like that. It would have just been the number 7 as a key. I’d have to check though.
@davidc32684 жыл бұрын
is there a benefit of adding the result of the current number - target as the object key, and the index of the current number as the value as opposed to adding the current number as the key and its index as the value? seems confusing this way.
@haripriyaramanathan38174 жыл бұрын
Nice video!! Thank you!! Do you have any recommendations on what resources to use to learn JS in terms of interview prep? I learned JS from W3Schools.
@Ali-mc4le4 жыл бұрын
learn Algorithms and Data Structures: www.udemy.com/course/coding-interview-crash-course-for-web-developer-2020/?couponCode=ADA6B72288D49A1CAE25
@Ali_Egamberdiyev2 жыл бұрын
Thank You Bro 😇
@okidokiyowyow3562 жыл бұрын
freaking awesome
@mohitsaud20713 жыл бұрын
Nice explanation and as a result, Subscribed to the channel.
@akarshs95524 жыл бұрын
Hey, are these algorithms enough to crack the technical interview?
@ThinkFWD4 жыл бұрын
Akarsh did you watch my ace the interview vid? These questions will help with major faang and other tech companies. However some may smaller companies may ask subject specific or system design type questions. But these algo will give you some foundation
@akarshs95524 жыл бұрын
@@ThinkFWD No I haven't watched I will watch now actually my problem is I am building mobile apps in React Native since a year but week at Data structure and algorithms. so is it really important to learn data structure for good development?
@ThinkFWD4 жыл бұрын
Akarsh s when you start building things that rely less on libraries and require more foundational ways to solve things, I would say yes. Especially if your apps deal with managing large sets of data efficiently. It is probably used more in the back end micro services as well. If you want to get into most tech companies in the valley or nyc, knowing you data structures and algo is super important. When scaling an app is part of your critical path. Anyone can manually sort a deck a cards, but what if you had to sort 1M deck of cards... that same process applied to 1 won’t fly. :) hope it helps!
@akarshs95524 жыл бұрын
@@ThinkFWD Thanks, I am good at basics but I find difficult to crack complicated problems
@medalikhaled3 жыл бұрын
thank you
@atoztravelgyan7247 Жыл бұрын
It says “EASY” on the top and you made it look like an enterprise project😂🤣
@ismaelfernandez63595 жыл бұрын
how can I change the language to Javascript on leetcode?
@ThinkFWD5 жыл бұрын
Emoneyonthebeat top left corner of where you can start to code, there is a drop down where you can select you me desired language
@cvxcfv3 жыл бұрын
could be explained better but as you said this is a raw footage
@ThinkFWD3 жыл бұрын
I do have a more refined one as I’m kicking off a new series called 75 questions, I believe everyone should do
@elvispastiche2 ай бұрын
you are my OPRAH
@jadedvi10443 жыл бұрын
Hello guys, I can not get the systax that he uses since part If(storage[num] ...... ). Can someone suggest me where to study this new JS version ?
@wolfdaddy0853 жыл бұрын
what is required to know for solving this?? I can't solve it by myself
@haoz75292 жыл бұрын
I got "nums.entries is not a function. (In 'nums.entries()', 'nums.entries' is undefined)
@HawkingMerchant3 жыл бұрын
I though by the name that I had to sum two numbers but problem is totally different
@emilyreese95512 жыл бұрын
What kind of for loop is this?
@geek_24 Жыл бұрын
if anyone having the same doubt this is called for of loop in javascript for of loops iterates over iterable objects, such as arrays, strings, or collections. Here is the simplest example of this loop -> const arr = ['apple', 'banana', 'cherry']; for (const fruit of arr) { console.log(fruit); } Now if I want to print the index numbers too of the associated entries like apple - 0, banana -1 etc the code will be like this -> const arr = ['apple', 'banana', 'cherry']; for (const [index, fruit] of arr.entries()) { console.log(`Index: ${index}, Fruit: ${fruit}`); } we are destructuring index and entries of array and while destructuring we have to use the .entries() to get the proper array iterable that holds the key and value. Hope that helps!
@mrSargi72 жыл бұрын
I would've definetly used two loops here. That was a clever solution, shows why knowing algorithms is so important
@imyounick2 жыл бұрын
twoSumd([3, 3, 4, 2], 6 ) - Fails in this case
@billybrains3 жыл бұрын
Is it bad that i couldnt even do this question on my own…
@ThinkFWD3 жыл бұрын
No way! Just keep it up and built up the experience :) stay positive
@billybrains3 жыл бұрын
@@ThinkFWD thanks bro! I just finished a bootcamp. Was an underwater welder for 12 years, COVID made me rethink everything. 33 years old changing careers appreciate the vids, subscribed!
@opdelta48542 жыл бұрын
@@billybrains if you don’t mind me asking which bootcamp did you go to? Was it worth it? Also, have you landed a job yet? I am currently deciding to attend a bootcamp or not.
@ishumishra81044 жыл бұрын
hey i have used you code but it doesn't got executed,showing output Undefined !! /** * @param {number[]} nums * @param {number} target * @return {number[]} */ const twoSum=(nums,target)=>{ let storage={}; for(const [num,index] of nums.entries()){ if(storage[num]!==undefined) return [storage[num],index]; storage[target-num]=index; } };
@ThinkFWD4 жыл бұрын
You have the Num and index in reverse order. const [index, num] !== const [num, index].
@ishumishra81044 жыл бұрын
@@ThinkFWD copied from. Your code only, so code was wrong?
@ThinkFWD4 жыл бұрын
@@ishumishra8104 the code in the video is not wrong. Look at my video @13:12 ... look at line 9 and you will see what you pasted is incorrect.
@ishumishra81044 жыл бұрын
@@ThinkFWD I was saying my code was wrong not yours 😒
@JazzFanss3 жыл бұрын
@@ishumishra8104 er.. maybe you should come up with your own solution like using a different type of loop instead of ( i assume) copy paste
@moosegoose12823 жыл бұрын
:/ did u already did the problem
@ThinkFWD3 жыл бұрын
I did recently, but that is more part of a 75 question series I recommend people do.
@moosegoose12823 жыл бұрын
@@ThinkFWD :( I prefer a fresh take so I can see thinking process
@ThinkFWD3 жыл бұрын
@@moosegoose1282 I redid it on the new vid :), but new questions are coming this week :). I’m doing 1 vid a week so stay tuned
@infyro2 жыл бұрын
Amazing explanation, I really appreciated this! I just have one question though that i'd like some clarification on. I'm not entirely clear how the algorithm knows to return the index once it finds the storage[num]. Any further explanation would be greatly appreciated thanks!
@dylandupasquier2 жыл бұрын
I'm also curious about that. I've never seen this syntax of using an array within your for loop. Super cool.
@stukagamerxtreme3 жыл бұрын
@etymology_4 жыл бұрын
Hi Everyone, here is an actual good answer, although leet code wants the indexes, this returns the actual values/the better way of doing it: const twoSum = (array, target) => { const numbers = {}; for(num of array){ const potentialMatch = target - num; if(potentialMatch in numbers){ return [potentialMatch, num] }else{ numbers[num] = true; } } return []; };
@vivarantx2 жыл бұрын
it's the same he did but you are actually making it less efficient by using that in
@christinele58323 жыл бұрын
are you Vietnamese ?
@abhishekpatnaik1449 күн бұрын
// arrayinput [2,7,11,15,3] target 18 function getIndicesToAchieveTarget(inputarray,target){ var finalarray=[]; for(let i=0;i
@uzairrajpoot52192 жыл бұрын
//Two number of sum let twoSum = (num1, num2) => { return num1 + num2; } twoSum(5, 5);