首页 > 其他分享 >力扣---1237. 找出给定方程的正整数解

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

时间:2023-02-18 23:55:59浏览次数:34  
标签:function 力扣 1237 int customfunction --- p2 p1 CustomFunction

给你一个函数  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

示例 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 <= function_id <= 9
    1 <= z <= 100
    题目保证 f(x, y) == z 的解处于 1 <= x, y <= 1000 的范围内。
    在 1 <= x, y <= 1000 的前提下,题目保证 f(x, y) 是一个 32 位有符号整数。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-positive-integer-solution-for-a-given-equation
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


 

题目想要读懂比较难。

题目意思是给你一个封装好的函数customfunction.f(x, y),和一个答案z,你需要寻找符合customfunction.f(x, y) == z 的x,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>> res = new ArrayList<>();
        for (int i = 1; i < 1001; i++) {
            int j;
            int left = 1;
            int right = 1000;
            while (left <= right) {
                j = (left + right) / 2;
                if (customfunction.f(i, j) == z) {
                    res.add(Arrays.asList(i, j));
                    break;
                } else if (customfunction.f(i, j) < z) {
                    left = j + 1;
                } else {
                    right = j - 1;
                }
            }
        }
        return res;
    }
}

优化,双指针,也是利用了单调递增。

由于不管是x增大,还是y增大,z都会增大,所以当x增大时,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>> res = new ArrayList<>();
        for (int p1 = 1, p2 = 1000; p1 < 1001; p1 ++) {
            while (p2 > 0 && customfunction.f(p1, p2) > z) {
                p2 --;
            }
            if (p2 > 0 && customfunction.f(p1, p2) == z) {
                res.add(Arrays.asList(p1, p2));
            }
        }
        return res;
    }
}

 

标签:function,力扣,1237,int,customfunction,---,p2,p1,CustomFunction
From: https://www.cnblogs.com/allWu/p/17134014.html

相关文章

  • 读--架构漫谈之后
    在阅读过王概凯的架构漫谈之后我理解到关于架构的更深层次的东西。思考了以下几个方面我有自己的一点认识:1.         什么是架构:作者说架构实际上就是指人们根......
  • 【Spring IOC】【七】容器源码解析- PostConstruct、PreDestory的源码分析
    1 前言@PostConstruct、@PreDestory这两个注解大家应该有用过吧,我们这篇分析一下主要是PostConstruct这个注解的解析时机和执行时机。2 源码分析2.1 解析时机-doC......
  • 从0到1一步一步玩转openEuler--18 openEuler 管理服务-简介
    18管理服务简介systemd是在Linux下,与SysV和LSB初始化脚本兼容的系统和服务管理器。systemd使用socket和D-Bus来开启服务,提供基于守护进程的按需启动策略,支持快照和系统状......
  • k8s学习-记录一次集群kube-controller,scheduler等多个pod重启的问题解决
    问题一次,集群的kube-controller,scheduler等容器重启,查看日志,发现时间很集中,在秒级范围内多个pod同时重启。查看pod状态kubectlgetpod-nkube-system|grepkube-contro......
  • Linux - ethtool 使用方法
    ethtool是用于查询及设置网卡参数的命令。1、安装yum源安装:yuminstallethtool源码包安装:ethtool源码包下载地址:http://ethtool.sourcearchive.com/2、ethtool常......
  • 分支和循环(二) 2023-2-16
    for循环for循环将条件、判断、调整放到同一部分相比while循环更好调整代码for循环流程图表达式1只执行一次之后不参与循环,执行完语句后调整表达式3for循环和while循环......
  • 每天一点基础K8S--prometheus基础
    prometheus基础1、简介1、开源的系统监控和告警系统Prometheusisanopen-sourcesystemsmonitoringandalertingtoolkit官网:https://prometheus.io/docs/introdu......
  • 信道估计算法误码率仿真,对比不同导频长度,对比不同信道估计算法包括CS-OMP,LS,MMSE
    1.算法描述所谓信道估计,就是从接收数据中将假定的某个信道模型的模型参数出来的过程。如果信道是线性的话,那么信道估计就是对系统冲激响应进行估计。CS-OMP正则正交匹......
  • etcd学习笔记-组件架构
    etcd项目结构其中核心模块etcdserver,lease,mvcc,raft整体架构客户端层包括clientv3和etcdctl等客户端,用户通过命令行或者客户端通过restfulapi降低了etcd客户端的使用复杂......
  • C# - sqlserver in 参数化传值
    用exec('+sql语句+')的形式,其中in判断条件全部写到参数中。sql=@"exec('SELECTSWH.Date,SWH.Shift,SWH.FunctionUnit,SWH.ShiftAttendance,SWH.[ShiftTotalWorking......