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

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

题目链接: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)。

http://www.lryc.cn/news/11529.html

相关文章:

  • 【路径规划】基于A*算法和Dijkstra算法的路径规划(Python代码实现)
  • 蓝桥杯 stm32 PWM 设置占空比
  • React 合成事件理解
  • 202302|读书笔记——国图点滴
  • Linux 操作系统原理 — NUMA 架构中的多线程调度开销与性能优化
  • OpenGL - 如何理解 VAO 与 VBO 之间的关系
  • Linux中sed的使用
  • [软件工程导论(第六版)]第1章 软件工程学概述(复习笔记)
  • ISP相关
  • vTESTstudio - VT System CAPL Functions - VT2004(续1)
  • WeakMap弱引用
  • Springboot 使用quartz 定时任务 增删改查
  • 华为OD机试 - 猜字谜(Python) | 机试题+算法思路 【2023】
  • Linux常用命令汇总
  • 1.TCP、UDP区别、TCP/IP七层、四层模型、应用层协议(计网)
  • 气敏电阻的原理,结构,分类及应用场景总结
  • 实验10 拓扑排序与最短路径2022
  • C/C++每日一练(20230218)
  • 【C语言】预编译
  • 音频信号处理笔记(一)
  • 【深度学习】模型评估
  • AcWing《蓝桥杯集训·每日一题》—— 3777 砖块
  • CleanMyMac X软件下载及详细功能介绍
  • pytorch零基础实现语义分割项目(一)——数据概况及预处理
  • ARM+LINUX嵌入式学习路线
  • echart在微信小程序的使用
  • 51单片机最强模块化封装(5)
  • 链表学习之判断链表是否回文
  • 【Linux06-基础IO】4.5万字的基础IO讲解
  • c++协程库理解—ucontext组件实践