Two Sum Problem (Python) - HOW TO NAIL LeetCode Interview Questions

  Рет қаралды 32,507

Kite

Kite

Күн бұрын

Пікірлер: 38
@ashleyhaa
@ashleyhaa 4 жыл бұрын
These videos are great! I suggest talking a bit slower though so less advanced coders like myself have an easier time following :)
@MsNennifer
@MsNennifer 3 жыл бұрын
You could always play the video at a slower speed.
@NN-uq6de
@NN-uq6de 2 жыл бұрын
@@MsNennifer Or he can go a little slower and explain things more thoroughly on each topic
@Code0866
@Code0866 2 жыл бұрын
@@NN-uq6de This is for intermediate-expert level programmers. You need to study more if you cant follow.
@rebeccatom2188
@rebeccatom2188 2 жыл бұрын
@@Code0866 ok nerd
@satyamagrawal6854
@satyamagrawal6854 2 жыл бұрын
your runtime will decrease significantly if you use for i in range(0,len(nums)): rather than enumerate
@cvxcfv
@cvxcfv 3 жыл бұрын
Probably the best explanation out of the 4 videos I've invested watching
@1pomii
@1pomii 3 жыл бұрын
so what was the point of this? to show everyone you can talk fast?
@njokuiyke01
@njokuiyke01 4 жыл бұрын
Hi, at what point did the elements get added to the dictionary
@justinnguyen8049
@justinnguyen8049 3 жыл бұрын
it was at line 18, values[elem] = i, that the element got put into the dictionary.
@RegiiPad
@RegiiPad 4 жыл бұрын
You can have more than one pair for the same target. The enhanced version of your code follows: 1 #!/usr/bin/python 2 3 from collections import namedtuple 4 import os 5 import sys 6 7 Pair = namedtuple('Pair', 'x1, x2, v1, v2') 8 9 def two_sum(list, target): 10 values = dict() 11 pairs = [] 12 for i, elem in enumerate(list): 13 comp = target - elem 14 if comp in values: 15 pairs.append(Pair(values[comp], i, list[values[comp]], list[i])) 16 pass 17 values[elem] = i 18 pass 19 return pairs 20 pass 21 22 def main(): 23 arr = [1, 2, 3, 4, 5, 6, 7] 24 tgt = 8 25 results = two_sum(arr, tgt) 26 27 for p in results: 28 print("Numbers are [{}]={} and [{}]={} to add {}".format( 29 p.x1, p.v1, p.x2, p.v2, tgt)) 30 pass 31 pass 32 33 if __name__ == '__main__': 34 main() 35 print("** END **") 36 pass 37 pass
@KiteHQ
@KiteHQ 4 жыл бұрын
! you should join our fb group and help out others looking to learn Python facebook.com/groups/505658083720291
@RegiiPad
@RegiiPad 4 жыл бұрын
Kite applied today
@codewithchandan6146
@codewithchandan6146 3 жыл бұрын
Hy can you solve my code please
@KiteHQ
@KiteHQ 4 жыл бұрын
If you liked this video, join the Kite Developer Community on Facebook for access to more resources + support from fellow Python developers. Time to level up! facebook.com/groups/505658083720291
@damanvirdi168
@damanvirdi168 2 жыл бұрын
nice and crisp explanation
@Samantha-no4es
@Samantha-no4es 4 жыл бұрын
Can someone explain line 14? When did anything get added to the dictionary? To me, it seems to be empty so I don't yet understand why we're checking if the compliment is there :o
@SouthernHadoken
@SouthernHadoken 4 жыл бұрын
It got added on line 18. That the else in if/else.
@universal4334
@universal4334 4 жыл бұрын
@@SouthernHadoken then all the elements added to dic . Finally returns empty list.
@faisalabdulkadir9439
@faisalabdulkadir9439 3 жыл бұрын
Why do we map the elements and indexes to the dict after our if statement??
@SayfSentinel
@SayfSentinel 4 жыл бұрын
Hi Your solution does not apply for an array like [3,2,4] and a target of 6 When you enter your if statement target minus 3 return 3 which is already in your array and stop the loop
@cristianmurrachavez5380
@cristianmurrachavez5380 4 жыл бұрын
No, because its searching in the dict not in the array and the number is only added after asking
@BrendanMetcalfe
@BrendanMetcalfe 4 жыл бұрын
Where is the code from the example?
@harrisonooi296
@harrisonooi296 3 жыл бұрын
@0:56
@frenchmike
@frenchmike 3 жыл бұрын
reading the answers at that pace with not detail or visuals it no better than handing me the code, no need for a video for that.
@aditiparashar9171
@aditiparashar9171 2 жыл бұрын
you are amazing
@BrendanMetcalfe
@BrendanMetcalfe 4 жыл бұрын
Thanks!
@TheJmization
@TheJmization 3 жыл бұрын
wtf this is on easy mode wtf
@seemeyeah1362
@seemeyeah1362 4 жыл бұрын
I was under the impression line 18 would include the elif statement, and an else statement for line 20. How comes this isn't the case?
@judahmcnicholl8334
@judahmcnicholl8334 3 жыл бұрын
I know its a bit late for an answer, but when returning out of a function nothing below it gets executed, so if that statement is hit you have found the solution.
@marufmazumder8294
@marufmazumder8294 3 жыл бұрын
values[elem] = i ; ====> why is that?
@killagarcia392
@killagarcia392 3 жыл бұрын
by doing so we map the key (element from our list) to the key value (index) {element:index,...}
@aakashjana6225
@aakashjana6225 4 жыл бұрын
I think you have over complicated the problem just run a nested loop where the second loop starts from one position after the current iterator value of the outer loop add the values stored in both the indexes and check if the equal your target that's it for i in range(0,len(arr)): for j in range(i, len(arr)): If are[i]+arr[j]==target: return i, j return False
@manastrivedi3841
@manastrivedi3841 4 жыл бұрын
It might seem like the solution you gave us optional but actually, it has a complexity of O(n^2)
@7minutesdead
@7minutesdead 4 жыл бұрын
Since this has a complexity of O(n^2), Leetcode won't accept it because your time limit will exceed on the later test cases that have very large input lists (I tried to do the same thing more or less). The solution in the video is much faster and won't time out.
@ras_mag
@ras_mag 3 жыл бұрын
This solution is not optimal you are scanning the whole list for the solution.
@scorpions7153
@scorpions7153 2 жыл бұрын
Why are you in a hurry?
Python Technical Interviews - 5 Things you MUST KNOW
9:07
Two Sum - Leetcode 1 - HashMap - Python
8:26
NeetCode
Рет қаралды 1,5 МЛН
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 193 М.
"Clean" Code, Horrible Performance
22:41
Molly Rocket
Рет қаралды 947 М.
Mock Technical Interview - Javascript Developer Entry Level
1:36:22
Tech with Nader
Рет қаралды 509 М.
How I Would Learn Python FAST (if I could start over)
12:19
3 Levels of Sorting Algorithms - FASTEST Comparison Sort!
10:38
How I Got Good at Coding Interviews
6:29
NeetCode
Рет қаралды 1,7 МЛН
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 353 М.
How to Remember Everything You Read
26:12
Justin Sung
Рет қаралды 3,3 МЛН
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 460 М.
Medium Google Coding Interview With Ben Awad
51:27
Clément Mihailescu
Рет қаралды 1,3 МЛН