序列DP问题

70. Climbing Stairs

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?

Note: Given n will be a positive integer.

/*
state:dp[i] 到达第i个位置有多少种方式
function:dp[i] = dp[i-1]+dp[i-2]
intialize:dp[0] = 1 dp[1] = 1
answer:dp[m][n]         
*/
class Solution {
    public int climbStairs(int n) {
        int[] dp = new int[n+1];
        dp[0] = 1;dp[1] = 1;
        for(int i = 2; i <= n; i++)
            dp[i] = dp[i-1] + dp[i-2];
        return dp[n];
    }
}


55. Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

/*
state:dp[i] 能否从起点到达第i个位置
function:dp[i] = OR(dp[j]&&j+A[j]>=i) for j = [0,i)
intialize:dp[0] = true
answer:dp[n-1]     
*/
//出现TLE
class Solution {
    public boolean canJump(int[] nums) {
        if(nums == null || nums.length == 0)
            return true;
        int n = nums.length;
        boolean[] dp = new boolean[n];
        dp[0] = true;
        for(int i = 1; i < n; i++){
            for(int j = 0; j < i;j++){
                if(dp[j] && j+nums[j] >= i){
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[n-1];
    }
}


132. Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

/*
state:dp[i] 前i个字符最少可以切出多少个回文字符串
function:dp[i] = for j = [0,i) if(s.substring(j+1,i)是回文串) dp[i] = min(dp[j]+1)
intialize:dp[i] = i;
answer:dp[n]-1     
*/
class Solution {
    public boolean[][] palindrome;
    public void isPalindrome(String s){
        int n = s.length();
        palindrome = new boolean[n][n];
        for(int i = n-1; i >= 0; i--){
            for(int j = i ; j < n; j++){
               palindrome[i][j] = s.charAt(i) == s.charAt(j) && (j - i < 3 || palindrome[i + 1][j - 1]); 
            }
        }
    }
    public int minCut(String s) {
        isPalindrome(s);
        if(s == null || s.length() == 0)
            return -1;
        int n = s.length();
        int[] dp = new int[n+1];
        
        //intialize
        for(int i = 0; i <= n; i++)
            dp[i] = i;
        
        for(int i = 1; i <= n; i++){
            for(int j = 0; j < i;j++){
                if(palindrome[j][i-1])
                    dp[i] = Math.min(dp[i],dp[j]+1);
            }
        }
        return dp[n]-1;
    }
}

139. Word Break

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

/*
state:dp[i] 前i个字符是否可以被完美切分
function:dp[i] = for j = [0,i) OR(dp[j]&&s.substring(j+1,i)是词典中的词)
intialize:dp[0] = true;
answer:dp[n]    
*/
class Solution{
    public boolean wordBreak(String s,List<String> wordDict){
        int n = s.length();
        boolean[] dp = new boolean[n+1];
        dp[0] = true;
        for(int i = 1; i <= n; i++){
            for(int j = 0; j < i; j++){
                if(dp[j] && wordDict.contains(s.substring(j,i))){
                    dp[i] = true;break;
                }
            }
        }
        return dp[n];
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值