4
4
import java .util .Arrays ;
5
5
import java .util .List ;
6
6
7
- /**
8
- * 1237. Find Positive Integer Solution for a Given Equation
9
- *
10
- * Given a function f(x, y) and a value z, return all positive integer pairs x and y where f(x,y) == z.
11
- * The function is constantly increasing, i.e.:
12
- *
13
- * f(x, y) < f(x + 1, y)
14
- * f(x, y) < f(x, y + 1)
15
- * The function interface is defined like this:
16
- *
17
- * interface CustomFunction {
18
- * public:
19
- * // Returns positive integer f(x, y) for any given positive integer x and y.
20
- * int f(int x, int y);
21
- * };
22
- * For custom testing purposes you're given an integer function_id and a target z as input,
23
- * where function_id represent one function from an secret internal list, on the examples you'll know only two functions from the list.
24
- * You may return the solutions in any order.
25
- *
26
- * Example 1:
27
- * Input: function_id = 1, z = 5
28
- * Output: [[1,4],[2,3],[3,2],[4,1]]
29
- * Explanation: function_id = 1 means that f(x, y) = x + y
30
- *
31
- * Example 2:
32
- * Input: function_id = 2, z = 5
33
- * Output: [[1,5],[5,1]]
34
- * Explanation: function_id = 2 means that f(x, y) = x * y
35
- *
36
- * Constraints:
37
- * 1 <= function_id <= 9
38
- * 1 <= z <= 100
39
- * It's guaranteed that the solutions of f(x, y) == z will be on the range 1 <= x, y <= 1000
40
- * It's also guaranteed that f(x, y) will fit in 32 bit signed integer if 1 <= x, y <= 1000
41
- */
42
7
public class _1237 {
43
8
44
9
// This is the custom function interface.
@@ -51,8 +16,10 @@ abstract class CustomFunction {
51
16
}
52
17
53
18
public static class Solution1 {
54
- /**Time: O(x*y)
55
- * Space: O(1)*/
19
+ /**
20
+ * Time: O(x*y)
21
+ * Space: O(1)
22
+ */
56
23
public List <List <Integer >>findSolution (CustomFunction customfunction ,int z ) {
57
24
List <List <Integer >>result =new ArrayList <>();
58
25
for (int x =1 ;x <=1000 ;x ++) {
@@ -69,7 +36,7 @@ public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
69
36
public static class Solution2 {
70
37
/**
71
38
* linear search
72
- *
39
+ *<p>
73
40
* Time: O(x + y)
74
41
* Space: O(1)
75
42
*/
@@ -94,10 +61,10 @@ public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
94
61
public static class Solution3 {
95
62
/**
96
63
* binary search
97
- *
64
+ * <p>
98
65
* Time: O(xlogy)
99
66
* Space: O(1)
100
- * * /
67
+ */
101
68
public List <List <Integer >>findSolution (CustomFunction customfunction ,int z ) {
102
69
List <List <Integer >>result =new ArrayList <>();
103
70
for (int x =1 ;x <=1000 ;x ++) {