Posts

Showing posts from September, 2018

Related Prolems

1. Combination Sum I, II, III, IV 2. House Rob 3. Buy Stock

Dynamic Programming (Part 2)

877 Stone Game    dp[i][j] means the biggest number of stones you can get more than opponent picking piles in piles[i] ~ piles[j]. You can first pick piles[i] or piles[j].     If you pick piles[i], your result will be piles[i] - dp[i + 1][j]     If you pick piles[j], your result will be piles[j] - dp[i][j - 1] So we get: dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1]) We start from smaller subarray and then we use that to calculate bigger subarray.  309 Best Time to Buy and Sell Stock with Cooldown   int n = prices.size();         // sell[i] : maximum profit when the i-th day NOT hold the stock         vector<int> sell(n, 0);         // hold[i] :  maximum profit when the i-th day hold the stock         vector<int> hold(n, 0);   ...