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

网站建设 专家全网关键词云查询

网站建设 专家,全网关键词云查询,唐山网站开发培训,做网站在阿里云买什么软件找出给定方程的正整数解 难度:中等 给你一个函数 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://www.ds6.com.cn/news/66784.html

相关文章:

  • 泉州比较好的网站开发建设公司广告联盟赚钱app
  • 聊城哪里网站做的好公众号seo排名
  • 家具网站建设规划书seo优化工具哪个好
  • 网站设计与网页制作招聘沈阳沈河seo网站排名优化
  • 淘宝客做网站好还是建群号站长网站推广
  • 西安驾校网站建设微博营销的特点
  • 电子商务网站建设技术山东工艺美术学院网站建设公司
  • centos做网站服务器吗百度搜索链接入口
  • 网站流量通道全国31省市疫情最新消息今天
  • 网站规划与开发技术专业网络运营推广合作
  • 合肥专业的房产网站建设搜索引擎营销案例
  • 桥头镇网站建设怎么网络推广
  • 自做淘宝客网站网站搭建外贸
  • 佛山做网站优化网站平台如何推广
  • 有什么可以在线做数学题的网站站长之家域名查询排行
  • 做网站话挣钱吗长沙网站推广智投未来
  • 做网站需要去哪里备案互联网广告销售是做什么的
  • 企业网站制作服务百度一下马上知道
  • 品牌型网站有哪些小红书广告投放平台
  • php电商网站开发app开发网站
  • 哪里办网站不用备案百度推广管理平台登录
  • 门户手机网站源码网站设计方案模板
  • 临沂做企业网站免费seo培训
  • 西安网站建设公司成都搜索优化整站优化
  • 做健身网站南京网站建设
  • wap网站百度爱采购推广一个月多少钱
  • 外贸网站平台是不是很难做优化落实疫情防控新十条
  • 建立网站需要注册公司吗百度公司招聘信息
  • 广州互联网seo的优点和缺点
  • 无锡网站制作哪些网络营销手段有哪四种