DP
文章平均质量分 62
再见小小ronnie
这个作者很懒,什么都没留下…
展开
-
最长公共子序列问题 Longest Common Subsequence problem
最优子结构设序列 X = {x1, x2, ..., xm} 和序列 Y = {y1, y2, ... , yn} 的最长公共子序列为 Z = {z1, z2, ... , zk}, 那么(1)若xm = yn = zk, 则 zk-1也是xm-1和yn-1的LCS(2)若xm!=yn, xm!=zk, 则Z是xm-1和yn的LCS(3)若yn!=xm, yn!=zk, 则Z是x原创 2016-09-13 05:49:15 · 342 阅读 · 0 评论 -
Leetcode 309. Best Time to Buy and Sell Stock with Cooldown
State machine graph/** * state machine approach * state a(hold): hold stack, sell or rest * state b(sold): just sold stack can only rest * state c(rest): cooldown, buy or rest * translate to for原创 2016-12-18 10:57:34 · 165 阅读 · 0 评论 -
Leetcode 16. 3Sum Closest
/** * Similar with 3sum. * Difference is target now becomes -(nums[i]-1), a * and also need a variable diff to save the closest distance * between triplets and targets. */ public class Solution原创 2017-01-02 07:07:27 · 173 阅读 · 0 评论 -
Leetcode 368. Largest Divisible Subset
/** * similar with the Longest Increasing Subsequence (LIS) * let l[i] to be the largest divisible subset ending at index i * to determine l[i+1] we need to do, * i. for every list l[j] in l[1] t转载 2016-12-19 07:32:06 · 155 阅读 · 0 评论 -
Leetcode 304. Range Sum Query 2D - Immutable
/** * very similar with range sum in 1D array, need a 2D array to save the intermediate values * instantiate a row+1, col+1 array make lot easier* */ public class NumMatrix { private int[][] d原创 2016-12-18 01:53:30 · 139 阅读 · 0 评论 -
Leetcode 337. House Robber III
Naive solution. There are many overlapping sub problems.e.g. if we rob(root), we need to know rob(left), rob(right), rob(left.left), rob(left.right), ...then, say if we want to rob(root.left), the原创 2017-01-18 03:20:42 · 143 阅读 · 0 评论 -
Leetcode 473. Matchsticks to Square
More information. public class Solution { public boolean makesquare(int[] nums) { if (nums == null || nums.length < 4) return false; int sum = 0; for (int num : nums) sum +=转载 2017-01-18 11:39:37 · 213 阅读 · 0 评论 -
Partition Problem
Given a set of positive integers, find if the set can be partitioned into two subsets such that the sum of these two subsets are equal. DFS approach. Sort the array in an increasing order. Create tw原创 2017-01-19 02:15:26 · 916 阅读 · 0 评论 -
Leetcode 131. Palindrome Partitioning
DFS non-DP solution.public class Solution { public List> partition(String s) { List> ret = new ArrayList<>(); List curr = new ArrayList<>(); dfs(0, s, curr, ret)原创 2017-02-10 04:46:08 · 177 阅读 · 0 评论 -
Leetcode 132. Palindrome Partitioning II
function f(i) tells the minimum cuts for sub string (0, i).f(0) = 0.f(1) = min(0 if (0, 1) is a palindrome, f(0)+1 b/c (1, 1) is a palindrome)f(2) = min(0 if (0, 2) is a palindrome, or f(原创 2017-02-10 11:56:47 · 160 阅读 · 0 评论 -
Leetcode 91. Decode Ways
/** * allocate an array w[n] to save ways of decryption for a message which length is n * suppose a1a2...an-1 is the first n-1 digits of the encoded message and has w[n-1] ways to decode * then for原创 2016-10-22 07:21:57 · 238 阅读 · 0 评论 -
Leetcode 300. Longest Increasing Subsequence
/** * let dp[i] be the length of the longest sub-array ending with nums[i] * then dp[n] = dp[m]+1 if nums[n] > nums[m] AND dp[m] is the largest (m is from 1 to n-1) * else dp[n] = 1 */ publi原创 2016-12-17 07:47:24 · 172 阅读 · 0 评论 -
Leetcode 96. Unique Binary Search Trees
Catalan numbers./** * # of binary trees = (# of trees on LHS) * (# of choices as root) * (# of trees on RHS) * let c[n] be the total # of binary trees * c[0] = c[1] = 1 * choose 1 as root, no e原创 2016-10-22 13:18:13 · 183 阅读 · 0 评论 -
Leetcode 120. Triangle
/** 1 * 1 2 * 1 2 3 * 1 2 3 4 * from bottom to top, we don't need to consider the last row (b/c if row # equals to 1 there's only one number just return this value), *原创 2016-11-03 11:52:15 · 218 阅读 · 0 评论 -
Leetcode 322. Coin Change
/** * given an array 'c[]' represents coins and a target value 'amount' * suppose the solution for 'amount' is sol(a) which refers to minimum # of conins to reach the amount * easy to know sol(a) = mi原创 2016-10-19 05:17:37 · 229 阅读 · 0 评论 -
Leetcode 375. Guess Number Higher or Lower II
@stupidbird911 said in [How does one think up DP solutions for these types of problems?](/post/122697):> Practice on similar questions, think carefully and you would get better understanding. Almost原创 2016-11-16 12:04:54 · 262 阅读 · 0 评论 -
Leetcode 338. Counting Bits
/** * num binary cnt * 0 0 0 * 1 1 1 * --------------------------------- * 2 10 1+0 * 3 11 1+1 * --------------------原创 2016-11-02 11:45:03 · 175 阅读 · 0 评论 -
Leetcode 416. Partition Equal Subset Sum
/** * given an array of positive integers and a target * find out if there exists a subset(s) that sum up to the target * subproblems: * 1. the subset includes nums[n] then return isSubsetSum(n-1,原创 2016-11-21 06:36:40 · 316 阅读 · 0 评论 -
Leetcode 357. Count Numbers with Unique Digits
/** * let f(n) to be the count of numbers with unique digits given the length of n * f(1) = 10 * f(2) = f(1) + 9*9, there are 9 choices (1 through 9) to form the first digit, and 9 choices to form原创 2016-12-15 04:10:42 · 175 阅读 · 0 评论 -
Leetcode 53. Maximum Subarray
/** * Kadane's algorithm */ public class Solution { public int maxSubArray(int[] nums) { int max_so_far = nums[0]; int max_ending_here = nums[0]; for (int i=1;原创 2016-12-15 13:23:53 · 197 阅读 · 0 评论 -
Leetcode 264. Ugly Number II
/** * k[0] = 1 * k[1] = min(k[0]*2, k[0]*3, k[0]*5) = 2 * k[2] = min(k[1]*2, k[0]*3, k[0]*5) = 3 * k[3] = min(k[1]*2, k[1]*3, k[0]*5) = 5 * k[4] = min(k[1]*2, k[1]*3, k[1]*5) = 4 * ... */ publ转载 2016-12-16 07:35:37 · 216 阅读 · 0 评论 -
Leetcode 5. Longest Palindromic Substring
/** * for string c1c2c3c4 ... cn, * check every substring of the string, * substrings may start from c1, c2, c3, ... respectively */ public class Solution { public String longestPalindrome(St原创 2017-02-02 11:47:10 · 170 阅读 · 0 评论