随笔分类 -  dp

摘要:public static int LPS(int[] a) { int[][] dp = new int[a.length][a.length]; for (int i = 0; i = 0; i--) { for (int j = i + 1; j < dp.length; j++) { if (a[i] == a[j]) { dp[i][j] = dp[... 阅读全文
posted @ 2017-12-03 04:07 apanda009 阅读(109) 评论(0) 推荐(0)
摘要:Time Complexity: O(N*K), Space Complexity: O(1) The idea is similar to the problem Paint House I, for each house and each color, the minimum cost of p 阅读全文
posted @ 2017-11-30 12:02 apanda009 阅读(146) 评论(0) 推荐(0)
摘要:Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 阅读全文
posted @ 2017-11-07 04:46 apanda009 阅读(105) 评论(0) 推荐(0)
摘要:/*Given a string and dictionary of words, break the string into minimum number of words from the dictionary. Ex: {"jumped", "over", "some", "thing”, " 阅读全文
posted @ 2017-11-01 10:07 apanda009 阅读(549) 评论(0) 推荐(0)
摘要:难度:90. 这道题跟Palindrome Partitioning非常类似,区别就是不需要返回所有满足条件的结果,而只是返回最小的切割数量就可以。做过Word Break的朋友可能马上就会想到,其实两个问题非常类似,当我们要返回所有结果(Palindrome Partitioning和Word B 阅读全文
posted @ 2017-11-01 08:30 apanda009 阅读(112) 评论(0) 推荐(0)
摘要:动归来做, 阅读全文
posted @ 2017-09-28 23:10 apanda009 阅读(111) 评论(0) 推荐(0)
摘要:字符串匹配的问题应该最先想到dp, 主要还是说一下动态规划的方法。跟Regular Expression Matching一样,还是维护一个假设我们维护一个布尔数组res[i],代表s的前i个字符和p的前j个字符是否匹配(这里因为每次i的结果只依赖于j-1的结果,所以不需要二维数组,只需要一个一维数 阅读全文
posted @ 2017-08-19 20:40 apanda009 阅读(156) 评论(0) 推荐(0)
摘要:方法一:分治法 TLE 最基本是得先清楚BST定义. 每个节点的所有左子节点值都比该节点小, 每个节点的所有右子节点值都比该节点小. 知道定义后在构建BST时可逐次选择value做根节点. 利用递归计算根节点左子树可能形状数目, 与根节点右子树可能形状数目, 最后将两个可能形状数目相乘得到所有可能的 阅读全文
posted @ 2017-08-12 09:56 apanda009 阅读(115) 评论(0) 推荐(0)
摘要:The best strategy to play the game is to minimize the maximum loss you could possibly face. Definition of dp[i][j]: minimum number of money to guarant 阅读全文
posted @ 2017-08-08 20:36 apanda009 阅读(118) 评论(0) 推荐(0)
摘要:一个dp,f(i,j)表示前i个house都paint了且第i个house paint成color_j的最小cost。 背包问题同 Minimum Adjustment Cost Better Solution: O(N) time, O(1) space The basic idea is whe 阅读全文
posted @ 2017-08-06 16:51 apanda009 阅读(233) 评论(0) 推荐(0)
摘要:这道题要看出是背包问题,不容易,跟FB一面 paint house很像,比那个难一点 定义res[i][j] 表示前 i个number with 最后一个number是j,这样的minimum adjusting cost 阅读全文
posted @ 2017-08-06 14:12 apanda009 阅读(179) 评论(0) 推荐(0)
摘要:状态: 一维的肯定不行, 二维的三个变量轮着试了一下也不行, 干脆把三维的都装上, 方程: 用遍历到当前的状态时, 用不用的上当前的元素(如何判断, 根据题意和当前元素的有无对另一维状态变量的改变来判断)来划分情况, 然后看看是状态相加还是 求最大, 还是怎么的 初始化边界点很重要: 注意size都 阅读全文
posted @ 2017-08-06 12:05 apanda009 阅读(169) 评论(0) 推荐(0)
摘要:状态和状态方程的思路和1 一样, 当前背包加用vs 不用, 找到与题意链接的一个状态, 再找他的对立面 一维 阅读全文
posted @ 2017-08-06 11:06 apanda009 阅读(176) 评论(0) 推荐(0)
摘要:用 integer.MIN_VALUE 来替代 根据当前背包能否用的上来求递推式, 结果是求m最大时的最大值, 因此要不断更新容量为j 时的最大值, 并更新全局最大值 阅读全文
posted @ 2017-08-05 23:46 apanda009 阅读(252) 评论(0) 推荐(0)
摘要:动态规划重点在于找到:维护量,递推式。维护量通过递推式递推,最后往往能得到想要的结果 先说说维护量,res[i][j]表示用s1的前i个字符和s2的前j个字符能不能按照规则表示出s3的前i+j个字符,如此最后结果就是res[s1.length()][s2.length()],判断是否为真即可。接下来 阅读全文
posted @ 2017-08-05 21:56 apanda009 阅读(141) 评论(0) 推荐(0)
摘要:res[i][j]表示Edit Distance between X数组的前i个元素以及Y数组的前j个元素,或者the minimum # of operations to convert X前i个元素 into Y的前j个元素 因为对于Xi 和 Yj,操作无非是 insert, delete, r 阅读全文
posted @ 2017-08-05 20:45 apanda009 阅读(208) 评论(0) 推荐(0)
摘要:状态方程时题意的转化, 通常要if, 遍历到当前状态时, 最后一个字母的情况与上一个或者上多个状态的关系 结果是最后的状态还是只是遍历到最后的状态求全局最优 如Longest Increasing Subsequence 阅读全文
posted @ 2017-08-05 18:19 apanda009 阅读(324) 评论(0) 推荐(0)
摘要:巧用 Integer.MAX_VALUE;来替换 true 阅读全文
posted @ 2017-08-05 17:52 apanda009 阅读(136) 评论(0) 推荐(0)
摘要:初始化, if 判断不同的状态 阅读全文
posted @ 2017-08-05 17:37 apanda009 阅读(127) 评论(0) 推荐(0)
摘要:You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Have you met this question in a... 阅读全文
posted @ 2017-08-05 17:29 apanda009 阅读(120) 评论(0) 推荐(0)