查看: 1770| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[动态规划] dp problem

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Why in some dp problem, when we define the dp array, sometimes we define the length of array is (length + 1), but sometimes is (length).

Example such as Unique Paths (int[][] dp = new int[m][n];) and Edit distance(int[][] dp = new int[word1.length() + 1][word2.length() + 1];)

Any discussion is appreciated.
  1. public class Solution {
  2.     public int uniquePaths(int m, int n) {
  3.         if(m == 0 || n == 0)
  4.         {
  5.             return 0;
  6.         }
  7.         
  8.         if(m == 1 || n == 1)
  9.         {
  10.             return 1;
  11.         }
  12.         
  13.         int[][] dp = new int[m][n];
  14.         
  15.         for(int i = 0; i < m; i++)
  16.         {
  17.             dp[i][0] = 1;
  18.         }
  19.         
  20.         for(int j = 0; j < n; j++)
  21.         {
  22.             dp[0][j] = 1;
  23.         }
  24.         
  25.         for(int i = 1; i < m; i++)
  26.         {
  27.             for(int j = 1; j < n; j++)
  28.             {
  29.                 dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
  30.             }
  31.         }
  32.         return dp[m - 1][n - 1];
  33.     }
  34. }
复制代码
  1. public class Solution {
  2.     public int minDistance(String word1, String word2) {
  3.         int len1 = word1.length();
  4.         int len2 = word2.length();
  5.         
  6.         int[][] dp = new int[word1.length() + 1][word2.length() + 1];
  7.         
  8.         for(int i = 0; i <= word1.length(); i++)
  9.         {
  10.             dp[i][0] = i;
  11.         }
  12.         
  13.         for(int j = 0; j <= word2.length(); j++)
  14.         {
  15.             dp[0][j] = j;
  16.         }
  17.         
  18.         for(int i = 0; i < word1.length(); i++)
  19.         {
  20.             for(int j = 0; j < word2.length(); j++)
  21.             {
  22.                 char c1 = word1.charAt(i);
  23.                 char c2 = word2.charAt(j);
  24.                 if(c1 == c2)
  25.                 {
  26.                     dp[i + 1][j + 1] = dp[i][j];
  27.                 }
  28.                 else
  29.                 {
  30.                     dp[i + 1][j + 1] = Math.min(Math.min(dp[i][j + 1] + 1, dp[i + 1][j] + 1), dp[i][j] + 1);
  31.                 }
  32.             }
  33.         }
  34.         return dp[len1][len2];
  35.     }
  36. }
复制代码

上一篇:Top 10 Algorithms for Coding Interview (zz)
下一篇:First Missing Positive
🔗
muybienw 2015-8-9 20:36:04 | 只看该作者
全局:
个人理解,这个要看dp的东西是什么含义,unique paths里面dp[i][j]就是坐标 i, j这个位置的情况,length * length就已经覆盖所有情况了;而edit distance里面,i,j可以理解为两个字符串分别到 i, j 位置的匹配情况,额外加一行一列,正好处理了两个string是 "",即空字符串的情况。一般string的dp处理,都是用length + 1的。

点评

Thanks for sharing.  发表于 2015-8-10 00:39
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表