Dynamic Programming

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Longest Increasing Subsequence

3, 1, 4, 5, 7, 6, 8, 2

1, 4, 5, 6, 8 (Or 1, 4, 5, 7, 8)

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Longest Increasing Subsequence

What is the optimal sub-structure?

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Longest Increasing Subsequence

Copyright © 直通硅谷

http://www.zhitongguigu.com/

We store lis[i] for the LIS by i?

We store lis[i] for the LIS using sequence[i]

Longest Increasing Subsequence

public int longestIncreasingSubsequence(int[] nums) {
    if(nums.length == 0){
        return 0;
    }
    int[] lis = new int[nums.length];
    int max = 0;
    for (int i = 0; i < nums.length; i++){
        int localMax = 0;
        for (int j = 0; j < i; j++){
            if (lis[j] > localMax && nums[j] <= nums[i]){
                localMax = lis[j];
            }
        }
        lis[i] = localMax + 1;
        max = Math.max(max, lis[i]);
    }
    return max;
}

Copyright © 直通硅谷

http://www.zhitongguigu.com/

What is the Time Complexity?

What is the Space Complexity?

Can we do better?

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Patient Sort

Copyright © 直通硅谷

http://www.zhitongguigu.com/

1, 3, 5, 2, 8, 4, 7, 6, 0, 9, 10

1 -> 0
1,3 -> 1,2
1,3,5 -> 1,3,4
1,3,5,8 -> 1,3,5,7 -> 1,3,5,6
1,3,5,6,9
1,3,5,6,9,10
public int longestIncreasingSubsequence(int[] nums) {
    if(nums.length == 0){
        return 0;
    }
    int len = 0;
    int[] tails = new int[nums.length];
    tails[0] = nums[0];
    for(int i = 1; i < nums.length; i++){
        if(nums[i] < tails[0]){
            tails[0] = nums[i];
        } else if (nums[i] >= tails[len]){
            tails[++len] = nums[i];
        } else {
            tails[binarySearch(tails, 0, len, nums[i])] = nums[i];
        }
    }
    return len + 1;
}
private int binarySearch(int[] tails, int min, int max, int target){
    while(min < max){
        int mid = min + (max - min) / 2;
        if(tails[mid] == target){
            return mid;
        }
        else if(tails[mid] < target){
            min = mid + 1;
        }
        else max = mid;
    }
    return min;
}

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Longest Common Sequence

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Longest Common Sequence

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Example: abcfbc abfcab

return 4 (abcb)

Longest Common Sequence

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Example: abcfbc abfcab

return 4 (abcb)

Longest Common Sequence

What is the optimal sub-structure?

maxCommon(i,j): longest common string for String A(0,i) and String B(0,j)

We finally need to get maxCommon(stringA.length, stringB.length)

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Longest Common Sequence

Copyright © 直通硅谷

http://www.zhitongguigu.com/

What is the relationship between maxCommon(i,j) and maxCommon(i-1,j-1)?

If(A[i-1] = B[j-1]) ?

If(A[i-1] != B[j-1])?

Longest Common Sequence

Copyright © 直通硅谷

http://www.zhitongguigu.com/

What is the relationship between maxCommon(i,j) and maxCommon(i-1,j-1)?

If(A[i-1] = B[j-1]) ?

If(A[i-1] != B[j-1])?

maxCommon(i,j) = maxCommon(i-1,j-1) + 1

maxCommon(i,j) = max(maxCommon(i-1,j), maxCommon(i,j-1))

Longest Common Sequence

Copyright © 直通硅谷

http://www.zhitongguigu.com/

public static int longestCommonString(String a, String b) {
	int m = a.length();
	int n = b.length();
	int[][] maxCommon = new int[m+1][n+1];
	for(int i = 0; i <= m; i ++) {
		maxCommon[i][0] = 0;
	}
	for(int j = 0; j <= n; j ++) {
		maxCommon[0][j] = 0;
	}
	for(int i = 1; i <= m; i ++) {
		for(int j = 1; j <= n; j ++) {
		  if(a.charAt(i-1) == b.charAt(j-1)) {
		    maxCommon[i][j] = maxCommon[i-1][j-1] + 1;
		  }
		  else {
		    maxCommon[i][j] = Math.max(maxCommon[i][j-1], maxCommon[i-1][j]);
		  }
		}
	}
	return maxCommon[m][n];
}

Edit Distance

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Replace: abc -> abd

Remove: abc -> ab

Add: abc -> abcd

How many steps you need from String A to B?

e.g.  abca -> eeba 3 steps

abcdf -> eecf 3 steps

Edit Distance

Copyright © 直通硅谷

http://www.zhitongguigu.com/

What is the optimal sub-structure?

what is the connection between EDIT[i,j]

and EDIT[i-1,j], EDIT[i,j-1] and EDIT[i-1,j-1]?

Edit Distance

Copyright © 直通硅谷

http://www.zhitongguigu.com/

what is the connection between EDIT[i,j]

and EDIT[i-1,j], EDIT[i,j-1] and EDIT[i-1,j-1]?

if(A[i-1]!=B[j-1])?

if(A[i-1]=B[j-1])?

Copyright © 直通硅谷

http://www.zhitongguigu.com/

public static int editDistance(String a, String b) {
    int m = a.length() + 1; 
    int n = b.length() + 1;
    int[][] f = new int[m][n];
    for(int i = 0; i < m; i ++) {
        f[i][0] = i;
    }
    for(int j = 0; j < n; j ++) {
        f[0][j] = j;
    }
	
    for(int i = 1; i < m; i ++) {
        for(int j = 1; j < n; j ++) {
            if(a.charAt(i-1) == b.charAt(j-1)) {
                f[i][j] = f[i-1][j-1];
            }
            else {
                f[i][j] = f[i-1][j-1] + 1;
            }
            f[i][j] = Math.min(f[i][j], Math.min(f[i-1][j]+1, f[i][j-1]+1));
        }
    }
    return f[m-1][n-1];
}

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Homework

Unique Path II

Climbing Stairs

Maximum Subarray

Copyright © 直通硅谷

http://www.zhitongguigu.com/

Homework (Optional)

Longest Valid Parentheses

Triangle

Best Time to Buy and Sell Stock

Distinct Subsequences

[GoValley-201612] Dynamic Programming 2

By govalley201612

[GoValley-201612] Dynamic Programming 2

  • 1,002