Dynamic Programming | Learn for Master
  • Longest Common Subsequence

    A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. It is a set of characters that appear in left-to-right order, but not necessarily consecutively.

    Longest common subsequence (LCS) of 2 sequences is a subsequence, with maximal length, which is common to both the sequences.

    Problem definition of Longest Common Subsequence

    1. Given the first sequence which contains (m) symbols X = (x1, x2, x3, …, xm)
    2. Given the second sequence which contains (n) symbols Y = (y1,
    [Read More...]
  • Leetcode Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S.

    A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ACE” is a subsequence of “ABCDE” while “AEC” is not).

    Here is an example:
    S = “rabbbit”, T = “rabbit”

    Return 3.

    Java solution

    Here are three solutions to solve this problem.


    [Read More...]
  • Leetcode – Maximal Rectangle solution 1 (Java)

    Maximum Rectangle area for a 0 1 matrix.

    Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area.

    Source: https://leetcode.com/problems/maximal-rectangle/
    This is a hard one from Facebook.
    The key to solve this problem is to first build a matrix ones[][], where ones[i][j] means the number of continuous 1s to the cell matrix[i][j] in jth column. See the following figure. 
     leetcode maximal rectangle
    We have:

    We can fill up the matrix ones[][] using dynamic programming. The formula is :

    How to use the ones[][] matrix to calculate the maximum area?

    [Read More...]
  • LeetCode – Count Numbers with Unique Digits (Python)

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

    Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])


    1. A direct way is to use the backtracking approach.
    2. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach number of steps equals to 10n.
    [Read More...]
  • LeetCode-Coin Change Problem (Python)

    You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

    Example 1:
    coins = [1, 2, 5], amount = 11
    return 3 (11 = 5 + 5 + 1)

    Example 2:
    coins = [2], amount = 3
    return -1.

    You may assume that you have an infinite number of each kind of coin.

    [Read More...]