🧠5 LeetCode Problems Explained with Code + Video
Part of the DSA Decoded series — step-by-step Python solutions with video walkthroughs.
Problems covered in this post:
- 🟢 Two Sum (LeetCode #1) · Easy · Arrays
- 🟢 Best Time to Buy and Sell Stock (LeetCode #121) · Easy · Arrays
- 🟢 Valid Parentheses (LeetCode #20) · Easy · Stack
- 🟡 Maximum Subarray (LeetCode #53) · Medium · Dynamic Programming
- 🟢 Climbing Stairs (LeetCode #70) · Easy · Dynamic Programming
🟢 Two Sum
Difficulty: Easy  | Topic: Arrays  | LeetCode #1
Problem
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice.
Solution
def two_sum(nums, target):
seen = {}
for i, n in enumerate(nums):
comp = target - n
if comp in seen:
return [seen[comp], i]
seen[n] = i
🔗 Solve on LeetCode  · 💻 Full solution + tests on GitHub
🟢 Best Time to Buy and Sell Stock
Difficulty: Easy  | Topic: Arrays  | LeetCode #121
Problem
You are given an array prices where prices[i] is the price of a given stock on the i-th day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Solution
def max_profit(prices):
min_price = float('inf')
max_profit = 0
for price in prices:
if price < min_price:
min_price = price
elif price - min_price > max_profit:
max_profit = price - min_price
return max_profit
🔗 Solve on LeetCode  · 💻 Full solution + tests on GitHub
🟢 Valid Parentheses
Difficulty: Easy  | Topic: Stack  | LeetCode #20
Problem
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: open brackets must be closed by the same type of brackets, and open brackets must be closed in the correct order.
Solution
def is_valid(s):
stack = []
mapping = {')': '(', '}': '{', ']': '['}
for char in s:
if char in mapping:
top = stack.pop() if stack else '#'
if mapping[char] != top:
return False
else:
stack.append(char)
return not stack
🔗 Solve on LeetCode  · 💻 Full solution + tests on GitHub
🟡 Maximum Subarray
Difficulty: Medium  | Topic: Dynamic Programming  | LeetCode #53
Problem
Given an integer array nums, find the subarray with the largest sum, and return its sum. A subarray is a contiguous non-empty sequence of elements within an array.
Solution
def max_subarray(nums):
max_sum = nums[0]
current_sum = nums[0]
for num in nums[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
🔗 Solve on LeetCode  · 💻 Full solution + tests on GitHub
🟢 Climbing Stairs
Difficulty: Easy  | Topic: Dynamic Programming  | LeetCode #70
Problem
You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Solution
def climb_stairs(n):
if n <= 2:
return n
a, b = 1, 2
for _ in range(3, n + 1):
a, b = b, a + b
return b
🔗 Solve on LeetCode  · 💻 Full solution + tests on GitHub
🔔 Follow Along
New problems every 2 days — subscribe to stay consistent with your DSA prep!
Drop a comment with your approach — always curious to see other solutions!
Top comments (0)