Leetcode 518. Coin Change II

In this post, solution to leetcode problem – 518, coin change II – is explained. Please refer to this link for detail problem description. This is very similar to combination sum IV problem. The only difference is that the order doesn’t matter and this is essentially a subset problem with duplicates. Let’s see how we can approach the problem. Brute Force Exhaustively enumerate all subsets and check if it makes the amount. This will have exponential cost. Optimized version We can follow the similar approach as combination sum IV problem. The only difference is that order doesn’t matter and if READ MORE

Leetcode 377. Combination Sum IV

This post explains how to solve leetcode 377. combination sum IV problem. It starts with brute force idea and optimize to efficiently solve the problem. Please refer to this link for problem detail. In this problem, you have two arguments – target and nums array. You are supposed to find the number of combinations to make the target amount only using numbers in nums array. In this problem, the order matters as you see in the example. Let’s think about brute force idea – enumerate all the possible subsets of nums array. For each subset, you also need to check READ MORE

Intro to dynamic programming

This article is a simple intro to dynamic programming. Dynamic programming is typically considered the hardest algorithm to work with. However, there is a systematic way to solve the problem and we just need to practice based on that method. Dynamic programming is best suited for the situation that needs to eliminate duplicate computations by caching it. Now let’s take a look at what steps are necessary to solve the problem. Steps to solve DP Define the state List out all the state transitions Implement a recursive solution Memoize (top-down) Make it bottom-up Define the state What are states in READ MORE