{"id":9106,"date":"2021-12-11T08:33:16","date_gmt":"2021-12-11T16:33:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9106"},"modified":"2021-12-11T08:37:59","modified_gmt":"2021-12-11T16:37:59","slug":"leetcode-ultimate-dp-study-plan-day-9","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-ultimate-dp-study-plan-day-9\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode Ultimate DP Study Plan Day 9"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/leetcode.com\/problems\/word-break\/\"><strong>139. Word Break<\/strong><\/a><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\n# Time complexity: O(n^2)\n# Space complexity: O(n^2)\nclass Solution:\n  def wordBreak(self, s: str, wordDict: List[str]) -> bool:\n    words = set(wordDict)\n    n = len(s)\n\n    @cache\n    def dp(s: str) -> bool:\n      \"\"\"Returns whether s is breakable.\"\"\"\n      if not s: return True\n      for l in range(1, min(n, 20) + 1):\n        if s[0:l] in words and dp(s[l:]): return True\n    \n    return dp(s)\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Optimization<\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\n# Time complexity: O(n*l^2)\n# Space complexity: O(n)\nclass Solution:\n  def wordBreak(self, s: str, wordDict: List[str]) -> bool:\n    words = set(wordDict)\n    n = len(s)\n    \n    @cache\n    def dp(i: int) -> bool:\n      \"\"\"Returns whether s[i:] is breakable.\"\"\"\n      if i >= n: return True\n      for l in range(1, min(n, 20) + 1):\n        if s[i:i+l] in words and dp(i + l): return True\n    \n    return dp(0)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>139. Word Break # Author: Huahua # Time complexity: O(n^2) # Space complexity: O(n^2) class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: words&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18],"class_list":["post-9106","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9106"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9106\/revisions"}],"predecessor-version":[{"id":9111,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9106\/revisions\/9111"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}