Problem Solving

Problem Solving

First time here? Start with Subscribing

Posts in “Problem Solving”

Leetcode 1. Two Sum - Diagram to assist visualization, Problem Solving, Leetcode, Leetcode Solutions, Crack Coding Interview, Coding Interview, Data Structures and Algorithms

Leetcode 15. 3Sum

As we have already solved “Two Sum” problem already, let’s reuse our existing knowledge to solve this problem. We know the target and we want to search 3 numbers. We know how to search 2 numbers, so if we fix one number at a time and look for the other two numbers using our previous solution that would solve this problem as well.

Prefer video over text? Watch the explanation here: https://youtu.be/MmhfOzYMYPkMore Leetcode 15. 3Sum

Leetcode 70. Climbing Stairs - Diagram to assist visualization, Problem Solving, Leetcode, Leetcode Solutions, Crack Coding Interview, Coding Interview, Data Structures and Algorithms

Leetcode 70. Climbing Stairs

Now that we know 3rd step can be reached directly from 1st and 2nd, what will be the number of ways in which we can reach the step 3? It would be all the ways in which we can reach the step 1 (because we can always take 2 steps from here to reach the step 3) + the number of ways in which we can reach the step 2 (because we can always take 1 more step to reach the step 3). … More Leetcode 70. Climbing Stairs

Leetcode 1. Two Sum - Diagram to assist visualization, Problem Solving, Leetcode, Leetcode Solutions, Crack Coding Interview, Coding Interview, Data Structures and Algorithms

Leetcode 1. Two Sum

once we fix one number, say K, then we know what the other number must be and i.e. target-K. For any other number, they won’t add up to the target. Now if we know that search is a vital operation, employ the best data structure for the search i.e. hash set. … More Leetcode 1. Two Sum

Leetcode #1529: Bulb Switcher IV - Diagram to assist visualization, Problem Solving, Leetcode, Leetcode Solutions, Crack Coding Interview, Coding Interview, Data Structures and Algorithms

Leetcode #1529: Bulb Switcher IV

Certainly, a “Flip” is a must when the current status of a bulb at index i and it’s the target status are not the same.

Initially all bulbs are OFF i.e. 0 and when a bulb at index i is flipped, every bulb after (in the right -> direction) that bulb gets flipped. So it only makes sense to start getting the status of the bulbs correct int the left to right order. … More Leetcode #1529: Bulb Switcher IV

Leetcode #1503: Last Moment Before All Ants Fall Out of a Plank - Diagram to assist visualization, Problem Solving, Leetcode, Leetcode Solutions, Crack Coding Interview, Coding Interview, Data Structures and Algorithms

Leetcode #1503: Last Moment Before All Ants Fall Out of a Plank

The problem is a lot about observation. Observing and analyzing the ant behavior will reveal that the problem is way too tricky to understand but way too easy to implement. The idea is to think of the bigger picture and focus only on the paths of the ants. At any collision, think of it as ants transferring their responsibility … … More Leetcode #1503: Last Moment Before All Ants Fall Out of a Plank

Leetcode #1488: Avoid Flood in The City - Diagram to assist visualization, Problem Solving, Leetcode, Leetcode Solutions, Crack Coding Interview, Coding Interview, Data Structures and Algorithms

Leetcode #1488: Avoid Flood in The City

When drying lake #L, it is only useful to dry it if it is FULL already. Otherwise, it’s just a waste to dry an empty lake. (Explained this in the code with a small example as well). In short, drying a lake #L only makes sense if it is already FULL and it is going to rain on that lake in future.

Which lake to dry on a day when there is no rain, can not be determined without knowing the rain sequence that is coming next. … More Leetcode #1488: Avoid Flood in The City

Leetcode #1423: Maximum Points You Can Obtain from Cards - Leetcode Solutions - Problem Solving

Leetcode #1423: Maximum Points You Can Obtain from Cards

You can’t choose the 2nd element from the front unless you have chosen the first one. Similarly, you can’t choose the 2nd element from the back unless you have chosen the last one already. You want to choose a total of K numbers. There are limited number of ways in which you can choose those numbers given the constraints … … More Leetcode #1423: Maximum Points You Can Obtain from Cards

Leetcode #1472: Design Browser History | Leetcode | Problem Solving | Coding Interview

Leetcode #1472: Design Browser History

Thought Process – Say you visit a website A. Then got to B, C, and D in the order. If you go back, you will be at C. Go back further, you will be at B, and if you go back again, you will reach A.
Thing to note is that the order of websites totally reversed when going back compared to the order in which you visited them. So certainly we need a stack data structure for the back operation which will store the history. … More Leetcode #1472: Design Browser History