DEV Community

Somuya Khandelwal
Somuya Khandelwal

Posted on

DAY 22 Backtracking: Exploring Every Possibility

Hey Everyone!

Today was all about Backtracking—a technique that feels like a mix of chess strategy and solving a treasure hunt. Backtracking problems are fascinating because they involve trying out all possible paths, and then undoing those steps (like retracing your steps in a maze) to find the optimal solution. It’s challenging, yet incredibly satisfying when the pieces fit perfectly.


How the Day Played Out

  1. Combination Sum (Medium Difficulty)

    • Imagine you have an unlimited supply of coins, and you’re trying to create exact amounts. The goal here was to find all combinations of numbers that add up to a target.
    • The Strategy:
      • Used recursion to explore all possible combinations of numbers.
      • Pruned paths that exceeded the target to save time.
    • The Fun Part:
      • The tree-like structure of recursive calls made the solution feel visual and methodical. Watching the algorithm backtrack and try another path was a real "aha moment."
  2. Permutations (Medium Difficulty)

    • This one was about arranging a set of numbers in all possible ways—a classic problem in combinatorics.
    • The Strategy:
      • Used backtracking to swap elements and generate all permutations.
      • Kept a list of used elements to avoid duplicates.
    • The Fun Part:
      • The recursive depth made the problem feel like an unfolding story, where each step brought a new twist.
  3. Word Search (Medium Difficulty)

    • Find if a word exists in a grid by traversing adjacent cells without revisiting them.
    • The Strategy:
      • Backtracked through the grid, marking cells as visited during each path.
      • Explored all directions (up, down, left, right) recursively.
    • The Fun Part:
      • The thrill of traversing the grid, marking cells, and undoing those marks felt like navigating a treasure map.

What Made Today Stand Out

  1. The Recursive Journey:

    • Backtracking problems aren’t just about finding solutions—they’re about exploring all possibilities systematically, like solving a giant puzzle.
  2. The Beauty of Undoing Steps:

    • Backtracking emphasizes the importance of undoing changes. The "reset" step is as critical as the exploration itself.
  3. Visually Satisfying:

    • Watching paths form, backtrack, and reform in Word Search made the problem-solving process feel like solving a real-world maze.

Key Takeaways

  • Pruning Saves Time:

    • Skipping unnecessary paths early (e.g., when the target is exceeded in Combination Sum) is crucial for efficiency.
  • Track Your Steps Carefully:

    • Whether it’s marking grid cells or keeping track of used elements, precision in state management is essential.
  • Backtracking is About Exploration:

    • The technique emphasizes trying all possibilities while knowing when to step back—it’s as much about discovery as optimization.

Reflections

The Word Search problem stood out for its mix of exploration and constraints. It reminded me how important it is to balance creativity with structure. On the other hand, Permutations felt like solving a dynamic puzzle, with pieces constantly shifting to form new patterns.


What’s Next?

Tomorrow, I’ll shift gears to Dynamic Programming Problems, including Climbing Stairs, Longest Increasing Subsequence, and Partition Equal Subset Sum. These problems will test my ability to break problems into overlapping subproblems and solve them systematically.

Thanks for being part of my journey! Let’s keep unraveling the mysteries of competitive programming together.

Top comments (0)