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

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 #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 #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 #1465: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts

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

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 #1443: Minimum Time to Collect All Apples in a Tree

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