序列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]; }}
- DP 子序列问题
- 序列DP问题
- hdu 1160 DP 最长子序列问题
- 最长公共子序列问题 经典DP
- [dp]最长公共子序列问题
- DP问题之最优加法序列
- DP---LCS 最长公共子序列问题
- 用DP解决最长子序列问题
- dp经典问题 最长非降子序列
- dp经典问题最大子序列和问题
- 哈理工OJ 1597 序列问题II【dp】【非递增子序列问题】
- NYOJ214 单调递增子序列(二) 【dp问题】
- NYOJ 36 最长公共子序列 【dp问题】
- poj-1836-Alignment-dp-最长最短子序列问题
- nyoj 214 单调递增子序列问题(dp)
- DP最长公共子序列LCS问题笔记
- hdu1160 - FatMouse's Speed (dp 最长有序子序列问题)
- hdu1159 - Common Subsequence (dp 最长公共子序列问题)
- Excel导出
- 爬虫学习01 发送带header的请求 发送带参数的请求
- MXNet官方文档中文版教程(4):神经网络训练和推断(Module)
- 使用OLAMI SDK和讯飞语音合成制作一个语音回复的短信小助手
- Android retrofit2 详解
- 序列DP问题
- 字典的一些基础用法
- 51nod 最大M子段和V2【贪心】【链表】【堆】
- 关于java继承的一些理解错误
- 虚拟桌面模拟查找点击自绘控件
- 常见的内存错误及对策
- Vi常用快捷键、文件权限操作、查看文件若干命令
- Python动态导入模块
- [Leetcode] 423. Reconstruct Original Digits from English 解题报告