First time here? Start with Subscribing
Posts in “Problem Solving”
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.
What is the maximum possible sum of any two numbers from the array? It is sum of highest 2 numbers. Similarly, minimum sum is sum of lowest 2 numbers. Target is going to be somewhere in between these two numbers. What if we start in the middle? … More Leetcode 167. Two Sum II – Input array is sorted
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
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
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
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
Secondly, we need to add a number in front in case of a conflict. So each folder name should have a number associated with it. The most intuitive data structure for this is a hash map. The corner case we need to handle is that what if we append a number as a suffix and generate … … More Leetcode #1487: Making File Names Unique
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
So the only task for us is to find what would be the height and width of each piece after all cuts and then find the piece with maximum height and width. Each cut in the cake, divides the cake into pieces with certain height and certain width. We want to maximize the area … … More Leetcode #1465: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
For example, say at t=0, t=1, and t=2, the number of frogs that are croaking is 1, 2, and 1 respectively. Finding how many frogs are croaking at any time is also not difficult. We know that each “croaking” starts with a ‘c’ and ends with a ‘k’. … More Leetcode #1419: Minimum Number of Frogs Croaking
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
Whenever you are at a node, say p, you will collect all apples in p’s subtree before returning back to the original root. This will avoid traveling the same path multiple times. …. Because you have the list of edges, construct a better representation – adjacency list – of the tree. … More Leetcode #1443: Minimum Time to Collect All Apples in a Tree
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