1237 Find Positive Integer Solution for a Given Equation 找出給定方程的正整數(shù)解
Description:
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:
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) = x + y
Example 2:
Input: function_id = 2, z = 5
Output: [[1,5],[5,1]]
Explanation: function_id = 2 means that f(x, y) = 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 range 1 <= x, y <= 1000
It's also guaranteed that f(x, y) will fit in 32 bit signed integer if 1 <= x, y <= 1000
題目描述:
給出一個函數(shù) f(x, y) 和一個目標結果 z,請你計算方程 f(x,y) == z 所有可能的正整數(shù) 數(shù)對 x 和 y。
給定函數(shù)是嚴格單調(diào)的,也就是說:
f(x, y) < f(x + 1, y)
f(x, y) < f(x, y + 1)
函數(shù)接口定義如下:
interface CustomFunction {
public:
// Returns positive integer f(x, y) for any given positive integer x and y.
int f(int x, int y);
};
如果你想自定義測試,你可以輸入整數(shù) function_id 和一個目標結果 z 作為輸入,其中 function_id 表示一個隱藏函數(shù)列表中的一個函數(shù)編號,題目只會告訴你列表中的 2 個函數(shù)。
你可以將滿足條件的 結果數(shù)對 按任意順序返回。
示例 :
示例 1:
輸入:function_id = 1, z = 5
輸出:[[1,4],[2,3],[3,2],[4,1]]
解釋:function_id = 1 表示 f(x, y) = x + y
示例 2:
輸入:function_id = 2, z = 5
輸出:[[1,5],[5,1]]
解釋:function_id = 2 表示 f(x, y) = x * y
提示:
1 <= function_id <= 9
1 <= z <= 100
題目保證 f(x, y) == z 的解處于 1 <= x, y <= 1000 的范圍內(nèi)。
在 1 <= x, y <= 1000 的前提下,題目保證 f(x, y) 是一個 32 位有符號整數(shù)。
思路:
注意到函數(shù)是嚴格單調(diào)的, 而且 x和 y都在 [1, 1000]的范圍內(nèi), 可以使用雙指針搜索 z的值
時間復雜度O(n), 空間復雜度O(1)
代碼:
C++:
/*
* // 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>> result;
int left = 1, right = 1000;
while (left < 1001 && right > 0)
{
int temp = customfunction.f(left, right);
if (temp == z) result.push_back({left, right});
if (temp > z) --right;
else ++left;
}
return result;
}
};
Java:
/*
* // 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>> result = new ArrayList<>();
int left = 1, right = 1000;
while (left < 1001 && right > 0) {
int temp = customfunction.f(left, right);
if (temp == z) result.add(Arrays.asList(left, right));
if (temp > z) right--;
else left++;
}
return result;
}
}
Python:
"""
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)
def f(self, x, y):
"""
class Solution:
def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
return filter(lambda x: customfunction.f(*x) == z, itertools.product(range(1, z + 1), repeat=2))