143. Reorder List

Quite an intriguing problem. Initially, I thought this solution would require extra storage to store some ListNodes as the order is L0 -> Ln -> L1 -> L(n-1)… I played around with some examples and observed the following pattern: First element -> last element -> second element -> second last element … Initial Observations: Ex1: [] -> No reordering as the list is empty. Ex2: [1] -> No reordering as there is only one element....

4 min · 743 words · Apurva Khatri

22. Generate Parentheses

Input: n Each Output String Length: 2 * n Approach The initial approach is to recognize that at every position in the resulting string, there are two possible characters: '(' or ')'. However, both options come with constraints: Constraints: Number of Opening Brackets (ob): The maximum number of opening brackets in the result is n. An opening bracket can only be inserted if its count is less than n and the count of opening brackets is greater than or equal to the count of closing brackets....

2 min · 391 words · Apurva Khatri

279. Perfect Squares

A challenging problem indeed. Initially, I thought of using a Dynamic Programming (DP) approach and did not consider the need to take the minimum at any point. My initial thought was that the answer to reach n would simply be: Dp[n] = 1 + Dp[n - pow(floor(sqrt(n)), 2)] Base Cases: Dp[1] = 1 Dp[m] = 1 if m == pow(floor(sqrt(m)), 2) (i.e., m is a perfect square). However, this approach is greedy and fails in some cases....

2 min · 347 words · Apurva Khatri

3. Longest Substring Without Repeating Characters

The problem of finding the longest substring without repeating characters is a classic one in algorithm design. It is particularly useful for understanding the sliding window technique, which is highly efficient for solving such problems involving contiguous subarrays or substrings. Sliding Window Approach In this solution, we use two pointers, start and end, to maintain a sliding window that keeps track of the current substring under consideration. The idea is to expand this window as long as the characters in the substring are unique, and to contract it when a repeated character is encountered....

2 min · 279 words · Apurva Khatri

338. Counting Bits

338. Counting Bits Description: Day III of coding challenges. Observations and Approach While working through examples, I noticed a pattern in how binary numbers are constructed. Specifically, to form any number, you first take the highest power of 2 that is less than or equal to the number, and then form the remaining part with the difference. For example, to form the number 9, we take 8 (which is 2^3, the highest power of 2 less than 9) and subtract it from 9....

2 min · 284 words · Apurva Khatri

45. Jump Game II

This is an interesting problem because it introduced me to a different approach to solving it. Let’s first discuss the initial approach I implemented. Approach I: The first approach is a simple Dynamic Programming solution. The idea is to store the minimum number of jumps required to reach each position. We use the precomputed results for higher stairs, and finally, the value at n-1 will store the minimum jumps required to reach that point....

4 min · 666 words · Apurva Khatri

647. Palindromic Substrings

This problem can be solved using two approaches: Dynamic Programming Time Complexity: O(n²) Space Complexity: O(n²) Two-Pointer (Expand Around Center) Time Complexity: O(n²) Space Complexity: O(1) Approach I: Dynamic Programming In this approach, we aim to check all substrings of the string. Specifically, we will check substrings of lengths 1, 2, 3, …, up to len(s). We use a 2D array dp of size n x n where n is the length of the string....

3 min · 539 words · Apurva Khatri

746. Min Cost Climbing Stairs

Dynamic Programming Approach In Dynamic Programming (DP), we first identify the target variable, which we’ll denote as F(n). The next step is to determine what F(n) depends on. For instance, F(n) might depend on F(n-1) or F(n-3). In this specific problem, F(n) depends on F(n-1) and F(n-2). This means that to reach the nth point, an individual can arrive either from one step before (F(n-1)) or from two steps before (F(n-2))....

1 min · 112 words · Apurva Khatri

91. Decode Ways

I can’t believe I solved this problem on my own without looking up any reference material, applying the concepts of dynamic programming. Let me walk you through my approach and how I navigated the problem. Problem Breakdown Dealing with zeros was particularly tricky, so I had to carefully consider the ways they could create problems. Initially, I thought about the cases where the string cannot be decoded: The string cannot be decoded if s[0] == '0'....

3 min · 629 words · Apurva Khatri