Skip to content

Commit a25be8e

Browse files
authored
Merge pull request 6boris#587 from 0xff-dev/1237
Add solution and test-cases for problem 1237
2 parents c82bdfe + 02c6ee9 commit a25be8e

File tree

3 files changed

+81
-24
lines changed

3 files changed

+81
-24
lines changed

leetcode/1201-1300/1237.Find-Positive-Integer-Solution-for-a-Given-Equation/README.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
11
# [1237.Find Positive Integer Solution for a Given Equation][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
75

8-
**Example 1:**
6+
While the exact formula is hidden, the function is monotonically increasing, i.e.:
7+
8+
- `f(x, y) < f(x + 1, y)`
9+
- `f(x, y) < f(x, y + 1)`
10+
11+
The function interface is defined like this:
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
interface CustomFunction {
15+
public:
16+
// Returns some positive integer f(x, y) for two positive integers x and y based on a formula.
17+
int f(int x, int y);
18+
};
1319
```
1420

15-
## 题意
16-
> ...
21+
We will judge your solution as follows:
22+
23+
- The judge has a list of `9` hidden implementations of `CustomFunction`, along with a way to generate an **answer key** of all valid pairs for a specific `z`.
24+
- The judge will receive two inputs: a `function_id` (to determine which implementation to test your code with), and the target `z`.
25+
- The judge will call your `findSolution` and compare your results with the **answer key**.
26+
- If your results match the **answer key**, your solution will be `Accepted`.
1727

18-
## 题解
28+
**Example 1:**
1929

20-
### 思路1
21-
> ...
22-
Find Positive Integer Solution for a Given Equation
23-
```go
2430
```
31+
Input: function_id = 1, z = 5
32+
Output: [[1,4],[2,3],[3,2],[4,1]]
33+
Explanation: The hidden formula for function_id = 1 is f(x, y) = x + y.
34+
The following positive integer values of x and y make f(x, y) equal to 5:
35+
x=1, y=4 -> f(1, 4) = 1 + 4 = 5.
36+
x=2, y=3 -> f(2, 3) = 2 + 3 = 5.
37+
x=3, y=2 -> f(3, 2) = 3 + 2 = 5.
38+
x=4, y=1 -> f(4, 1) = 4 + 1 = 5.
39+
```
40+
41+
**Example 2:**
2542

43+
```
44+
Input: function_id = 2, z = 5
45+
Output: [[1,5],[5,1]]
46+
Explanation: The hidden formula for function_id = 2 is f(x, y) = x * y.
47+
The following positive integer values of x and y make f(x, y) equal to 5:
48+
x=1, y=5 -> f(1, 5) = 1 * 5 = 5.
49+
x=5, y=1 -> f(5, 1) = 5 * 1 = 5.
50+
```
2651

2752
## 结语
2853

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(customFunction func(int, int) int, z int) [][]int {
4+
ans := make([][]int, 0)
5+
for y := 1; y <= 1000; y++ {
6+
l, r := 1, 1000
7+
if r := customFunction(1, y); r > z {
8+
break
9+
}
10+
for l < r {
11+
m := l + (r-l)/2
12+
rr := customFunction(m, y)
13+
14+
if rr == z {
15+
ans = append(ans, []int{m, y})
16+
break
17+
}
18+
if rr < z {
19+
l = m + 1
20+
continue
21+
}
22+
r = m
23+
}
24+
}
25+
return ans
526
}

leetcode/1201-1300/1237.Find-Positive-Integer-Solution-for-a-Given-Equation/Solution_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,45 @@ import (
66
"testing"
77
)
88

9+
func f1(a, b int) int {
10+
return a + b
11+
}
12+
func f2(a, b int) int {
13+
return a * b
14+
}
915
func TestSolution(t *testing.T) {
1016
// 测试用例
1117
cases := []struct {
1218
name string
13-
inputs bool
14-
expect bool
19+
inputs func(int, int) int
20+
z int
21+
expect [][]int
1522
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
23+
{"TestCase1", f1, 5, [][]int{{4, 1}, {3, 2}, {2, 3}, {1, 4}}},
24+
{"TestCase2", f2, 480, [][]int{
25+
{480, 1}, {240, 2}, {160, 3}, {120, 4}, {96, 5}, {80, 6}, {60, 8}, {48, 10},
26+
{40, 12}, {32, 15}, {30, 16}, {24, 20}, {20, 24}, {16, 30}, {15, 32},
27+
{12, 40}, {10, 48}, {8, 60}, {6, 80}, {5, 96}, {4, 120}, {3, 160}, {2, 240}, {1, 480},
28+
}},
29+
{"TestCase3", f2, 5, [][]int{{5, 1}, {1, 5}}},
1930
}
2031

2132
// 开始测试
2233
for i, c := range cases {
2334
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
35+
got := Solution(c.inputs, c.z)
2536
if !reflect.DeepEqual(got, c.expect) {
2637
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
38+
c.expect, got, c.z)
2839
}
2940
})
3041
}
3142
}
3243

33-
// 压力测试
44+
// 压力测试
3445
func BenchmarkSolution(b *testing.B) {
3546
}
3647

37-
// 使用案列
48+
// 使用案列
3849
func ExampleSolution() {
3950
}

0 commit comments

Comments
 (0)