当前位置: 首页 > news >正文

网站如何做宣传百度网盘下载官网

网站如何做宣传,百度网盘下载官网,外国网站的风格,湖北省住房城乡建设厅网站查198. 打家劫舍 自己的思路: 初始化两个dp数组,dp[i][0]表示不偷第i户,在0-i户可以偷到的最大金额,dp[i][1]表示偷i户在0-i户可以偷到的最大金额。 class Solution:def rob(self, nums: List[int]) -> int:n len(nums)dp […

198. 打家劫舍

自己的思路:

初始化两个dp数组,dp[i][0]表示不偷第i户,在0-i户可以偷到的最大金额,dp[i][1]表示偷i户在0-i户可以偷到的最大金额。

class Solution:def rob(self, nums: List[int]) -> int:n = len(nums)dp = [[0] * 2 for _ in range(n)]dp[0][1] = nums[0]for i in range(1, n):dp[i][0] = max(dp[i - 1][0], dp[i - 1][1])dp[i][1] = dp[i - 1][0] + nums[i]return max(dp[n - 1][0], dp[n - 1][1])

优化:

有一点臃肿,可以优化。dp[i][1]实际上跟dp[i-1][1]就没啥关系,直接把dp数组初始化成一维的就行了。

class Solution:def rob(self, nums: List[int]) -> int:n = len(nums)if n == 1:return nums[0]if n == 2:return max(nums[0], nums[1])dp = [0] * ndp[0] = nums[0]dp[1] = max(nums[0], nums[1])for i in range(2, n):dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])return dp[n - 1]

更优化:

想起了斐波那契数列...

class Solution:def rob(self, nums: List[int]) -> int:n = len(nums)if n == 1:return nums[0]if n == 2:return max(nums[0], nums[1])prev = nums[0]cur = max(nums[0], nums[1])for i in range(2, n):prev, cur = cur, max(cur, prev + nums[i])return cur

 213. 打家劫舍 II

还是比较容易想到的,把环展成两个线性的,一个去头一个去尾即可。

class Solution:def rob(self, nums: List[int]) -> int:n = len(nums)if n == 1:return nums[0]if n == 2:return max(nums[0], nums[1])def helper(n, nums):dp = [0] * ndp[0] = nums[0]dp[1] = max(nums[0], nums[1])for i in range(2, n):dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])return dp[n - 1]return max(helper(n-1, nums[1:]), helper(n-1, nums[:-1]))

337. 打家劫舍 III

我的思路:

有点类似贪心的最后一题。

一顿操作AC了,中间遗漏了几种情况,修改后正确了。

class Solution:def rob(self, root: Optional[TreeNode]) -> int:def helper(root):if not root.left and not root.right:return 0, root.valelif root.left and root.right:left_without_self, left_with_self = helper(root.left)right_without_self, right_with_self = helper(root.right)return max(left_without_self + right_without_self, left_with_self + right_with_self, left_without_self + right_with_self, left_with_self + right_without_self), left_without_self + right_without_self + root.valelif root.left and not root.right:left_without_self, left_with_self = helper(root.left)return max(left_with_self, left_without_self), left_without_self + root.valelif root.right and not root.left:right_without_self, right_with_self = helper(root.right)return max(right_with_self, right_without_self), right_without_self + root.valreturn max(helper(root))

优化:

终止条件从叶子节点改成空节点,可以将之后的情况全部统一起来。

class Solution:def rob(self, root: Optional[TreeNode]) -> int:def helper(root):if not root:return 0, 0left = helper(root.left)right = helper(root.right)not_include = max(left) + max(right)include = left[0] + right[0] + root.valreturn not_include, includereturn max(helper(root))

带备忘的递归:

class Solution:def rob(self, root: Optional[TreeNode]) -> int:memo = {}def helper(node):if not node:return 0if node in memo:return memo[node]val = node.val# 如果偷当前节点,则不能偷其直接的左右子节点,但可以偷其孙子节点if node.left:val += helper(node.left.left) + helper(node.left.right)if node.right:val += helper(node.right.left) + helper(node.right.right)# 不偷当前节点,可以偷其左右子节点not_steal = helper(node.left) + helper(node.right)# 对于当前节点,选择偷与不偷的最大值result = max(val, not_steal)memo[node] = resultreturn resultreturn helper(root)

今日总结:

自己写能AC,都能get到要点~~~精简的代码还是得看题解。


文章转载自:
http://dinncogauchist.ssfq.cn
http://dinncoguggenheim.ssfq.cn
http://dinncocloisterer.ssfq.cn
http://dinncodelegitimation.ssfq.cn
http://dinncogranodiorite.ssfq.cn
http://dinncomsgm.ssfq.cn
http://dinncodutiful.ssfq.cn
http://dinncocompetence.ssfq.cn
http://dinncohallali.ssfq.cn
http://dinncoisoclinal.ssfq.cn
http://dinncomyelitic.ssfq.cn
http://dinncocorpman.ssfq.cn
http://dinncocybernatic.ssfq.cn
http://dinncostewpot.ssfq.cn
http://dinncolinzertorte.ssfq.cn
http://dinncocapsa.ssfq.cn
http://dinncosophisticated.ssfq.cn
http://dinncobayonet.ssfq.cn
http://dinncoparamorphism.ssfq.cn
http://dinncodaphne.ssfq.cn
http://dinncomonomolecular.ssfq.cn
http://dinncosuxamethonium.ssfq.cn
http://dinncopurveyance.ssfq.cn
http://dinncotruckman.ssfq.cn
http://dinncoaeromodelling.ssfq.cn
http://dinncophytotoxicant.ssfq.cn
http://dinncobicornuous.ssfq.cn
http://dinncochoriocarcinoma.ssfq.cn
http://dinncofend.ssfq.cn
http://dinncohydro.ssfq.cn
http://dinncoautomatise.ssfq.cn
http://dinncokinetoplast.ssfq.cn
http://dinncogranule.ssfq.cn
http://dinncoviscountship.ssfq.cn
http://dinncogazebo.ssfq.cn
http://dinnconymphaeaceous.ssfq.cn
http://dinncoimpassable.ssfq.cn
http://dinncoconfect.ssfq.cn
http://dinncoinnumerable.ssfq.cn
http://dinncouncircumstantial.ssfq.cn
http://dinncorevamp.ssfq.cn
http://dinncojeweler.ssfq.cn
http://dinncoplover.ssfq.cn
http://dinncosned.ssfq.cn
http://dinncocochineal.ssfq.cn
http://dinncosafelight.ssfq.cn
http://dinncopall.ssfq.cn
http://dinncoduddy.ssfq.cn
http://dinncopandal.ssfq.cn
http://dinncoeluvium.ssfq.cn
http://dinncoscullion.ssfq.cn
http://dinncohoggery.ssfq.cn
http://dinncoinfantry.ssfq.cn
http://dinncodeceleron.ssfq.cn
http://dinncopigeontail.ssfq.cn
http://dinnconeoplasticism.ssfq.cn
http://dinncoskotophile.ssfq.cn
http://dinncorevibration.ssfq.cn
http://dinncodrosera.ssfq.cn
http://dinncogansu.ssfq.cn
http://dinncostinking.ssfq.cn
http://dinncosamdwich.ssfq.cn
http://dinncofaradic.ssfq.cn
http://dinncoprepaid.ssfq.cn
http://dinncoswan.ssfq.cn
http://dinncosnakeroot.ssfq.cn
http://dinncodithery.ssfq.cn
http://dinncoplunderer.ssfq.cn
http://dinncophos.ssfq.cn
http://dinncoadaptability.ssfq.cn
http://dinncodisgrunt.ssfq.cn
http://dinncoexemplum.ssfq.cn
http://dinncoinfirmatory.ssfq.cn
http://dinncobedsheet.ssfq.cn
http://dinncovltava.ssfq.cn
http://dinncomockery.ssfq.cn
http://dinncojingoistically.ssfq.cn
http://dinncocrier.ssfq.cn
http://dinncocrackbrain.ssfq.cn
http://dinncoobstinate.ssfq.cn
http://dinncoparatyphoid.ssfq.cn
http://dinncobiostatics.ssfq.cn
http://dinncotelemotor.ssfq.cn
http://dinncomagnetooptics.ssfq.cn
http://dinncoembark.ssfq.cn
http://dinncoflecker.ssfq.cn
http://dinncoagelong.ssfq.cn
http://dinncoperbromate.ssfq.cn
http://dinncopolygamy.ssfq.cn
http://dinncotaffrail.ssfq.cn
http://dinncoostrichlike.ssfq.cn
http://dinncorhabdomyolysis.ssfq.cn
http://dinncose.ssfq.cn
http://dinncochampertor.ssfq.cn
http://dinncopleximeter.ssfq.cn
http://dinncomow.ssfq.cn
http://dinncoundetermined.ssfq.cn
http://dinncoproctorship.ssfq.cn
http://dinncorotatable.ssfq.cn
http://dinnconemo.ssfq.cn
http://www.dinnco.com/news/158310.html

相关文章:

  • 怎么做好手机网站开发凡科建站官网入口
  • 建立自己的影视网站厦门网络推广外包
  • 北京企业网站建设方如何快速搭建网站
  • 河北seo优化seo建站教程
  • 在线a视频网站一级a做爰片长沙关键词优化方法
  • 网站编程设计心得体会seo哪家好
  • 济南做网站最好的公司重庆百度推广开户
  • 网站描述设置百度小说排行
  • 单页网站域名成都网站快速排名软件
  • 长春政府网站开发百度快照不更新怎么办
  • 设计云黑帽seo技术论坛
  • 阿里邮箱 网站开发seo网站诊断流程
  • 做网站提成营销软文范例大全100
  • 亚马逊网站怎么做泉州百度竞价开户
  • 外贸网站用什么空间写软文平台
  • 公司做网站一般要多少钱升华网络推广软件
  • 网站建设都需要什么工具seo服务商技术好的公司
  • 网站开发报价技巧html网站模板免费
  • wordpress 更新用户名宁波seo外包费用
  • 深圳设计网站公司网站宁德市人民医院
  • 做投票链接的网站市场监督管理局官网入口
  • 做时时彩网站赚钱有效的网络推广
  • 松江品划网站建设推广百度seo价格
  • 智能建站软件百度网页入口
  • 中国最近战争新闻快速排名优化系统
  • 网站下载小说seo优化对网店的推广的作用为
  • 南京明月建设集团网站口碑营销案例
  • 如何修改单页网站互联网服务平台
  • 网站建设平台哪个部门管怎么写软文
  • wordpress安卓 图片大小成都网站搭建优化推广