回溯到动态规划
dp的开始从一个典型的例子开始:
300. 最长上升子序列
首先试着用回溯法来解决这个问题:
class Solution {
int res = 0;
public int lengthOfLIS(int[] nums) {
if(nums.length == 0) return res;
helper(nums, new ArrayList<>(),0);
return res;
}
public void helper(int[] nums, List<Integer> list, int index){
if(index >= nums.length) return;
for(int i = index; i < nums.length; i++){
if(list.size() > 0 && nums[i] <= list.get(list.size()-1)) continue;
list.add(nums[i]);
res = Math.max(res, list.size());
helper(nums, list, i+1);
list.remove(list.size()-1);
}
}
}
额,发现超时了~~~~ 好吧,上DP吧:
class Solution {
public int lengthOfLIS(int[] nums) {
int res = 0;
if(nums.length == 0) return res;
int[] dp = new int[nums.length];
for(int i = 0; i<nums.length;i++) dp[i] = 1;
for(int i = nums.length-2; i>=0 ;i--){
for(int j = i+1; j<nums.length; j++){
if(nums[i] < nums[j]) dp[i] = Math.max(dp[j] + 1,dp[i]);
}
}
for(int i = 0; i<nums.length; i++)
res = Math.max(res,dp[i]);
return res;
}
}
我们定义dp[i]为元素nums[i]为起点最长上升子序列的长度,初始化为1,从最后一个元素,依次向前推,最后找dp数组中最大的那个数,下标即为从当前位置出发,最长上升子序列
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
int len = intervals.length;
if(len == 0) return 0;
int width = intervals[0].length;
if(width == 0) return 0;
Arrays.sort(intervals, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
if(a[0] == b[0])
return a[1]-b[1];
else
return a[0]-b[0];
}
});
int[] dp = new int[len];
Arrays.fill(dp,1);
for(int i = len-2; i>=0; i--){
for(int j = i+1; j<len; j++){
if(intervals[j][0] >= intervals[i][1])
dp[i] = Math.max(dp[j]+1, dp[i]);
}
}
for(int i = 0; i<len; i++)
dp[0] = Math.max(dp[0],dp[i]);
return len-dp[0];
}
}
常规先用回溯来解决,超时再来考虑dp:
class Solution {
int res = 1;
public int wiggleMaxLength(int[] nums) {
if(nums.length <= 1) return nums.length;
helper(nums, 0, new ArrayList<>(), 0);
return res;
}
public void helper(int[] nums, int index, List<Integer> list, int pre){
if(index >= nums.length) return;
for(int i = index; i<nums.length; i++){
if((list.size() > 0 && pre*(nums[i] - list.get(list.size()-1)) > 0)
|| (list.size() > 0 && nums[i] == list.get(list.size()-1))) continue;
if(list.size() == 0){
list.add(nums[i]);
continue;
}
int back = pre;
pre = nums[i] - list.get(list.size()-1);
list.add(nums[i]);
res = Math.max(res, list.size());
helper(nums, i+1, list, pre);
pre = back;
list.remove(list.size()-1);
}
return;
}
}
这题的思路和上一题基本是一样的,不过稍微复杂一点点,需要定义一个pre来记录之前的状态,如果之前两数之差为正数,那就将pre置为正数,这里用了这么一个条件:pre*(nums[i] - list.get(list.size()-1)) > 0,这个表示下一次的两数之差一定和上一次的相反,如果相同就continue;同样记录list中的最大个数,这里稍微注意两个数相同的情况
超时了~
所以,派大星准备好了吗?马上要上dp了!!!!!!
class Solution {
public int wiggleMaxLength(int[] nums) {
int len = nums.length;
if(len <= 1) return len;
/**
* 定义dp[i][0] 表示 下标i到末尾最长wig,此处表示与上一个差为负数
* 定义dp[i][1] 表示 下标i到末尾最长wig,此处表示与上一个差为正数
*/
int[][] dp = new int[len][2];
dp[len-1][0] = 1;
dp[len-1][1] = 1;
for(int i = len-2; i>=0; i--){
for(int j = i+1; j<len; j++){
if(nums[i] > nums[j]){
dp[i][1] = Math.max(dp[j][0]+1, dp[i][1]);
dp[i][0] = Math.max(dp[i][0], dp[j][0]);
}else if(nums[i] < nums[j]){
dp[i][0] = Math.max(dp[j][1]+1, dp[i][0]);
dp[i][1] = Math.max(dp[i][1], dp[j][1]);
}else{
dp[i][1] = Math.max(dp[i][1], dp[j][1]);
dp[i][0] = Math.max(dp[i][0], dp[j][0]);
}
}
}
return Math.max(dp[0][0],dp[0][1]);
}
}
还可以优化,我想想,能不能更快点
日常回溯~
class Solution {
int res = Integer.MAX_VALUE;
public int minPathSum(int[][] grid) {
if(grid.length <= 0 || grid[0].length <= 0) return 0;
helper(grid,grid[0][0],0,0);
return res;
}
public void helper(int[][] grid, int sum, int x, int y){
if(x == grid.length-1 && y == grid[0].length-1){
res = Math.min(sum,res);
return;
}
int new_x = x+1;
if(InArea(new_x,y,grid))
helper(grid,sum+grid[new_x][y],new_x,y);
int new_y = y+1;
if(InArea(x,new_y,grid))
helper(grid,sum+grid[x][new_y],x,new_y);
return;
}
boolean InArea(int x, int y, int[][]grid){
if(x >= grid.length || y >= grid[0].length || x < 0 || y < 0) return false;
return true;
}
}
超时!!!!!!那就上dp:
class Solution {
public int minPathSum(int[][] grid) {
if(grid.length <= 0 || grid[0].length <= 0) return 0;
int len = grid.length;
int width = grid[0].length;
int[][] dp = new int[len][width];
int sum = 0;
for(int i = 0; i<width; i++){
sum += grid[0][i];
dp[0][i] = sum;
}
sum = 0;
for(int i = 0; i<len; i++){
sum += grid[i][0];
dp[i][0] = sum;
}
for(int i = 1; i<len; i++){
for(int j = 1; j<width; j++){
dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1])+grid[i][j];
}
}
return dp[len-1][width-1];
}
}
其中dp[i][j]表示从起点到该坐标的最短路径
这道题拿到手想用回溯的思路解决:
class Solution {
int res = 0;
public int maxProfit(int[] prices) {
if(prices.length == 0) return 0;
helper(prices,0,0);
return res;
}
public void helper(int[] prices, int sum, int index){
if(index >= prices.length) return;
for(int i = index; i<prices.length; i++){
for(int j = i+1; j<prices.length; j++){
if(prices[j] <= prices[i]) continue;
sum += prices[j]-prices[i];
res = Math.max(res,sum);
helper(prices,sum,j+2);
sum -= prices[j]-prices[i];
}
}
return;
}
}
很显然,两层循环下再回溯,肯定会超时:
那就只能dp解决了: