1237. Find Positive Integer Solution for a Given Equation*
Posted 珍妮的选择
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1237. Find Positive Integer Solution for a Given Equation*相关的知识,希望对你有一定的参考价值。
1237. Find Positive Integer Solution for a Given Equation*
https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/
题目描述
Given a function f(x, y)
and a value z
, return all positive integer pairs x
and y
where f(x,y) == z
.
The function is constantly increasing, i.e.:
-
f(x, y) < f(x + 1, y)
-
f(x, y) < f(x, y + 1)
The function interface is defined like this:
interface CustomFunction
public:
// Returns positive integer f(x, y) for any given positive integer x and y.
int f(int x, int y);
;
For custom testing purposes you’re given an integer function_id
and a target z
as input, where function_id
represent one function from an secret internal list, on the examples you’ll know only two functions from the list.
You may return the solutions in any order.
Example 1:
Input: function_id = 1, z = 5
Output: [[1,4],[2,3],[3,2],[4,1]]
Explanation: function_id = 1 means that f(x, y) =
Example 2:
Input: function_id = 2, z = 5
Output: [[1,5],[5,1]]
Explanation: function_id = 2 means that f(x, y) =
Constraints:
-
1 <= function_id <= 9
-
1 <= z <= 100
- It’s guaranteed that the solutions of
f(x, y) == z
will be on the range1 <= x, y <= 1000
- It’s also guaranteed that
f(x, y)
will fit in 32 bit signed integer if1 <= x, y <= 1000
C++ 实现 1
这道题的目的是考察二分法. 我一开始没有考虑到… 用的暴力的方法, beats 100%.
题目描述中, 说 f(x, y)
具体的形式未知, 只知道它递增. 另外, 注意约束中 1 <= x, y <= 1000
, 那么写两个 for 循环一个值一个值尝试即可.
/*
* // This is the custom function interface.
* // You should not implement it, or speculate about its implementation
* class CustomFunction
* public:
* // 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)
* int f(int x, int y);
* ;
*/
class Solution
public:
vector<vector<int>> findSolution(CustomFunction& customfunction, int z)
vector<vector<int>> res;
for (int x = 1; x <= 1000; ++ x)
for (int y = 1; y <= 1000; ++ y)
if (customfunction.f(x, y) > z) break;
if (customfunction.f(x, y) == z) res.push_back(x, y);
return res;
;
C++ 实现 2
实际上, 本题的目的是考察二分法. 因为题目中有递增的字眼, 要对这样的条件敏感一些.
来自 LeetCode Submission.
/*
* // This is the custom function interface.
* // You should not implement it, or speculate about its implementation
* class CustomFunction
* public:
* // 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)
* int f(int x, int y);
* ;
*/
/*
1. for all value of x, apply binary search to find the value of y
2. Time Complexity : O(x logy)
*/
class Solution
public:
vector<vector<int>> findSolution(CustomFunction& cf, int z)
vector<vector<int>> res;
for( int x = 1; x <= 1000; ++x )
int lo = 1, hi = 1000;
while( lo < hi )
int mid = lo + ( hi - lo )/2;
if( z > cf.f(x, mid) )
lo = mid + 1;
else
hi = mid;
if( cf.f(x, lo) == z )
res.push_back(x, lo);
return res;
;
以上是关于1237. Find Positive Integer Solution for a Given Equation*的主要内容,如果未能解决你的问题,请参考以下文章
1. MissingInteger 最小遗失整数 Find the minimal positive integer not occurring in a given sequence.
[LeetCode] 2441. Largest Positive Integer That Exists With Its Negative