算法刷题打卡第94天: 找出给定方程的正整数解

找出给定方程的正整数解

难度:中等

给你一个函数 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 ( m log ⁡ n ) O(m \log n) O(mlogn),其中 m m m x x x 的取值数目, n n n y y y 的取值数目。
  • 空间复杂度: O ( 1 ) O(1) O(1)。返回值不计入空间复杂度。
"""
   This is the custom function interface.
   You should not implement it, or speculate about its implementation
   class 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, 1000
        while l1 <= r1:   
            mid = (l1 + r1) // 2 
            if customfunction.f(mid, 1) > z:
                r1 = mid - 1
            else:
                l1 = mid + 1

        # 求 x 可能的最小值     
        l2, r2 = 1, l1
        while l2 <= r2:   
            mid = (l2 + r2) // 2 
            if customfunction.f(mid, 1000) < z:
                l2 = mid + 1
            else:
                r2 = mid - 1

        # 求 x 合理区间内,和 y 可能的数组
        res = []
        for i in range(l2, l1):
            l, r = 1, 1000
            while l <= r:
                mid = (l + r) // 2
                if customfunction.f(i, mid) == z:
                    res.append([i, mid])
                    break
                elif customfunction.f(i, mid) > z:
                    r = mid - 1
                else:
                    l = mid + 1
        return res

双指针

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

复杂度分析:

  • 时间复杂度: O ( m + n ) O(m+n) O(m+n),其中 m m m x x x 的取值数目, n n n y y y 的取值数目。
  • 空间复杂度: O ( 1 ) O(1) O(1)。返回值不计入空间复杂度。
"""
   This is the custom function interface.
   You should not implement it, or speculate about its implementation
   class 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 = 1000
        res = []
        for x in range(1, 1001):
            while y and customfunction.f(x, y) > z:
                y -= 1
            if y == 0:
                break
            if customfunction.f(x, y) == z:
                res.append([x, y])
        return res

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏秃然

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值