Posts

Showing posts with the label recursion

Path Sum

 Path Sum You are given the root of a binary tree and an integer targetSum . Your task is to determine whether the tree has any root-to-leaf path such that adding up all the values along the path equals targetSum . A leaf is a node with no children. Example Input : root = [ 5 , 4 , 8 , 11 , null , 13 , 4 , 7 , 2 , null , null , null , 1 ] targetSum = 22 Output : true This is because the path 5 → 4 → 11 → 2 adds up to 22. Best Data Structure for the Job Binary trees are already built using the classic TreeNode class. We just need: A recursive function to traverse the tree, tracking the remaining target sum. Different Approaches Approach 1: Recursive (Depth-First Search) We use recursion to walk through every path from root to leaf. At each node, we subtract the node’s value from the remaining targetSum , and pass it down the recursive call. When we reach a leaf, we check if the remaining sum equals the leaf’s value. /** * Definition for a binary tree node. * pub...

Modifies String Transformation

Modified String Transformation  Problem Statement You are given a string s of lowercase English letters and an integer t representing the number of transformation steps. Each transformation proceeds as follows: If a character is 'z' , it is transformed into "ab" (two characters). Any other character ch is replaced with the next character in the English alphabet (e.g., 'a' becomes 'b' , 'b' becomes 'c' , and so on). Your task is to calculate the length of the string after t transformations , modulo 10⁹ + 7 . Example Input: s = "abcyy" t = 2 Output: 7 Explanation: After 1st transformation: "a" → "b" , "b" → "c" , "c" → "d" , "y" → "z" , "y" → "z" → "bcdzz" After 2nd transformation: "b" → "c" , "c" → "d" , "d" → "e" , "z...

Tower of Hanoi Modified

Tower of Hanoi Modified – When the First Rod is a No-Go Problem Statement You’re given a classic Tower of Hanoi setup with three rods (A, B, and C) and N disks of different sizes stacked on Rod A , smallest on top. BUT there’s a twist... You cannot move a disk directly between rod A and rod C. You must always pass through rod B when going from A ➡️ C or C ➡️ A.  Goal: Move all disks from Rod A to Rod C , following the usual rules plus this new restriction, and print: The minimum number of moves required. The actual sequence of moves (formatted like Move x from A to B ). Example Input: 2 1 2 Output: 2 Move 1 from A to B Move 1 from B to C 8 Move 1 from A to B Move 1 from B to C Move 2 from A to B Move 1 from C to B Move 1 from B to A Move 2 from B to C Move 1 from A to B Move 1 from B to C Best Data Structure for the Job No need for trees, heaps, or graphs here. Just: Simple recursion  Strings to log ea...

Special Grid Quest: Rule of Four

Special Grid Generator – Divide and Rule in Action Problem Statement You're given a non-negative integer N , and you need to generate a 2^N × 2^N grid filled with integers from 0 to 4^N - 1 . The twist? The grid has to be special , satisfying all the following rules: All numbers in the top-right quadrant must be smaller than those in the bottom-right quadrant. All numbers in the bottom-right quadrant must be smaller than those in the bottom-left quadrant. All numbers in the bottom-left quadrant must be smaller than those in the top-left quadrant. Each of the four quadrants must recursively also be a special grid. Note: A 1x1 grid (when N = 0) is trivially special. Example Let’s take N = 1 (so grid size = 2x2) Output: [[3, 0], [2, 1]] Explanation: Top-right quadrant: 0 Bottom-right quadrant: 1 Bottom-left quadrant: 2 Top-left quadrant: 3 Ordering: 0 < 1 < 2 < 3 — perfect. Each quadrant is 1x1, which is trivially special. Best Data Structure for ...

Exponentiation Exploration

Pow(x, n) – Unlocking the Power of Exponentiation At first glance, calculating x raised to the power of n might seem like a simple task, but it’s a perfect opportunity to explore powerful techniques like brute force, fast exponentiation (divide and conquer), and iterative approaches. Let’s dive into the different ways we can efficiently solve this problem while making sure we understand both the logic and the efficiency behind each method. The Best Data Structure for Solving It (And Why!) When solving problems involving exponentiation, two things matter the most: Efficient calculation of powers. Handling negative exponents. In the case of x^n , we are tasked with computing the power of x raised to an integer n . This may seem straightforward, but there are various approaches to optimize the process. Brute Force helps us understand the problem at its core. Fast Exponentiation (or divide and conquer) offers a more efficient approach, leveraging binary representatio...

Addition, But Make It Linked 🔗➕

Adding Two Numbers – One Node at a Time We’ve all done basic addition, right? But what happens when each digit is stored in a linked list and… in reverse order? Welcome to a fan-favorite problem: Add Two Numbers (LeetCode #2). Let’s dive into the different ways to tackle it in Java – from the tried-and-true iterative method to a cheeky BigInteger hack. The best data structure for solving it (and why!) The obvious MVP here? Singly Linked Lists . Since the digits are stored in reverse order (1’s place first), we can iterate from the head without reversing anything manually. We just need to simulate digit-by-digit addition — like we did in school, carry and all. And for tracking the result? A dummy head node makes things a breeze. It helps keep the logic simple while chaining together the resulting nodes. Different approaches – brute force to optimized solutions 1: Iterative (Most Common) class ListNode { int val; ListNode next; ListNode( int x) { val = x; } } p...