随笔分类 -  DP

摘要:class Solution { public int rob(int[] nums) { if(nums.length==0) return 0; if(nums.length==1) return nums[0]; return Math.max(rob1(nums, 0, nums.le... 阅读全文
posted @ 2017-11-07 08:03 Weiyu Wang 阅读(167) 评论(0) 推荐(0)
摘要:class Solution { public int maxProfit(int k, int[] prices) { if(k>=prices.length/2) { int maxProfit=0; for(int i=1;iprices[i-1]?prices[i]-prices[i-1]:0; ... 阅读全文
posted @ 2017-10-26 01:19 Weiyu Wang 阅读(143) 评论(0) 推荐(0)
摘要:class Solution { public int calculateMinimumHP(int[][] dungeon) { if(dungeon.length==0||dungeon[0].length==0) return 0; int M=dungeon.length; int N=dungeon[0].... 阅读全文
posted @ 2017-10-25 02:55 Weiyu Wang 阅读(94) 评论(0) 推荐(0)
摘要:class Solution { public boolean wordBreak(String s, List wordDict) { boolean[] dp=new boolean[s.length()+1]; dp[0]=true; for(int i=0;i=0&&w.equals(s.substring(i+1-w.length... 阅读全文
posted @ 2017-10-10 03:50 Weiyu Wang 阅读(125) 评论(0) 推荐(0)
摘要:class Solution { public int minCut(String s) { int[] dp=new int[s.length()+1]; for(int i=0;i=0&&i+len=0&&i+len<s.length()&&s.charAt(i-1-len)==s.charAt(i+len);len++) ... 阅读全文
posted @ 2017-10-05 04:23 Weiyu Wang 阅读(146) 评论(0) 推荐(0)
摘要:压缩 阅读全文
posted @ 2017-10-02 03:22 Weiyu Wang 阅读(124) 评论(0) 推荐(0)
摘要:class Solution { public int minimumTotal(List> triangle) { int[] dp=new int[triangle.size()]; for(int i=triangle.size()-1;i>=0;i--) for(int j=0;j<=i;j++) ... 阅读全文
posted @ 2017-09-30 09:48 Weiyu Wang 阅读(120) 评论(0) 推荐(0)
摘要:class Solution { public int numDistinct(String s, String t) { int[][] dp=new int[t.length()+1][s.length()+1]; for(int i=0;i<=t.length();i++) for(int j=i;j<=s.length();... 阅读全文
posted @ 2017-09-30 04:44 Weiyu Wang 阅读(121) 评论(0) 推荐(0)
摘要:class Solution { public boolean isInterleave(String s1, String s2, String s3) { if(s1.length()+s2.length()!=s3.length()) return false; boolean [][] dp=new boolean[s1.l... 阅读全文
posted @ 2017-09-29 06:20 Weiyu Wang 阅读(122) 评论(0) 推荐(0)
摘要:public class Solution { public int numTrees(int n) { int[] dp=new int[n+1]; dp[0]=1; dp[1]=1; for(int i=2;i<=n;i++) for(int j=1;j<=i;j++) ... 阅读全文
posted @ 2017-09-29 02:30 Weiyu Wang 阅读(130) 评论(0) 推荐(0)
摘要:public class Solution { public int numDecodings(String s) { if(s.length()==0) return 0; int[] dp=new int[s.length()+1]; dp[0]=1; for(int i=0;i'0'&&s.ch... 阅读全文
posted @ 2017-09-28 13:14 Weiyu Wang 阅读(132) 评论(0) 推荐(0)
摘要:class Solution { public int minDistance(String word1, String word2) { int[][] dp=new int[word1.length()+1][word2.length()+1]; for(int i=0;i<=word1.length();i++) dp[i][... 阅读全文
posted @ 2017-09-26 10:52 Weiyu Wang 阅读(131) 评论(0) 推荐(0)
摘要:class Solution { public int minPathSum(int[][] grid) { for(int i=0;i0&&j>0) grid[i][j]+=Math.min(grid[i-1][j],grid[i][j-1]); else if(j>0) ... 阅读全文
posted @ 2017-09-26 05:11 Weiyu Wang 阅读(108) 评论(0) 推荐(0)
摘要:class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { for(int i=0;i=0?1:0; for(int i=0;i=0) obstacleGrid[i][j]+=(i>0&&obstacleGrid[i-1][... 阅读全文
posted @ 2017-09-26 04:56 Weiyu Wang 阅读(139) 评论(0) 推荐(0)
摘要:class Solution { public int uniquePaths(int m, int n) { int[][] dp=new int[m][n]; dp[0][0]=1; for(int i=0;i0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0); return dp[m-1][n-1];... 阅读全文
posted @ 2017-09-26 04:47 Weiyu Wang 阅读(82) 评论(0) 推荐(0)
摘要:class Solution { public boolean isMatch(String s, String p) { boolean[][] dp=new boolean[p.length()+1][s.length()+1]; dp[0][0]=true; for(int i=1;i0&&(p.charAt(i-1)==s.char... 阅读全文
posted @ 2017-09-25 02:01 Weiyu Wang 阅读(109) 评论(0) 推荐(0)
摘要:Recursive DP 阅读全文
posted @ 2017-09-22 04:31 Weiyu Wang 阅读(169) 评论(0) 推荐(0)
摘要:Time Limit: 3000MSMemory Limit: 65536KTotal Submissions: 26714Accepted: 8915DescriptionA palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right... 阅读全文
posted @ 2009-10-14 12:09 Weiyu Wang 阅读(791) 评论(0) 推荐(0)
摘要:Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 8125Accepted: 4497DescriptionIt is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simp... 阅读全文
posted @ 2009-10-14 11:05 Weiyu Wang 阅读(798) 评论(0) 推荐(0)
摘要:Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 5823Accepted: 3802DescriptionThe cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though... 阅读全文
posted @ 2009-10-13 13:25 Weiyu Wang 阅读(487) 评论(0) 推荐(0)