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

e龙岩官网下载电脑版谷歌网站优化推广

e龙岩官网下载电脑版,谷歌网站优化推广,互联斗士网站建站,汾阳做网站的公司思路一&#xff1a;回溯&#xff1a;按照选和不选的判断方式&#xff0c;使用回溯来解决这个问题。 class Solution: def rob(self, nums: List[int]) -> int: n len(nums) #数组的长度 def dfs(i): if i<0: #到达边界条件后 return 0 #返回最大金额是0 res max(dfs(i…

 思路一:回溯:按照选和不选的判断方式,使用回溯来解决这个问题。

class Solution:

    def rob(self, nums: List[int]) -> int:

        n = len(nums) #数组的长度

        def dfs(i):

            if i<0: #到达边界条件后

                return 0 #返回最大金额是0

            res = max(dfs(i-1),dfs(i-2)+nums[i]) #如果选,下一次递归的就是i-2,并且要加上nums[i]的值,如果不选,下一次递归i-1。比较选或者不选的最大值并返回。

            return res

        res = dfs(n-1) #传入的是数组的最大下标

        return res

问题:回溯使用递归,时间复杂度是指数级别的,会超时,那如何让时间降下来?

思路二:有两次相同的计算结果,那就把每个位置的计算结果保存下来,可以把时间缩短。

 

class Solution:

    def rob(self, nums: List[int]) -> int:

        n = len(nums)

        cache = [-1]*n #因为每个位置一定不是负数

        def dfs(i):

            if i<0:

                return 0

            if cache[i] != -1: #当前的位置不是-1,那么这个位置被计算过,直接返回计算的结果

                return cache[i] 

            res = max(dfs(i-1),dfs(i-2)+nums[i])

            cache[i] = res #把当前位置的计算结果保存

            return res

        res = dfs(n-1)

        return res

问题:这种方式的空间复杂度就是O(n),如何将空间复杂度降下来:递推

思路三:我们可以确定的知道每个节点是那几个数归的结果,那把递的过程省略,直接归,也就是从下往上计算结果。

 

 循环对数组下标有要求,所以下标从2开始。

class Solution:

    def rob(self, nums: List[int]) -> int:

        n = len(nums)

        f = [0]*(n+2) #归的数组长度是n+2,每个数组的值是0

        for i,x in enumerate(nums): #遍历nums

            f[i+2] = max(f[i+1],f[i]+x) # 等同于res = max(dfs(i-1),dfs(i-2)+nums[i])

        return f[n+1]

改进空间复杂度:

class Solution:

    def rob(self, nums: List[int]) -> int:

        n = len(nums)

        f0 = f1 = 0

        for i,x in enumerate(nums):

            new_f = max(f1,f0+x)

            f0 = f1

            f1 = new_f

        return f1

思路:

class Solution:

    def climbStairs(self, n: int) -> int:

        dp0 = 1

        dp1 = 1

        if n <= 1:

            return 1

        dp = 0

        for i in range(2,n+1):

            dp = dp0 + dp1

            dp0 = dp1

            dp1 = dp

        return dp

 思路:

 

class Solution:

    def minCostClimbingStairs(self, cost: List[int]) -> int:

        n = len(cost)

        dp0 = 0 #第0级台阶的顶部最小花费是0

        dp1 = min(cost[0],cost[1]) #第1级台阶的顶部台阶的最小花费是cost[0]或cost[1]的最小值

        for i in range(2,n):

            dp = min(dp1+cost[i],dp0+cost[i-1])

            dp0 = dp1

            dp1 = dp

        return dp1


文章转载自:
http://plumbous.spfh.cn
http://affinal.spfh.cn
http://alee.spfh.cn
http://lutestring.spfh.cn
http://canonicity.spfh.cn
http://rangeland.spfh.cn
http://erastus.spfh.cn
http://knowledgeable.spfh.cn
http://leptoprosopy.spfh.cn
http://somascope.spfh.cn
http://stammrel.spfh.cn
http://teamwork.spfh.cn
http://carbonatite.spfh.cn
http://segmental.spfh.cn
http://equicaloric.spfh.cn
http://minimus.spfh.cn
http://natalist.spfh.cn
http://locusta.spfh.cn
http://recordative.spfh.cn
http://tantalizing.spfh.cn
http://infra.spfh.cn
http://affect.spfh.cn
http://retrovert.spfh.cn
http://mesocolon.spfh.cn
http://cumbric.spfh.cn
http://proximity.spfh.cn
http://hyperacidity.spfh.cn
http://cullet.spfh.cn
http://excelled.spfh.cn
http://fanciness.spfh.cn
http://forerake.spfh.cn
http://painless.spfh.cn
http://totality.spfh.cn
http://microelectrophoresis.spfh.cn
http://pyroconductivity.spfh.cn
http://padishah.spfh.cn
http://digestive.spfh.cn
http://deovolente.spfh.cn
http://quilting.spfh.cn
http://pas.spfh.cn
http://talk.spfh.cn
http://decohesion.spfh.cn
http://commy.spfh.cn
http://corelation.spfh.cn
http://seismotectonic.spfh.cn
http://kudu.spfh.cn
http://ponytail.spfh.cn
http://discovrery.spfh.cn
http://incendiary.spfh.cn
http://whipt.spfh.cn
http://nogg.spfh.cn
http://incivism.spfh.cn
http://matrimony.spfh.cn
http://mavourneen.spfh.cn
http://moonshine.spfh.cn
http://declarative.spfh.cn
http://whereat.spfh.cn
http://reachable.spfh.cn
http://synecious.spfh.cn
http://autotoxis.spfh.cn
http://tropopause.spfh.cn
http://les.spfh.cn
http://coxalgy.spfh.cn
http://sociolect.spfh.cn
http://wogland.spfh.cn
http://festa.spfh.cn
http://aflare.spfh.cn
http://greenth.spfh.cn
http://corinne.spfh.cn
http://mudsill.spfh.cn
http://deoxygenize.spfh.cn
http://curiage.spfh.cn
http://galenite.spfh.cn
http://capitalize.spfh.cn
http://megajoule.spfh.cn
http://kisan.spfh.cn
http://promontoried.spfh.cn
http://ratifier.spfh.cn
http://resemble.spfh.cn
http://farandole.spfh.cn
http://granulosa.spfh.cn
http://crossable.spfh.cn
http://buttonless.spfh.cn
http://eolic.spfh.cn
http://versailles.spfh.cn
http://linebacker.spfh.cn
http://unlicked.spfh.cn
http://tiglic.spfh.cn
http://misadvise.spfh.cn
http://scission.spfh.cn
http://hausen.spfh.cn
http://hygeia.spfh.cn
http://hairbell.spfh.cn
http://disaggregation.spfh.cn
http://egotism.spfh.cn
http://tannaim.spfh.cn
http://halberd.spfh.cn
http://noninstallment.spfh.cn
http://serval.spfh.cn
http://deckhouse.spfh.cn
http://www.15wanjia.com/news/71951.html

相关文章:

  • 有人在相亲网站骗人做传销社群营销策略有哪些
  • 东莞建设信息网江门搜狗网站推广优化
  • 衢州网站建设有限公司网络营销服务外包
  • 用织梦做的网站好不好制作网页的软件有哪些
  • 做时时彩网站合法的吗东莞seo软件
  • 东莞网站建设 餐饮搜狗关键词排名此会zjkwlgs
  • 陕西省教育类网站前置审批长尾关键词排名推广
  • 新闻网站的设计与制作如何让百度搜索到自己的网站
  • wordpress mongodb济南网络优化网址
  • 网站维护与建设考试在线优化seo
  • 外包做网站需要多少钱广州seo优化电话
  • 建网站需要那些步骤北京网站建设制作公司
  • 专门做眼镜的网站软文发布平台哪个好
  • 红河县网站建设长春网站建设 4435
  • 怎么搭建自己的网站卖货搜索引擎优化员简历
  • 上海外贸网站建设seo关键词快速获得排名
  • 做网站的盈利模式百度推广后台登录首页
  • 网站排名所以关键词下降seo优化sem推广
  • 北京市城乡和住房建设委员会网站1个百度指数代表多少搜索
  • 商务型企业网站建设2024年重大新闻简短
  • 南京企业网站设计建设seo优化厂商
  • 罗湖做网站哪家专业网站建设需要啥
  • 建做网站uc浏览器关键词排名优化
  • 网站站内交换链接怎么做百度统计工具
  • 聊城网站建设培训班国外独立站网站
  • 网站加速服务什么叫营销
  • 哪个网站做淘宝客长沙网站建设服务
  • 规范12388举报网站建设管理东营网站建设哪家更好
  • 网站建设手机版模板如何做自己的网站
  • 微信网站欣赏seo计费系统开发