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

[动态规划] 求解Pramp上一道类似edit distance的DP题

全局:

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

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

x
在Pramp上做了一道和edit distance类似的题。后面我写了我的javascript代码,但是有些edge case就是过不去。
有没有哪个大神可以指点一下这个题怎么做的

Pramp 链接: https://www.pramp.com/challenge/5j2xWAx1qPtlZGLnG2O0
题目:
Diff Between Two Strings
Given two strings of uppercase letters source and target, list (in string form) a sequence of edits to convert from source to target that uses the least edits possible.

For example, with strings source = "ABCDEFG", and target = "ABDFFGH" we might return: ["A", "B", "-C", "D", "-E", "F", "+F", "G", "+H"

More formally, for each character C in source, we will either write the token C, which does not count as an edit; or write the token -C, which counts as an edit.

Additionally, between any token that we write, we may write +D where D is any letter, which counts as an edit.

At the end, when reading the tokens from left to right, and not including tokens prefixed with a minus-sign, the letters should spell out target (when ignoring plus-signs.)

In the example, the answer of A B -C D -E F +F G +H has total number of edits 4 (the minimum possible), and ignoring subtraction-tokens, spells out A, B, D, F, +F, G, +H which represents the string target.

If there are multiple answers, use the answer that favors removing from the source first.

Constraints:

[time limit] 5000ms
[input] string source
2 ≤ source.length ≤ 12
[input] string target
2 ≤ target.length ≤ 12
[output] array.string

我的代码:

  1. function diffBetweenTwoStrings(source, target) {
  2.   const dp = [...Array(target.length + 1)].map(row => Array(source.length + 1).fill(0))

  3.   for(let i = 0; i <= target.length; i++) {
  4.     dp[i][0] = i
  5.   }

  6.   for(let j = 0; j <= source.length; j++) {
  7.     dp[0][j] = j
  8.   }

  9.   for(let j = 1; j <= source.length; j++) {
  10.     for(let i = 1; i <= target.length; i++) {
  11.       const row = i - 1
  12.       const col = j - 1
  13.       if(target[row] === source[col]) {
  14.         dp[i][j] = dp[i-1][j-1] + 1
  15.       } else {
  16.         dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + 1
  17.       }
  18.     }
  19.   }

  20.   let ans = []
  21.   let i = target.length
  22.   let j = source.length

  23.   while(i > 0 && j > 0) {
  24.     if(target[i - 1] !== source[j - 1]) {
  25.       if(dp[i - 1][j] <= dp[i][j-1]) {
  26.         ans.unshift(`+${target[i - 1]}`)
  27.         i--
  28.       } else {
  29.         ans.unshift(`-${source[j - 1]}`)
  30.         j--
  31.       }
  32.     } else {
  33.       ans.unshift(target[i-1])
  34.       i--
  35.       j--
  36.     }

  37.   }

  38.   while(j > 0) {
  39.     ans.unshift(`-${source[j-1]}`)
  40.     j--
  41.   }

  42.   while(i > 0) {
  43.     ans.unshift(`+${target[i-1]}`)
  44.     i--
  45.   }

  46.   for(let idx = 0; idx < ans.length - 1; idx++) {
  47.     if(ans[idx] === '+' + ans[idx + 1]) {
  48.       let temp = ans[idx]
  49.       ans[idx] = ans[idx + 1]
  50.       ans[idx + 1] = temp
  51.     }
  52.   }
  53.   return ans
  54. }
  55. console.log(diffBetweenTwoStrings("CBBC", "CABAABBC"))
复制代码


最后这个就是没过去的case,diffBetweenTwoStrings("CBBC", "CABAABBC")

标准答案是 ["C","+A","B","+A","+A","B","+B","C"]
我的答案是 ["C","'+A","+B","+A","+A","B","B","C"]


上一篇:关于Leetcode会员购买和自动续费
下一篇:找实习为目标,刷题重点应该放在什么上面呢?
🔗
cjhcjj 2020-10-24 15:15:44 | 只看该作者
全局:
虽然我看不懂JS,但17行不应该是dp[i][j] = dp[i-1][j-1]嘛
回复

使用道具 举报

🔗
cozec2013 2020-10-27 08:06:02 | 只看该作者
全局:
line 17:  dp[i][j] = dp[i-1][j-1]
line 19: dp[i][j] = Math.min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1
回复

使用道具 举报

🔗
usr_opta 2020-10-28 08:52:16 | 只看该作者
全局:
这题就是 LCS,比方说 "ACC" 要变成 "ACB"

  1. Ø A C B

  2. A 1←1←1
  3.   ↑↖
  4. C 1 2←2
  5.   ↑↖↑ ↑
  6. C 1 2←2
复制代码


右下角到右上角一共3条路,对应着三种合法的序列
A C +B -C
A C -C +B
A -C C B

看题目要求应该是要尽量先往左走。
回复

使用道具 举报

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

本版积分规则

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