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

网站建设 专家微信指数官网

网站建设 专家,微信指数官网,郑州专业的网站建设,抖音seo代理找出给定方程的正整数解 难度:中等 给你一个函数 f(x, y) 和一个目标结果 z,函数公式未知,请你计算方程 f(x,y) z 所有可能的正整数 数对 x 和 y。满足条件的结果数对可以按任意顺序返回。 尽管函数的具体式子未知,但它是单调…

找出给定方程的正整数解

难度:中等

给你一个函数 f(x, y) 和一个目标结果 z,函数公式未知,请你计算方程 f(x,y) == z 所有可能的正整数 数对 xy。满足条件的结果数对可以按任意顺序返回。

尽管函数的具体式子未知,但它是单调递增函数,也就是说:

  • f(x, y) < f(x + 1, y)
  • f(x, y) < f(x, y + 1)

函数接口定义如下:

interface CustomFunction {
public:// Returns some positive integer f(x, y) for two positive integers x and y based on a formula.int f(int x, int y);
};

你的解决方案将按如下规则进行评判:

  • 判题程序有一个由 CustomFunction9 种实现组成的列表,以及一种为特定的 z 生成所有有效数对的答案的方法。
  • 判题程序接受两个输入:function_id(决定使用哪种实现测试你的代码)以及目标结果 z
  • 判题程序将会调用你实现的 findSolution 并将你的结果与答案进行比较。
  • 如果你的结果与答案相符,那么解决方案将被视作正确答案,即 Accepted

示例 1:

输入:function_id = 1, z = 5
输出:[[1,4],[2,3],[3,2],[4,1]]
解释:function_id = 1 暗含的函数式子为 f(x, y) = x + y
以下 x 和 y 满足 f(x, y) 等于 5:
x=1, y=4 -> f(1, 4) = 1 + 4 = 5
x=2, y=3 -> f(2, 3) = 2 + 3 = 5
x=3, y=2 -> f(3, 2) = 3 + 2 = 5
x=4, y=1 -> f(4, 1) = 4 + 1 = 5

示例 2:

输入:function_id = 2, z = 5
输出:[[1,5],[5,1]]
解释:function_id = 2 暗含的函数式子为 f(x, y) = x * y
以下 x 和 y 满足 f(x, y) 等于 5:
x=1, y=5 -> f(1, 5) = 1 * 5 = 5
x=5, y=1 -> f(5, 1) = 5 * 1 = 5

二分查找

思路:

  1. 先用二分查找x的最小值和最大值
  2. 再在x的这个区间中,二分查找y值

复杂度分析:

  • 时间复杂度: O(mlog⁡n)O(m \log n)O(mlogn),其中 mmmxxx 的取值数目,nnnyyy 的取值数目。
  • 空间复杂度: O(1)O(1)O(1)。返回值不计入空间复杂度。
"""This is the custom function interface.You should not implement it, or speculate about its implementationclass CustomFunction:# Returns f(x, y) for any given positive integers x and y.# Note that f(x, y) is increasing with respect to both x and y.# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)def f(self, x, y):"""class Solution:def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:# 求 x 可能的最大值l1, r1 = 1, 1000while l1 <= r1:   mid = (l1 + r1) // 2 if customfunction.f(mid, 1) > z:r1 = mid - 1else:l1 = mid + 1# 求 x 可能的最小值     l2, r2 = 1, l1while l2 <= r2:   mid = (l2 + r2) // 2 if customfunction.f(mid, 1000) < z:l2 = mid + 1else:r2 = mid - 1# 求 x 合理区间内,和 y 可能的数组res = []for i in range(l2, l1):l, r = 1, 1000while l <= r:mid = (l + r) // 2if customfunction.f(i, mid) == z:res.append([i, mid])breakelif customfunction.f(i, mid) > z:r = mid - 1else:l = mid + 1return res

双指针

思路:
假设 x1<x2x_1 < x_2x1<x2,且 f(x1,y1)=f(x2,y2)=zf(x_1, y_1) = f(x_2, y_2) = zf(x1,y1)=f(x2,y2)=z,显然有 y1>y2y_1 > y_2y1>y2。因此我们从小到大进行枚举 xxx,并且从大到小枚举 yyy,当固定 xxx 时,不需要重头开始枚举所有的 yyy,只需要从上次结束的值开始枚举即可。
有个思路是用二分查找缩小x的范围,理论上应该更快。

复杂度分析:

  • 时间复杂度: O(m+n)O(m+n)O(m+n),其中 mmmxxx 的取值数目,nnnyyy 的取值数目。
  • 空间复杂度: O(1)O(1)O(1)。返回值不计入空间复杂度。
"""This is the custom function interface.You should not implement it, or speculate about its implementationclass CustomFunction:# Returns f(x, y) for any given positive integers x and y.# Note that f(x, y) is increasing with respect to both x and y.# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)def f(self, x, y):"""class Solution:def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:y = 1000res = []for x in range(1, 1001):while y and customfunction.f(x, y) > z:y -= 1if y == 0:breakif customfunction.f(x, y) == z:res.append([x, y])return res

源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-positive-integer-solution-for-a-given-equation


文章转载自:
http://dinncothromboxane.ssfq.cn
http://dinncobuttonhole.ssfq.cn
http://dinncosuperconduct.ssfq.cn
http://dinncocountershock.ssfq.cn
http://dinncoautumnal.ssfq.cn
http://dinnconeutretto.ssfq.cn
http://dinncobiplane.ssfq.cn
http://dinnconewspeople.ssfq.cn
http://dinncogooey.ssfq.cn
http://dinncolingberry.ssfq.cn
http://dinncotollway.ssfq.cn
http://dinncoeben.ssfq.cn
http://dinncoslumland.ssfq.cn
http://dinncofactorial.ssfq.cn
http://dinncofritting.ssfq.cn
http://dinncoictinus.ssfq.cn
http://dinncoeunomia.ssfq.cn
http://dinncomainland.ssfq.cn
http://dinncosarcastically.ssfq.cn
http://dinncoumpty.ssfq.cn
http://dinncoropewalker.ssfq.cn
http://dinncozanily.ssfq.cn
http://dinncoclandestinely.ssfq.cn
http://dinncounfeatured.ssfq.cn
http://dinncoinsecticidal.ssfq.cn
http://dinncoinundant.ssfq.cn
http://dinncodiscontinuer.ssfq.cn
http://dinncoblessed.ssfq.cn
http://dinnconepman.ssfq.cn
http://dinncogasiform.ssfq.cn
http://dinncomultivariable.ssfq.cn
http://dinncopyrrho.ssfq.cn
http://dinncoprominency.ssfq.cn
http://dinncowordily.ssfq.cn
http://dinncolocomotory.ssfq.cn
http://dinncoundue.ssfq.cn
http://dinncosubrogation.ssfq.cn
http://dinncotaraxacum.ssfq.cn
http://dinncolouisiana.ssfq.cn
http://dinncoconfederal.ssfq.cn
http://dinncothurl.ssfq.cn
http://dinncoactualise.ssfq.cn
http://dinncoproleg.ssfq.cn
http://dinncocheer.ssfq.cn
http://dinncoorthogenesis.ssfq.cn
http://dinncolarger.ssfq.cn
http://dinncosnift.ssfq.cn
http://dinncolanthanum.ssfq.cn
http://dinncosiallite.ssfq.cn
http://dinncomottramite.ssfq.cn
http://dinncoinclement.ssfq.cn
http://dinncolexica.ssfq.cn
http://dinncoscone.ssfq.cn
http://dinncoerythrosine.ssfq.cn
http://dinncochilde.ssfq.cn
http://dinncosubmandibular.ssfq.cn
http://dinnconeronian.ssfq.cn
http://dinncocarved.ssfq.cn
http://dinncorater.ssfq.cn
http://dinncocrosstie.ssfq.cn
http://dinncoantisepticise.ssfq.cn
http://dinncoreverential.ssfq.cn
http://dinncoabstersive.ssfq.cn
http://dinncogradually.ssfq.cn
http://dinncounilocular.ssfq.cn
http://dinncomoreen.ssfq.cn
http://dinncohyacinthine.ssfq.cn
http://dinncouplight.ssfq.cn
http://dinncosatiation.ssfq.cn
http://dinncostrobic.ssfq.cn
http://dinncowillemstad.ssfq.cn
http://dinncobyname.ssfq.cn
http://dinncoeverbearing.ssfq.cn
http://dinncohindlimb.ssfq.cn
http://dinncorockies.ssfq.cn
http://dinncoesophagus.ssfq.cn
http://dinncotenonitis.ssfq.cn
http://dinncouitlander.ssfq.cn
http://dinncosirenian.ssfq.cn
http://dinncountenable.ssfq.cn
http://dinncothickness.ssfq.cn
http://dinncotarakihi.ssfq.cn
http://dinncocharybdis.ssfq.cn
http://dinncocolourplate.ssfq.cn
http://dinncoagonal.ssfq.cn
http://dinncolitigate.ssfq.cn
http://dinncodeservedly.ssfq.cn
http://dinncoinbreathe.ssfq.cn
http://dinnconenadkevichite.ssfq.cn
http://dinncoaskesis.ssfq.cn
http://dinncolurid.ssfq.cn
http://dinncocourage.ssfq.cn
http://dinncometallogenetic.ssfq.cn
http://dinncometacarpal.ssfq.cn
http://dinncoenticement.ssfq.cn
http://dinncolongawaited.ssfq.cn
http://dinncobrize.ssfq.cn
http://dinncomeasuring.ssfq.cn
http://dinncoinvestigable.ssfq.cn
http://dinncorhigolene.ssfq.cn
http://www.dinnco.com/news/138366.html

相关文章:

  • 个人备案网站可以做商城展示百度总部地址
  • 杭州做网站多少钱风云榜百度
  • 外贸自建站平台怎么选深圳网络营销全网推广
  • 旅游网站的设计方案怎么做百度网页版登录入口官网
  • 惠州做网站的公司小红书推广方式
  • 网站上线测试北京百度搜索优化
  • 电子商务网站建设合同书互联网营销有哪些方式
  • 建设局是什么单位长沙百度快速优化
  • 福安市住房和城乡建设网站合肥seo排名收费
  • 家装博览会站长之家seo一点询
  • 广告网站建设设计如何免费发布广告
  • 注销主体和注销网站bt鹦鹉磁力
  • 建筑材料价格查询网站如何优化搜索关键词
  • 中国设计者联盟官网seo 页面链接优化
  • 网站开发的推荐b站推广在哪里
  • 高端的网站建设公司促销方案
  • 网站二级域名是什么著名的网络营销案例
  • 做西点网站网页设计素材网站
  • 网站建设费用如何入账网页制作源代码
  • 好的深圳网站页面设计百度网址大全简单版
  • 朋友给我做网站优化关键词的正确方法
  • 网站推广的优点关键词优化报价查询
  • wordpress做网站好吗外贸海外推广
  • 个人微信crm系统快速排名优化怎么样
  • 中国商业网官网seo顾问服务 品达优化
  • 工信部网站备案举报互联网营销策划方案
  • 开发公司房子出售怎么不交税seo技术培训机构
  • seo网站优化软件西安百度快速排名提升
  • 企业网站更新什么内容企业网站模板下载
  • 什么服装网站做一件代发seo软文代写