每日一题——找出给定方程的正整数解

108 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 18 天,点击查看活动详情


1237. 找出给定方程的正整数解

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

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

  • 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);
};

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

  • 判题程序有一个由 CustomFunction 的 9 种实现组成的列表,以及一种为特定的 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

 

提示:

  • 1 <= function_id <= 9
  • 1 <= z <= 100
  • 题目保证 f(x, y) == z 的解处于 1 <= x, y <= 1000 的范围内。
  • 在 1 <= x, y <= 1000 的前提下,题目保证 f(x, y) 是一个 32 位有符号整数。

思路

这道题目看起来很难理解,但其实仔细分析一下就能看出来,我们要求得 x 和 y,使得 f(x, y) == z 即可,因为我们要找到所有可能的情况,这就需要遍历两个元素的所有取值。

因为其取值本身就是有序的,故我们可以遍历 x,对于 y 来说,我们可以使用二分查找来找到符合条件的 y 的取值。

其中需要调用一下接口的函数,题目指出我们不需要去实现函数,只是简单的对其进行调用即可,我们关心的点在于函数的结果是否满足要求。

题解

/*
 * // 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)
 *     public int f(int x, int y);
 * };
 */

class Solution {
    public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
        List<List<Integer>> ans = new ArrayList<>();
        for(int x = 1; x <= 1000; x++) {
            int left = 1, right = 1000;
            while(left <= right) {
                int mid = left + ((right - left) >> 1);
                if(customfunction.f(x, mid) < z) {
                    left = mid + 1;
                }else if(customfunction.f(x, mid) > z) {
                    right = mid - 1;
                }else {
                    List<Integer> list = Arrays.asList(x, mid);
                    ans.add(list);
                    break;
                }
            }
        }
        return ans;
    }
}

如果你有其他的思路或者更好的解法,亦或者你发现了文章出现了错误或有不足,欢迎在评论区和我交流,我看到了一定会回复。

写文章不易,如果你觉得文章对你有帮助,麻烦点一下点赞、收藏,你的支持是我写文章的最大动力!