Python Programming Practice: LeetCode #1 -- Two Sum

preview_player
Показать описание
In this episode of Python Programming Practice, we tackle LeetCode #1 -- Two Sum.

Link to the problem here:

Python Programming Practice is a series focused on teaching practical coding skills by solving exercises on popular coding websites. Note that the solutions seen here may not be the most efficient possible.

I am not going to provide the full code in the video description for this series, since copy and pasting solutions is not in the spirit of doing coding exercises. It is intended that the video will help you think about problems, approaches and how to structure solutions so that you are able to code up a working solution yourself.

Рекомендации по теме
Комментарии
Автор

I've seen 100 vids on this two-sum problem and it just never clicked with me. I just dont think people could teach it in a way that I would understand. This short video of yours very clearly explained it and I finally understand now. This was a big brick wall for me so it feels amazing to finally get through it.

GamingNoodle
Автор

This is great content. Your explanations are clear and logical. Thanks

maxgriffiths
Автор

def twoSum(self, nums: List[int], target: int) -> List[int]:
numToIndex = {}
for i in range(len(nums)):
if target - nums[i] in numToIndex:
return [numToIndex[target - nums[i]], i]
numToIndex[nums[i]] = i
return []

NOBOBI
Автор

def twoSum(array, target):
for x in array:
if target - x in array and target - x != x: return [array.index(x), array.index(target - x)]

easy 3 liner

VihaanKembhavi-uy
Автор

def sum_index(arr, i):
if i >= len(arr) - 1:
return 0
now = arr[i]
next = arr[i+1]
sum = now+next
print(f"Sum {i+1}: {sum}")
return sum + sum_index(arr, i+1)
print(sum_index([2, 7, 11, 18], 0))

kvelez
Автор

Your explanations are so precise and easy to understand. Is it possible to make three sum explanation video?

Draco-puro
Автор

In the 2nd solution, how does Python find out whether the value is or is not in the dictionary without looping through the dictionary somehow?

jimmymesa
Автор

other videos in yt looks like they memorizing the code and faking like the they thinking and coding, but this one looks legit

joshikaran-zmju
Автор

Great video with clear explanation on every concept. Amazing!

marcofer
Автор

Using recursion:
def twoSum(j=0, i=1):
if (nums[j]+nums[i])==target:
return [j, i]
else:
return twoSum(j+1, i+1)

print(twoSum())

jaishreeram
Автор

What about traversing the list, subtracting the number at the current index from 9 and then searching the list for the complement?

liatris
Автор

This is an awesome walkthrough of coding !! Just a quick question.. Do we need the elif in 2nd solution ? Can't it be within for .

boy
Автор

Amazing explanation, I understood for the first time after watching many videos

yowanehakuuu
Автор

I don't understand the 2nd solution.
At first we have an empty dict.
On a first iteration we added "2: 0" to it.
On a second iteration we subtracted 7 from 9 (target - num) and got 2, that we already have in "seen" as key.
But what is going on in the line 8???
As I can see it is [2, 1] because 9-7 = 2 and i on 2nd iteration is 1.
But how are we getting [0, 1]?

Kerogas_
Автор

Should we just assume that for the first solution that the two values that sum to target will be consecutive?

Elunicozorro
Автор

def check_result(myList, target):

if len(myList) <= 1 :
return myList
else:

list_indx = []

for i in range(len(myList)-1):
if myList[i] + myList[i+1] > target \
or target != myList[i] + myList[i+1] :

continue
list_indx.extend([i, i+1])

return list_indx


myList = [1, 2, 3, 4, 5, 6]

target = 7
print(check_result(myList, target ))

ajmo
Автор

I added this code into my Pycharm.

nums = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 100]
target = 18

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
seen = {}
for i, num in enumerate(nums):
if target - num in seen:
return([seen[target - num], i])
elif num not in seen:
seen[num] = i

print(twoSum())

and I got error:
NameError: name 'List' is not defined

michasekua
Автор

Great explanation, i needed it thank you!

bvlmari
Автор

Thank you for sharing the videos. keep going and I'm purely subscribing to your channel just for leet code problems in python.

TheMouseJerry-dumd
Автор

The second solution was highly efficient

ThatWomanIsNotMeAndImNotWhite
welcome to shbcf.ru