首页 > 其他分享 >leetcode: TC of top-down memorization

leetcode: TC of top-down memorization

时间:2023-08-19 22:59:32浏览次数:28  
标签:top TC down start length str wordDict memorization memo

example to explain how to calculate Time Complexity

the memo size means each state will be calculated only once

how about the TC in each state? 

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        return dfs(s,wordDict,0,new Boolean[s.length()]);
    }
    // Given n as the length of s, m as the length of wordDict, and k as the average length of the words in wordDict,
    public boolean dfs(String s, List<String> wordDict,int start,Boolean[] memo) {
        if(start == s.length()){
            return true;
        }
        if(memo[start] != null) return memo[start]; // n states
        boolean res = false;
        for(String str: wordDict){ // to calculate each state, iterate m times
            if(start+str.length() > s.length()) continue;
            String t = s.substring(start,start+str.length()); // cost k times
            if(t.equals(str) && dfs(s,wordDict,start+str.length(),memo)){
               res = true;
            }
        }
        return memo[start] = res;
    }
}

so the TC is O(n * m * k)

标签:top,TC,down,start,length,str,wordDict,memorization,memo
From: https://www.cnblogs.com/sabertobih/p/17643343.html

相关文章

  • 测试Markdown插件
    目录一、标题11.1标题2三、标题3测试角标[^1][[md_note_test]]:内连文件在博客园不起作用。一、标题11.1标题2三、标题3待办原始图片修改大小后的图片,并将图片居中测试关联已经有的博文测试修改文件名后的效果......
  • Markdown
    Markdown一级标题二级标题三级标题四级标题字体helloworldhelloworldhelloworldhelloworldhelloworld引用选择狂神说java分割线图片超链接点击跳转到狂神博客列表列表1.+空格回车键ABC表格名字性别生日 张三男2004.1.1 ......
  • MarkDown学习
    MarkDown学习标题字体helloworldhelloworldhelloworldhelloworld引用这句话是引用分割线图片超链接点击跳转b站主页列表ABCABC表格名字性别生日GOG男2000.07.26代码publichello......
  • 【vscode】markdown preview mermaid 不显示
    需要安装插件markdownpreviewmermaidsupport插件,注意如果使用服务器,服务器也需要安装。远程服务安装插件可能速度较慢,我们可以下载vsix直接安装。通过vscodeplugins,搜索需要的插件,在version中选择合适的版本,下载并上传到远程服务器,最后通过vscode安装......
  • 【学校文章】FAQ--Markdown(2023-08-18更新)
    FAQ--Markdown本文章的访问次数为次。Part1提示欢迎大家指出错误并私信这个蒟蒻欢迎大家在下方评论区写出自己的疑问(记得@这个蒟蒻)本文已同步至学校网站、博客园。Part2背景本蒟蒻最近看了\(\operatorname{QOJ}\)中的FAQ,然后发现了一件很神奇的事:\(\operatornam......
  • linux系统性能监控top
    1、top命令用于监控系统的资源,包括内存、交换分区和CPU的使用率等。它会定期更新显示内容top-09:25:38up7days,19:27, 3users, loadaverage:0.15,0.08,0.02Tasks:187total, 1running,186sleeping, 0stopped, 0zombieCpu(s): 0.8%us, 1.6%sy,......
  • web 通用 request - download
    requestimportaxiosfrom'axios'import{MessageBox,Message}from'element-ui'importstorefrom'@/store'import{getToken,getzyToken}from'@/utils/auth'//createanaxiosinstanceconstservice=axios.c......
  • X710网卡LACP模式下ifdown网卡后交换机侧依然处于UP状态,导致网络通信异常
    以下配置属于临时配置,重启后失效,具体建议在bios或者固件中解决。主要包含两个配置:1、使用ifdown命令关闭网卡无法使linkdown,交换机侧依然认为端口UP进行流量转发,无法正常通信2、在某些环境中,LACP可能无法正常工作,这些环境要求将包含LCAP信息的LLDP帧转发到网络堆栈。#查看网卡......
  • cmd shutdown命令:关机,重启,休眠
    一段时间后关机:shutdown-s-t秒数效果是倒计时到该秒数后关机,例如shutdown-s-t3600就是3600秒后关机,也就是一小时后关机立即关机命令:shutdown-p关闭本地计算机,效果是马上关机,而不进行倒计时也可以使用shutdown-s-t0设置0秒后关机,也就是立即关机的意思。一段时间后重启s......
  • Qt for ARM_Linux环境搭建-Qt5.7+iTop4412嵌入式平台移植
    原文:https://blog.csdn.net/hechao3225/article/details/52981148经过为期3天的编译、移植,终于将Qt5.7成功移植到iTop4412开发板,板载exynos4412处理器,基于ARMCortex-A9内核。因此,本篇教程以iTop4412示例,适用于Qt5.7在ARM_Linux平台上的移植。---------------------------------......