DEV Community

DSAwithSM
DSAwithSM

Posted on

5 LeetCode Problems #215, #455, #347, #226, #104 Explained | DSA Decoded

🧠 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:

  • 🟡 Kth Largest Element in an Array (LeetCode #215) · Medium · Heap
  • 🟢 Assign Cookies (LeetCode #455) · Easy · Greedy
  • 🟡 Top K Frequent Elements (LeetCode #347) · Medium · Heap
  • 🟢 Invert Binary Tree (LeetCode #226) · Easy · Trees
  • 🟢 Maximum Depth of Binary Tree (LeetCode #104) · Easy · Trees

🟡 Kth Largest Element in an Array

Difficulty: Medium  |  Topic: Heap  |  LeetCode #215

Problem

Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Can you solve it without sorting?

Solution

import heapq

def find_kth_largest(nums, k):
    min_heap = []
    for num in nums:
        heapq.heappush(min_heap, num)
        if len(min_heap) > k:
            heapq.heappop(min_heap)
    return min_heap[0]
Enter fullscreen mode Exit fullscreen mode

🔗 Solve on LeetCode  ·  💻 Full solution + tests on GitHub


🟢 Assign Cookies

Difficulty: Easy  |  Topic: Greedy  |  LeetCode #455

Problem

Assume you are an awesome parent and want to give your children some cookies. Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with. Each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

Solution

def find_content_children(g, s):
    g.sort()
    s.sort()
    child = cookie = 0
    while child < len(g) and cookie < len(s):
        if s[cookie] >= g[child]:
            child += 1
        cookie += 1
    return child
Enter fullscreen mode Exit fullscreen mode

🔗 Solve on LeetCode  ·  💻 Full solution + tests on GitHub


🟡 Top K Frequent Elements

Difficulty: Medium  |  Topic: Heap  |  LeetCode #347

Problem

Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

Solution

import heapq
from collections import Counter

def top_k_frequent(nums, k):
    count = Counter(nums)
    return heapq.nlargest(k, count.keys(), key=count.get)
Enter fullscreen mode Exit fullscreen mode

🔗 Solve on LeetCode  ·  💻 Full solution + tests on GitHub


🟢 Invert Binary Tree

Difficulty: Easy  |  Topic: Trees  |  LeetCode #226

Problem

Given the root of a binary tree, invert the tree, and return its root. Inverting means swapping the left and right children of every node in the tree.

Solution

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def invert_tree(root):
    if not root:
        return None
    root.left, root.right = invert_tree(root.right), invert_tree(root.left)
    return root
Enter fullscreen mode Exit fullscreen mode

🔗 Solve on LeetCode  ·  📺 Watch video explanation  ·  💻 Full solution + tests on GitHub


🟢 Maximum Depth of Binary Tree

Difficulty: Easy  |  Topic: Trees  |  LeetCode #104

Problem

Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Solution

def max_depth(root):
    if not root:
        return 0
    return 1 + max(max_depth(root.left), max_depth(root.right))
Enter fullscreen mode Exit fullscreen mode

🔗 Solve on LeetCode  ·  📺 Watch video explanation  ·  💻 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)