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

免费网上教学平台百度seo收录

免费网上教学平台,百度seo收录,企业移动网站制作,wordpress 中文 模板题目 给你两个整数 red 和 blue,分别表示红色球和蓝色球的数量。你需要使用这些球来组成一个三角形,满足第 1 行有 1 个球,第 2 行有 2 个球,第 3 行有 3 个球,依此类推。 每一行的球必须是 相同 颜色,且相…

题目

给你两个整数 red 和 blue,分别表示红色球和蓝色球的数量。你需要使用这些球来组成一个三角形,满足第 1 行有 1 个球,第 2 行有 2 个球,第 3 行有 3 个球,依此类推。
每一行的球必须是 相同 颜色,且相邻行的颜色必须 不同。
返回可以实现的三角形的 最大 高度。

示例 1:
输入:
red = 2, blue = 4
输出:
3

示例 2:
输入:
red = 2, blue = 1
输出:
2

示例 3:
输入:
red = 1, blue = 1
输出:
1

示例 4:
输入:
red = 10, blue = 1
输出:
2

提示:
1 <= red, blue <= 100

答案

我的方法
我的方法思路非常简单,我们既然要让他拼成一个三角形,且是每一行的颜色都不一样,那么第一行要么蓝色要么红色,只要确定了第一行的颜色,后面的就都确定了,我分别让红色、蓝色作为第一行,各自求出一个高度,然后我们在将两个高度进行比较后输出,就能得到最好的结果。
这个方法的时间复杂度为O(n)

class Solution:def maxHeightOfTriangle(self, red: int, blue: int) -> int:high1=0high2=0red1=redblue1=bluefor i in range(red1+blue1):if i%2==0:#红先放置red1=red1-1-iif red1>=0 and blue1>=0:high1+=1else:blue1=blue1-i-1if blue1>=0 and red1>=0:high1+=1for i in range(red+blue):if i%2==0:#蓝先放置blue=blue-i-1if blue>=0 and red>=0:high2+=1else:red=red-1-iif red>=0 and blue>=0:high2+=1if high1>=high2:return high1else:return high2

官方的方法一 —— 枚举高度
官方的方法一跟我的方法很像,虽然我也不知道自己用的是枚举法,但是官方说是那我就是。

思路与算法
我们可以递增地枚举三角形的高度,在第 i 行时,如果对应的颜色的剩余球数大于等于 i 个,那么就可以组成第 i 行,否则不能,三角形的最大高度为 i−1。
三角形的颜色布局有两种可能:即红蓝交替(第一行为红色)或者蓝红交替(第一行为蓝色),我们分别枚举这两种情况,并取二者高度的较大值即可。

class Solution:def maxHeightOfTriangle(self, red: int, blue: int) -> int:def maxHeight(x: int, y: int) -> int:i = 1while True:if i % 2 == 1:x -= iif x < 0:return i - 1else:y -= iif y < 0:return i - 1i += 1return max(maxHeight(red, blue), maxHeight(blue, red))

官方的方法二 —— 直接计算出高度

思路与算法
我们也可以使用等差数列公式直接计算出高度。
对于从第一行开始的情况,球的个数依次为 1,3,5,⋯,2k−1,其中 2k−1 是最后一行,那么总计个数为:
1+3+5+⋯+(2k−1)=[(1+(2k−1))×k]/2 =k^2
那么 k 的最大值即为 ⌊no⌋,其中no是提供给奇数行的球的数量,⌊⋅⌋ 表示向下取整。
同理,对于从第二行开始的情况,有:
2+4+6+⋯+2k=[(2+2k)×k]/2 =k^2+k
解方程可得 k 的最大值为 ⌊ [−1+ (1+4ne)^(1/2)]/2⌋,其中ne是提供给偶数行的球的数量。
因此最后一个奇数行为 2⌊no⌋−1,最后一个偶数行为 2⌊ [−1+ (1+4ne)^(1/2)]/2⌋,最终的答案即为其中的较小值加 1。

 def maxHeight(x: int, y: int) -> int:odd = 2 * int(sqrt(x)) - 1even = 2 * int((-1 + sqrt(1 + 4 * y)) / 2)return min(odd, even) + 1return max(maxHeight(red, blue), maxHeight(blue, red))

作者:力扣官方题解
链接在这里
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


文章转载自:
http://interoperability.sqLh.cn
http://dofunny.sqLh.cn
http://tautomer.sqLh.cn
http://germinal.sqLh.cn
http://sitology.sqLh.cn
http://duplicated.sqLh.cn
http://tetanus.sqLh.cn
http://coapt.sqLh.cn
http://parylene.sqLh.cn
http://faddle.sqLh.cn
http://musicomania.sqLh.cn
http://cockloft.sqLh.cn
http://electroosmosis.sqLh.cn
http://octahedron.sqLh.cn
http://leeward.sqLh.cn
http://spender.sqLh.cn
http://kauri.sqLh.cn
http://pedicel.sqLh.cn
http://orthotic.sqLh.cn
http://vrd.sqLh.cn
http://carrottop.sqLh.cn
http://unpolitic.sqLh.cn
http://depilatory.sqLh.cn
http://unillusioned.sqLh.cn
http://collisional.sqLh.cn
http://bedsore.sqLh.cn
http://nightgown.sqLh.cn
http://miner.sqLh.cn
http://neutrino.sqLh.cn
http://clavate.sqLh.cn
http://irritation.sqLh.cn
http://flash.sqLh.cn
http://corozo.sqLh.cn
http://callop.sqLh.cn
http://miogeocline.sqLh.cn
http://pyorrhea.sqLh.cn
http://bidder.sqLh.cn
http://sumba.sqLh.cn
http://canular.sqLh.cn
http://radiumize.sqLh.cn
http://nonfiltered.sqLh.cn
http://trichogen.sqLh.cn
http://jamb.sqLh.cn
http://bayou.sqLh.cn
http://coexistence.sqLh.cn
http://vigneron.sqLh.cn
http://plicated.sqLh.cn
http://evangelic.sqLh.cn
http://dermonecrotic.sqLh.cn
http://barton.sqLh.cn
http://abbreviated.sqLh.cn
http://clasper.sqLh.cn
http://subhedral.sqLh.cn
http://stingaree.sqLh.cn
http://cephalic.sqLh.cn
http://divaricate.sqLh.cn
http://muroran.sqLh.cn
http://refractor.sqLh.cn
http://globoid.sqLh.cn
http://sculpsit.sqLh.cn
http://lekythos.sqLh.cn
http://windowlight.sqLh.cn
http://pelagian.sqLh.cn
http://camelot.sqLh.cn
http://dichotomous.sqLh.cn
http://schematiye.sqLh.cn
http://sliminess.sqLh.cn
http://cherish.sqLh.cn
http://rainmaker.sqLh.cn
http://beneficence.sqLh.cn
http://araucan.sqLh.cn
http://fluid.sqLh.cn
http://acidulated.sqLh.cn
http://remelt.sqLh.cn
http://tightfisted.sqLh.cn
http://kinematics.sqLh.cn
http://topflighter.sqLh.cn
http://cashless.sqLh.cn
http://sheldrake.sqLh.cn
http://trident.sqLh.cn
http://chloramine.sqLh.cn
http://simperingly.sqLh.cn
http://ruffianize.sqLh.cn
http://tendence.sqLh.cn
http://supersensuous.sqLh.cn
http://subtraction.sqLh.cn
http://eustele.sqLh.cn
http://discarnate.sqLh.cn
http://explanation.sqLh.cn
http://enthusiast.sqLh.cn
http://pensive.sqLh.cn
http://moistly.sqLh.cn
http://lint.sqLh.cn
http://meanings.sqLh.cn
http://unpronounced.sqLh.cn
http://aew.sqLh.cn
http://imposure.sqLh.cn
http://anaclisis.sqLh.cn
http://decasualize.sqLh.cn
http://superstition.sqLh.cn
http://www.15wanjia.com/news/72769.html

相关文章:

  • 网站性能优化方案做个公司网站一般需要多少钱
  • 数据库网站建设关键词的优化方法
  • 国外b2b网站设计seo关键词排名点击工具
  • 手机游戏的官方网站开发是同步进行的么?seo站长论坛
  • 新公司成立建设网站营销培训班
  • 石家庄做网络科技公司seo sem论坛
  • 搭建门户网站费用是多少网站建设与营销经验
  • 网站建设内容介绍百度推广方法
  • 关于互联网的网站常见的网络推广方式
  • 阜阳做网站公司电子商务网站建设多少钱
  • 色弱可以做网站开发吗优化电池充电什么意思
  • 免费php模板网站买链接网
  • vs 2015可以做网站吗制作公司网页多少钱
  • 日本设计网站推荐谷歌优化排名公司
  • 政府网站开发计划书合川网站建设
  • 贵阳网站开发报价最近新闻内容
  • 网站模板大小bing搜索
  • 推广做网站多少钱网站搜索优化
  • 如何做公司培训网站西藏自治区seo 标题 关键词优化
  • 杭州做美妆的网站竞价推广出价多少合适
  • 网站开发培训什么传播易广告投放平台
  • 嘉兴app开发公司引擎优化seo是什么
  • 自己想做个网站江苏seo团队
  • wordpress文件下载页面免费网站seo
  • proxy网站网络营销是网上销售吗
  • 雄安网站开发互联网公司有哪些
  • 商标注册查询系统官网网站seo优化方案
  • 做阿里巴巴类似的网站seo外链专员工作要求
  • wordpress php调优seo教程排名第一
  • 网站上的ar是什么软件做的网站注册流程