Two Sum Brute Force Solution in Python (Leetcode 1)

preview_player
Показать описание
In this video tutorial, I break down the Two Sum problem, a popular question often seen in coding interviews, and guide you through a brute force approach to solve it using Python. Whether you're preparing for a software engineering interview or simply want to sharpen your coding skills, this comprehensive guide will help you understand the logic behind the problem and write efficient code.
Рекомендации по теме
Комментарии
Автор

fastest solution is loading all numbers into a map with the key being the number and the value being the index of that number in the list using a loop. Then run another loop (not nested) to iterate over all numbers again and this time take the current number and find the difference in the target and the current number. Take the difference and look it up in the map and if it exists, then the current idx of your loop and the returned idx from the map is your answer. Runs in 2n time.

aaronmarsh
Автор

Remove all numbers larger than the goal, then subtract each number from the goal. Now find all instances of the differences that match the original less than goal list

tainicon
Автор

Isn't the complexity of brute force approach O(n²)? It's quadratic non exponential

CortezPro
Автор

Think you know a better solution? Comment it here.

CanonicalCode
Автор

Time complexity will be 0(n²) and this isn't the good practice

TheHarveyi
join shbcf.ru