{"id":9125,"date":"2021-12-11T15:51:27","date_gmt":"2021-12-11T23:51:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9125"},"modified":"2021-12-11T17:12:09","modified_gmt":"2021-12-12T01:12:09","slug":"leetcode-ultimate-dp-study-plan-day-10","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-ultimate-dp-study-plan-day-10\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode Ultimate DP Study Plan Day 10"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/leetcode.com\/problems\/arithmetic-slices\/\"><a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/leetcode.com\/problems\/arithmetic-slices\/\"><strong>413. Arithmetic Slices<\/strong><\/a><\/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)\n# Space complexity: O(n)\nclass Solution:\n  def numberOfArithmeticSlices(self, nums: List[int]) -> int:\n    \n    @cache\n    def dp(i: int) -> int:\n      \"\"\"Number of arithmetic subarrays end with nums[i].\"\"\"\n      if i < 2: return 0\n      if nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]:\n        return 1 + dp(i - 1)\n      return 0\n    \n    return sum(dp(i) for i in range(len(nums)))\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/leetcode.com\/problems\/decode-ways\/\"><strong>91. Decode Ways<\/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) -> O(n)\n# Space complexity: O(n)\nclass Solution:\n  @cache\n  def numDecodings(self, s: str) -> int:\n    if not s: return 1\n    if s[0] == '0': return 0    \n    ans = self.numDecodings(s[1:])\n    if len(s) > 1 and (s[0] == '1' or (s[0] == '2' and s[1] <= '6')):\n      ans += self.numDecodings(s[2:])\n    return ans\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>413. Arithmetic Slices # Author: Huahua # Time complexity: O(n) # Space complexity: O(n) class Solution: def numberOfArithmeticSlices(self, nums: List[int]) -> int: @cache def dp(i:&#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-9125","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\/9125","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=9125"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9125\/revisions"}],"predecessor-version":[{"id":9128,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9125\/revisions\/9128"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}