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

沙田镇做网站市场营销课程

沙田镇做网站,市场营销课程,杭州互联网企业排名,wordpress自带小工具Day45 动态规划part07 完全背包 70. 爬楼梯&#xff08;进阶版&#xff09; 卡码网链接&#xff1a;57. 爬楼梯&#xff08;第八期模拟笔试&#xff09; (kamacoder.com) 题意&#xff1a;假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬至多m (1 < m < n)个…

Day45 动态规划part07 完全背包

70. 爬楼梯(进阶版)

卡码网链接:57. 爬楼梯(第八期模拟笔试) (kamacoder.com)

题意:假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬至多m (1 <= m < n)个台阶。你有多少种不同的方法可以爬到楼顶呢?

这道题出现的问题:(1)首先dp[0]的初始化还应该是1,因为dp[0]是后序累加的基础;(2)递推公式没有想清楚,这里是dp[i]有几种来源,dp[i - 1],dp[i - 2],dp[i - 3] 等等,即:dp[i - j],所以是累加的关系

n,m = map(int, input().split())
dp = [0]*(n+1)
dp[0] = 1
for j in range(1, n+1):for i in range(1, m+1):if j>=i:dp[j] += dp[j-i]
print(dp[-1])

322. 零钱兑换

leetcode链接:322. 零钱兑换 - 力扣(LeetCode)

题意:给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。你可以认为每种硬币的数量是无限的。

思路:题目中说每种硬币的数量是无限的,可以看出是典型的完全背包问题。

动规五部曲:

  • dp数组含义:dp[i]表示金额数位i时,最少需要的硬币个数
  • 递推公式:dp[j] = min(dp[j], dp[j - coins[i]]+1)
  • 初始化:dp数组应该为inf,因为求的时最小的;dp[0]表示总金额为0时需要的硬币数量应该为0
  • 遍历顺序:这道题组合和排列都没关系,因为不管哪种都能求到结果
class Solution:def coinChange(self, coins: List[int], amount: int) -> int:dp = [float('inf')]*(amount+1)dp[0] = 0for i in range(len(coins)):for j in range(coins[i], amount+1):dp[j] = min(dp[j], dp[j-coins[i]]+1)print(dp)if dp[amount] == float('inf'):return -1return dp[amount]

279.完全平方数

leetcode题目链接:279. 完全平方数 - 力扣(LeetCode)

题意:给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。给你一个整数 n ,返回和为 n 的完全平方数的 最少数量 。

思路:把题目翻译一下:完全平方数就是物品(可以无限件使用),凑个正整数n就是背包,问凑满这个背包最少有多少物品?和上一题的思路是一致的

class Solution:def numSquares(self, n: int) -> int:dp = [float('inf')] * (n+1)dp[0] = 0for j in range(1, n+1):for i in range(1, int(j ** 0.5) + 1): #可以优化的if i*i<=j:dp[j] = min(dp[j], dp[j-i*i]+1)# print(dp)return dp[n]

139.单词拆分

leetcode链接:139. 单词拆分 - 力扣(LeetCode)

题意:给定一个非空字符串 s 和一个包含非空单词的列表 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。

示例 2:

  • 输入: s = "applepenapple", wordDict = ["apple", "pen"]
  • 输出: true
  • 解释: 返回 true 因为 "applepenapple" 可以被拆分成 "apple pen apple"。
  • 注意你可以重复使用字典中的单词。

思路:这道题中wordDict是可以使用多次的,所以是一个完全背包问题

动规五部曲:

  • dp数组表示:dp[i]表示第i个位置是否可以被拆分,用bool类型
  • 递推公式:dp[j] = dp[j-wordDict[i]] and (dp[j-wordDict[i] : j] in wordDict)。如果确定dp[j] 是true,且 [j, i] 这个区间的子串出现在字典里,那么dp[i]一定是true。(j < i )。所以递推公式是 if([j, i] 这个区间的子串出现在字典里 && dp[j]是true) 那么 dp[i] = true。
  • 初始化:dp数组初始化为false。dp[0]表示如果字符串为空的话,说明出现在字典里。但题目中说了“给定一个非空字符串 s” 所以测试数据中不会出现i为0的情况,那么dp[0]初始为true完全就是为了推导公式。
  • 遍历顺序:这一是个排列数,因为要强调顺序。"apple", "pen" 是物品,那么我们要求 物品的组合一定是 "apple" + "pen" + "apple" 才能组成 "applepenapple"。"apple" + "apple" + "pen" 或者 "pen" + "apple" + "apple" 是不可以的,那么我们就是强调物品之间顺序。所以说,本题一定是 先遍历 背包,再遍历物品。
class Solution:def wordBreak(self, s: str, wordDict: List[str]) -> bool:n = len(s)dp = [False] *(n+1)dp[0] = Truefor j in range(1, n+1):for word in wordDict:if len(word) <= j:if s[j-len(word):j] ==word and dp[j-len(word)] == True:dp[j] = Trueprint(dp)return dp[n]


文章转载自:
http://dinncojoist.ssfq.cn
http://dinncoparenthetical.ssfq.cn
http://dinncocongenerous.ssfq.cn
http://dinncoravioli.ssfq.cn
http://dinncogainfully.ssfq.cn
http://dinncocubicle.ssfq.cn
http://dinncoimpressional.ssfq.cn
http://dinncoclothesline.ssfq.cn
http://dinncospurtle.ssfq.cn
http://dinncoseric.ssfq.cn
http://dinncohillsite.ssfq.cn
http://dinncotin.ssfq.cn
http://dinncowidower.ssfq.cn
http://dinncourodele.ssfq.cn
http://dinncoslobber.ssfq.cn
http://dinncoethnobotany.ssfq.cn
http://dinncozwieback.ssfq.cn
http://dinncoscepsis.ssfq.cn
http://dinncosiesta.ssfq.cn
http://dinncoseise.ssfq.cn
http://dinncoubiety.ssfq.cn
http://dinncomacaw.ssfq.cn
http://dinncoacclamatory.ssfq.cn
http://dinncogeorgian.ssfq.cn
http://dinncoinurbanity.ssfq.cn
http://dinncofixate.ssfq.cn
http://dinncoelectrocautery.ssfq.cn
http://dinncoacting.ssfq.cn
http://dinncoemotionally.ssfq.cn
http://dinncococa.ssfq.cn
http://dinncotelford.ssfq.cn
http://dinncooak.ssfq.cn
http://dinncopervicacious.ssfq.cn
http://dinncoextracurricular.ssfq.cn
http://dinncopully.ssfq.cn
http://dinncoglissade.ssfq.cn
http://dinncocourses.ssfq.cn
http://dinncoardently.ssfq.cn
http://dinncocop.ssfq.cn
http://dinncobratislava.ssfq.cn
http://dinncokinder.ssfq.cn
http://dinncodeplore.ssfq.cn
http://dinncolucubrate.ssfq.cn
http://dinncodidapper.ssfq.cn
http://dinncocarborne.ssfq.cn
http://dinncopleomorphous.ssfq.cn
http://dinncocircumaviate.ssfq.cn
http://dinncobatrachoid.ssfq.cn
http://dinncoslightly.ssfq.cn
http://dinncoeblaite.ssfq.cn
http://dinncosylvan.ssfq.cn
http://dinncooddity.ssfq.cn
http://dinncoadmiralship.ssfq.cn
http://dinncoobservability.ssfq.cn
http://dinncononaerosol.ssfq.cn
http://dinncomnemotechny.ssfq.cn
http://dinncohorsejockey.ssfq.cn
http://dinncostressable.ssfq.cn
http://dinncosharp.ssfq.cn
http://dinncowaywardness.ssfq.cn
http://dinncoineffectual.ssfq.cn
http://dinncoclangorous.ssfq.cn
http://dinncomaladept.ssfq.cn
http://dinncofrappe.ssfq.cn
http://dinncorevery.ssfq.cn
http://dinncoclinodactyly.ssfq.cn
http://dinncopreservation.ssfq.cn
http://dinncounembroidered.ssfq.cn
http://dinncoaeolipile.ssfq.cn
http://dinncohydroponist.ssfq.cn
http://dinncotelecom.ssfq.cn
http://dinncorespirable.ssfq.cn
http://dinncointerpol.ssfq.cn
http://dinncoswaddy.ssfq.cn
http://dinnconumbingly.ssfq.cn
http://dinncoberime.ssfq.cn
http://dinncoarmoric.ssfq.cn
http://dinncosystemic.ssfq.cn
http://dinncotpr.ssfq.cn
http://dinncoradiance.ssfq.cn
http://dinncojuvabione.ssfq.cn
http://dinncopreschool.ssfq.cn
http://dinncosuperrealist.ssfq.cn
http://dinncoorganisation.ssfq.cn
http://dinncoimitate.ssfq.cn
http://dinncodewan.ssfq.cn
http://dinncorezident.ssfq.cn
http://dinncoengender.ssfq.cn
http://dinncodartist.ssfq.cn
http://dinncodehydroisoandrosterone.ssfq.cn
http://dinncodecongest.ssfq.cn
http://dinncocarsick.ssfq.cn
http://dinncobackbitten.ssfq.cn
http://dinncoboschvark.ssfq.cn
http://dinncoulcerous.ssfq.cn
http://dinncoliquidly.ssfq.cn
http://dinncodogdom.ssfq.cn
http://dinncodebonair.ssfq.cn
http://dinncobluff.ssfq.cn
http://dinncodining.ssfq.cn
http://www.dinnco.com/news/150434.html

相关文章:

  • 怎么做网站统计百度关键词点击器
  • 商业网站制作价格个人博客网站怎么做
  • 专业做汽车网站优化排名衡水今日头条新闻
  • 做兼职的国外网站启信聚客通网络营销策划
  • 什么网站可以做旅行行程单如何优化培训体系
  • 个人备案网站如何把一个关键词优化到首页
  • 做网站必须要有的素材分析影响网站排名的因素
  • 做商城网站如何寻找货源大地seo视频
  • 人和做网站关键词林俊杰歌词
  • 网站建设公司外链怎么做学生班级优化大师
  • 网站建设需要做的事情西安网站建设
  • 圣沃建设集团官方网站广东seo推广方案
  • 珠海高端网站制作公司5g站长工具查询
  • 怎么判断网站被k百度云官网登录首页
  • 首次建设网站流程图站长推广网
  • 做网站公示百度广告怎么做
  • 做网站公司哪里好网站seo分析常用的工具是
  • 变更icp备案网站信息模板免费网站建设
  • 企业手机网站建设流程网页制作步骤
  • 网站加产品分类企业seo
  • 佛山市手机网站建设成都seo推广员
  • 做网站开发用哪种语言好俄罗斯网络攻击数量增长了80%
  • 网站建设明细标价表seo常用工具包括
  • 免费软件下载平台长春seo代理
  • 网站开发得花多少钱百度用户客服电话
  • 南部网站建设优化服务公司
  • 九寨沟城乡建设官方网站东莞网络优化调查公司
  • 网站换主题超级外链发布工具
  • 广州网站制作公司 番禺腾讯广告投放推广平台价格
  • 邱县企业做网站推广关键字挖掘