-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1237-find-positive-integer-solution-for-a-given-equation.js
More file actions
51 lines (47 loc) · 1.27 KB
/
1237-find-positive-integer-solution-for-a-given-equation.js
File metadata and controls
51 lines (47 loc) · 1.27 KB
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
46
47
48
49
50
51
/**
* 1237. Find Positive Integer Solution for a Given Equation
* https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/
* Difficulty: Medium
*
* Given a callable function f(x, y) with a hidden formula and a value z, reverse engineer
* the formula and return all positive integer pairs x and y where f(x,y) == z. You may
* return the pairs in any order.
*
* While the exact formula is hidden, the function is monotonically increasing, i.e.:
* - f(x, y) < f(x + 1, y)
* - f(x, y) < f(x, y + 1)
*/
/**
* // This is the CustomFunction's API interface.
* // You should not implement it, or speculate about its implementation
* function CustomFunction() {
* @param {integer, integer} x, y
* @return {integer}
* this.f = function(x, y) {
* ...
* };
* };
*/
/**
* @param {CustomFunction} customfunction
* @param {integer} z
* @return {integer[][]}
*/
function findSolution(customfunction, z) {
const pairs = [];
let left = 1;
let right = 1000;
while (left <= 1000 && right >= 1) {
const value = customfunction.f(left, right);
if (value === z) {
pairs.push([left, right]);
left++;
right--;
} else if (value < z) {
left++;
} else {
right--;
}
}
return pairs;
}