[LeetCode]1237. 找出给定方程的正整数解
创始人
2024-05-26 06:26:28
0

题目链接:https://leetcode.cn/problems/find-positive-integer-solution-for-a-given-equation/description/
题目描述:
在这里插入图片描述
样例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:暴力枚举

func findSolution(customFunction func(int, int) int, z int) [][]int {res := make([][]int, 0, 100)for x := 1; x <= 1000; x++ {for y := 1; y<= 1000; y++ {if customFunction(x,y) == z {temp := []int{x, y}res = append(res,temp)}}}return res
}

暴力枚举法每次都从开始找y,x最多枚举1000次,而y每次也会枚举1000次,因此,总的复杂度为O(x*y)。

方法2:枚举+二分查找

func findSolution(customFunction func(int, int) int, z int) (res [][]int)  {for x := 1; x <= 1000; x++ {l, r := 1, 1000for l <= r {mid := (l+r)/2if customFunction(x,mid) == z {res = append(res, []int{x,mid})break} else if customFunction(x,mid) > z {r = mid -1 } else {l = mid + 1}}}return res
}

既然我们知道y每次都从头开始找比较慢,那么我们可以优化y的查找时间,利用二分查找即可将找y的复杂度降到log级别,因此总的时间复杂度为O(xlogy)。

当然,golang中也可以使用sort库的Search方法:

func findSolution(customFunction func(int, int) int, z int) (res [][]int) {for x := 1; x <= 1000; x++ {y := 1 + sort.Search(999, func(y int) bool {return customFunction(x, y+1) >= z})if customFunction(x,y) == z {res = append(res, []int{x,y})}}return res
}

同时需要注意Search的用法,是从[0,n)去查找的,所以类似我们是从0-999中查找出来的下标,最后加上1就表示从1到1000了:

// Search uses binary search to find and return the smallest index i
// in [0, n) at which f(i) is true, assuming that on the range [0, n),
// f(i) == true implies f(i+1) == true. That is, Search requires that
// f is false for some (possibly empty) prefix of the input range [0, n)
// and then true for the (possibly empty) remainder; Search returns
// the first true index. If there is no such index, Search returns n.
// (Note that the “not found” return value is not -1 as in, for instance,
方法3:双指针

func findSolution(customFunction func(int, int) int, z int) (res [][]int)  {y := 1000for x := 1; x <= 1000; x++ {for ; y > 0 ; y-- {if customFunction(x,y) < z {break}if customFunction(x,y) == z {res = append(res, []int{x,y})break}}}return res
}

这种方法关键在于直接利用函数单调递增的特性,一个答案从前往后找,另一个答案从后往前找,下一次寻找一定是基于上一次的结果,因此会少很多次遍历,x最多遍历1000次,y总的遍历次数也最多1000次,因此总的时间复杂度为O(x+y)。

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...