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

[高频题] 140 word break ii time complexity

全局:
高频题
公司名称: 自己刷题

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

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

x
正在复习
https://leetcode.com/problems/word-break-ii/
对于时间复杂度的分析有些疑问
1.
我自己的解法, dp, dp[i] store all possible sentences to form str[0,i-1]


  1.     public List<String> wordBreak(String s, List<String> wordDict) {
  2.         List<String> [] dp = new ArrayList[s.length() + 1];
  3.         Set<String> dictSet = new HashSet<>(wordDict);
  4.         List<String> res = new ArrayList<>();
  5.         //dp[i] : all possbile sentences to form s[0,i-1];
  6.         for (int i = 0; i < dp.length; i++) {
  7.             dp[i] = new ArrayList<>();
  8.         }
  9.         dp[0].add("");
  10.         
  11.         for (int i = 1; i < dp.length; i++) {
  12.             for (int start = 0; start < i; start++) {
  13.                 //when index operation is meesy, conisder a 1-element or 2-element case to test
  14.                 String substr = s.substring(start, i);
  15.                 if (!dp[start].isEmpty() && wordDict.contains(substr)) {
  16.                     for (String prefix : dp[start]) {
  17.                         dp[i].add(prefix + " " + substr);
  18.                     }
  19.                 }
  20.             }
  21.         }
  22.         
  23.         for (String sentence : dp[dp.length - 1]) {
  24.             if (sentence.length() >= 1)
  25.                 res.add(sentence.substring(1));
  26.         }
  27.         
  28.         return res;
  29.     }
复制代码

自己能确定的是 first loop O(n) * substring cost O(n), but can't decide time cost of nested loop ( for (String prefix : dp[start])
各位大佬能帮着看下吗.
2. lc 上 最快解法


  1. public List<String> wordBreak(String s, List<String> wordDict) {
  2.   if (s.length() > 100) {
  3.             return new ArrayList();
  4.         }
  5.         
  6.   List<String> result = new ArrayList<String> ();
  7.   wordBreakUtil(s, wordDict, result, new StringBuilder());
  8.   return result;
  9. }
  10.    
  11.     public void wordBreakUtil(String s, List<String> wordDict, List<String> result, StringBuilder subList) {
  12.         // add " " between 2 words in subList
  13.   if (subList.length() != 0) {
  14.    subList.append(" ");
  15.   }
  16.   // iterate over all the words in wordDict
  17.         for (String word: wordDict) {
  18.    
  19.    if (s.startsWith(word)) {
  20.     StringBuilder sb = new StringBuilder(subList);
  21.        // append current match in sb
  22.     sb.append(word);
  23.        // if this is last word to be matched
  24.     if (s.equals(word)) {
  25.      result.add(new String(sb));
  26.     } else {
  27.      wordBreakUtil(s.substring(word.length()), wordDict, result, sb);
  28.     }
  29.             }
  30.   }
  31. }
复制代码
这个解法是在loop against wordDict, can perfect solve a very tricky test case on lc(which cause many other solution TLE)
我不是很确定这个解法的时间复杂度,分析是, O (len(s) ^ len(wordDict)). 理由是for each recursion node in recursion tree, loops by wordDict, so each recursion has len(wordDict)  branches, and whole recursion tree has height   len(s)

请大佬们帮忙看看

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

本版积分规则

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