序列DP问题

来源:互联网 发布:手机网站源码模板 编辑:程序博客网 时间:2024/06/03 21:26

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] = 1answer: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] = trueanswer:dp[n-1]     *///出现TLEclass 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];    }}




原创粉丝点击