DP

你是怎么想到的+复杂度

  • 多重循环

  1. 定义DP数组,一般自顶向下,从起点到该位置的int,boolean
  2. 初始化DP数组
  3. 多重循环填充数组

568. Maximum Vacation Days

LeetCode wants to give one of its best employees the option to travel among N cities to collect algorithm problems. But all work and no play makes Jack a dull boy, you could take vacations in some particular cities and weeks. Your job is to schedule the traveling to maximize the number of vacation days you could take, but there are certain rules and restrictions you need to follow.

Rules and restrictions:
You can only travel among N cities, represented by indexes from 0 to N-1. Initially, you are in the city indexed 0 on Monday.
The cities are connected by flights. The flights are represented as a NN matrix (not necessary symmetrical), called flights representing the airline status from the city i to the city j. If there is no flight from the city i to the city j, flights[i][j] = 0; Otherwise, flights[i][j] = 1. Also, flights[i][i] = 0 for all i.
You totally have K weeks (each week has 7 days) to travel. You can only take flights at most once per day and can only take flights on each week's Monday morning. Since flight time is so short, we don't consider the impact of flight time.
For each city, you can only have restricted vacation days in different weeks, given an N
K matrix called days representing this relationship. For the value of days[i][j], it represents the maximum days you could take vacation in the city i in the week j.
You're given the flights matrix and days matrix, and you need to output the maximum vacation days you could take during K weeks.

Example 1:
Input:flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[1,3,1],[6,0,3],[3,3,3]]
Output: 12
Explanation:
Ans = 6 + 3 + 3 = 12.

One of the best strategies is:
1st week : fly from city 0 to city 1 on Monday, and play 6 days and work 1 day.
(Although you start at city 0, we could also fly to and start at other cities since it is Monday.)
2nd week : fly from city 1 to city 2 on Monday, and play 3 days and work 4 days.
3rd week : stay at city 2, and play 3 days and work 4 days

注意飞不到的时候如何处理,二维DP

   public int maxVacationDays(int[][] flights, int[][] days) {
       int N = flights.length;
       int K = days[0].length;
       int[][] dp = new int[N][K];
       boolean[][] canReach = new boolean[N][K];
       int res = 0;
       // init : week1 start from city 0
       for (int i = 0; i < N; i++) {
           if (i == 0 || flights[0][i] == 1) {
               
               dp[i][0] = days[i][0];
               canReach[i][0] = true;
           }
       }
       // dp
       for (int j = 1; j < K; j++) {
           for (int i = 0; i < N; i++) {
               for (int k = 0; k < N; k++) {
                   // we can fly from city k to i
                   if ((k == i || flights[k][i] == 1) && canReach[k][j - 1]) {
                       
                       dp[i][j] = Math.max(dp[i][j], dp[k][j - 1] + days[i][j]);
                       canReach[i][j] = true;
                   } 
               }
           }            
       }
       for (int i = 0; i < N; i++) {
           res = Math.max(res, dp[i][K - 1]);
       }
       return res;
   }

688. Knight Probability in Chessboard

给一个N*N的棋盘,走的步数K,开始位置x,y,求K步之后骑士仍在棋盘里的概率。

棋盘上的动态规划,三维的动态规划 k,x,y,dp数组表示,K步之后骑士有几种走法能停在x,y点。最后加起来除以8的k次方得到概率。递推公式是dp[k][i][j] += dp[k-1][x][y],从x,ymove到i,j。
K从0开始想就能想通了。k如果等于0,在原地不动,概率100%,k如果等于1,有八种走法,算出有几种能停在棋盘里。K等于2时。。。

    public double knightProbability(int N, int K, int r, int c) {
        int[] directionX = {1, 2, -1, -2, 1, 2, -1, -2};
        int[] directionY = {2, 1, -2, -1, -2, -1, 2, 1};

        // dp[k][i][j] = sum(dp[k-1][i][j])
        double[][] dp = new double[N][N]; 
        // initialize all 0
        dp[r][c] = 1;
        for (int k = 1; k <= K; k++) {
            double[][] temp_dp = new double[N][N];
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    // i, j can be reached by 8 positions on the board
                    for (int p = 0; p < 8; p++) {
                        if (i + directionX[p] >= 0 && i + directionX[p] < N 
                            && j + directionY[p] >= 0 && j + directionY[p] < N) {
                            temp_dp[i][j] += dp[i + directionX[p]][j + directionY[p]];
                        }
                    }
                }
            }
            dp = temp_dp;
        }
        double total = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                total += dp[i][j];
            }
        }
        return total / Math.pow(8, K);
    }
  • DFS + Memory Search

44,72很像,递归的定义和与前一步的关系(三种情况分类讨论取最值)

44. Wildcard Matching

isMatch("ab", "?*") → true
isMatch("aab", "c * a * b") → false
?代表单个任意字符, * 代表空或单个或多个任意字符,与前一个字符无关
public boolean isMatch(String s, String p) {

二维DP, i表示s的0-i子串,j同理,dp[i][j]表示这两个子串是否匹配。
自顶向下的DP,根据DP数组的定义可知。
初始化难想的是如果p以一个或多个开头,那么需要把他们都标记为true(默认false)
递推公式:
如果p扫到一个字符或者“ ?”,处理比较简单。
如果扫到“ * “是难点所在因为情况比较复杂,跟518很像,每种情况单独讨论,有任何一种成立,那么就是true。
1 如果把 * 当空,那么比p.substring(0, j-1)和s.substring(0,i)。
2 把 * 当一个或多个任意字符,考虑caba, c

c和c* 匹配,。。。,cab和c匹配,那么caba就和c匹配,所以知道dp[i][j] = dp[i-1][j]

    public boolean isMatch(String s, String p) {
        if (s == null || p == null) {
            return false;
        }
        int m = s.length(), n = p.length();
        boolean[][] dp = new boolean[m + 1][ n + 1];
        // 1 initialize
        dp[0][0] = true;
        // "**ho"
        for (int j = 1; j < n + 1; j++) {
            if (p.charAt(j - 1) == '*') {
                dp[0][j] = true;
            } else {
                break;
            }
        }

        for (int i = 1; i < m + 1; i++) {
            for (int j = 1; j < n + 1; j++) {
                if (p.charAt(j - 1) != '*') {
                    // 最后一个字符相等,且前面的是匹配的
                    dp[i][j] = (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '?') && dp[i - 1][j - 1];
                } else {
                    // 把 * 当成空处理 || 当成 一个或多个任意字符处理
                    // 1. 匹配0个字符:dp[i][j] = dp[i][j-1]
                    // 2. 匹配1个字符:dp[i][j] = dp[i-1][j-1]
                    // 3. 匹配多个字符:dp[i][j] = dp[i-1][j]
                    dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
                }
            }
        }
        return dp[m][n];
    }

72. Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character

https://leetcode.com/problems/edit-distance/discuss/25911
add和delete对称,replace是各退一步
注意注释里提的,Math.min只能比两个

    public int minDistance(String word1, String word2) {
        int m = word1.length();
        int n = word2.length();
        int[][] dp = new int[m + 1][n + 1];
        // dp[i][j] -> substring(0, i) substring(0, j)
        // initialize
        for (int i = 0; i <= n; i++) {
            dp[0][i] = i;
        }
        for (int i = 0; i <= m; i++) {
            dp[i][0] = i;
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    // min 只能比两个
                    // add (append) 对称情况 or delete i or replace i by j
                    dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
                }
            }
        }
        return dp[m][n];
    }

714. Best Time to Buy and Sell Stock with Transaction Fee

198. House Robber

这两道题比较像,基础的DP,都是有两种状态
第i 天买的话最高利润,第i天卖的话最高利润,do nothing已经包含在两个dp数组中了。https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108871
第i个屋子偷的话最多挣多少钱,第i个屋子不偷的话最多挣多少钱
都是用两个一维数组记录。(都有follow-up,有时间做一下)

    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int[] steal = new int[nums.length + 1];
        int[] notSteal = new int[nums.length + 1];
        // initialize 0, 0
        for (int i = 1; i <= nums.length; i++) {
            steal[i] = nums[i - 1] + notSteal[i - 1];
            notSteal[i] = Math.max(notSteal[i - 1], steal[i - 1]);
        }
        return Math.max(steal[nums.length], notSteal[nums.length]);
    }
    public long houseRobber(int[] A) {
        // write your code here
        int n = A.length;
        if(n == 0)
            return 0;
        long []res = new long[n+1];
        
        
        res[0] = 0;
        res[1] = A[0];
        for(int i = 2; i <= n; i++) {
            res[i] = Math.max(res[i-1], res[i-2] + A[i-1]);
        }
        return res[n];
    }
    public int maxProfit(int[] prices, int fee) {
        // pay fee when buying
        int days = prices.length;
        int[] buy = new int[days]; // we buy at day i
        int[] sell = new int[days]; // we sell at day i
        buy[0] = -prices[0] - fee; // sell[0] = 0
        for (int i = 1; i < days; i++) {
            // buy or not
            buy[i] = Math.max(sell[i - 1] - prices[i] - fee, buy[i - 1]);
            // sell or not
            sell[i] = Math.max(buy[i - 1] + prices[i], sell[i - 1]);
        }
        return sell[days - 1];
    }

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
For example, given the following matrix:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.

矩阵上的DP

516. Longest Palindromic Subsequence 补DP做法

subsequence和substring不一样 比如 bbbab 最长的sub sequency是bbbb,所以返回4

memo search比较好理解,helper函数传入头尾指针,如果头尾的字符相同,那么该字符串的最长回文长度就是2+中间部分的最长回文长度,进行递归。如果头尾的字符不同,那么头指针+1尾指针不动,或尾-1头不动。查过的substring用memo记录。helper函数传入头尾指针和memo二维数组。memo[i][j]表示以i, j分割出的substring的最长回文长度
Palindromic常用套路dp[i][j],指定起点终点,往中间找
Palindromic还有一种方法叫做extend,定义中心pivot然后向两边扩展,pivot可以是a,也可以是aa这种

class Solution {
    public int longestPalindromeSubseq(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        return helper(s, 0, s.length() - 1, new Integer[s.length()][s.length()]);
    }
    public int helper(String s, int start, int end, Integer[][] memo) {
        if (memo[start][end] != null) {
            return memo[start][end];
        }
        if (start > end) {
            return 0;
        }
        if (start == end) {
            return 1;
        }
        if (s.charAt(start) == s.charAt(end)) {
            memo[start][end] = 2 + helper(s, start + 1, end - 1, memo);
        } else {
            memo[start][end] = Math.max(helper(s, start + 1, end, memo), helper(s, start, end - 1, memo));
        }
        return memo[start][end];
    }
}

673. Number of Longest Increasing Subsequence

Input: [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
找到最长递增子序列的个数

思路来自于找最长连续递增子序列的长度,一个DP数组记录截止到当前位置最长的递增子序列长度,另一个DP数组记录到当前位置的子序列,有几种可能构造出最长长度。何时更新第二个DP数组是复杂的地方。
DP写法需要学习

  • DP数组在哪初始化
  • 全局变量在何处更新
class Solution {
    public int findNumberOfLIS(int[] nums) {
        int N = nums.length;
        if (N <= 1) return N;
        int[] lengths = new int[N]; //lengths[i] = length of longest ending in nums[i]
        int[] counts = new int[N]; //count[i] = number of longest ending in nums[i]
        Arrays.fill(counts, 1);

        for (int j = 0; j < N; ++j) {
            for (int i = 0; i < j; ++i) if (nums[i] < nums[j]) {
                if (lengths[i] >= lengths[j]) {
                    lengths[j] = lengths[i] + 1;
                    counts[j] = counts[i];
                } else if (lengths[i] + 1 == lengths[j]) {
                    counts[j] += counts[i];
                }
            }
        }

        int longest = 0, ans = 0;
        for (int length: lengths) {
            longest = Math.max(longest, length);
        }
        for (int i = 0; i < N; ++i) {
            if (lengths[i] == longest) {
                ans += counts[i];
            }
        }
        return ans;
    }
}

329. Longest Increasing Path in a Matrix

Given an integer matrix, find the length of the longest increasing path.

From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).

Example 1:

nums = [
[9,9,4],
[6,6,8],
[2,1,1]
]
Return 4
The longest increasing path is [1, 2, 6, 9].

DP做不了的原因

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,377评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,390评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,967评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,344评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,441评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,492评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,497评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,274评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,732评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,008评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,184评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,837评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,520评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,156评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,407评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,056评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,074评论 2 352

推荐阅读更多精彩内容