-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path1237. Find Positive Integer Solution for a Given Equation.cpp
45 lines (39 loc) · 1.58 KB
/
1237. Find Positive Integer Solution for a Given Equation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Find Positive Integer Solution for a Given Equation.
//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for Find Positive Integer Solution for a Given Equation.
/*
* // 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>> ans;
int x = 1, y;
for(y = 1; customfunction.f(x, y) <= z; y++){
// cout << x << " " << y << " " << customfunction.f(x, y) << endl;
if(customfunction.f(x, y) == z){
ans.push_back({x, y});
break;
}
}
//customfunction.f(x, 1) <= z: break if smallest y will make f(x,y) > z
for(x = 2; customfunction.f(x, y) >= z && customfunction.f(x, 1) <= z; x++){
for(; customfunction.f(x, y) >= z && y > 0; y--){
// cout << x << " " << y << " " << customfunction.f(x, y) << endl;
if(customfunction.f(x, y) == z){
ans.push_back({x, y});
break;
}
}
y++;
}
return ans;
}
};