Skip to content

Add solution and test-cases for problem 1237 #587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
# [1237.Find Positive Integer Solution for a Given Equation][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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.

**Example 1:**
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)`

The function interface is defined like this:

```
Input: a = "11", b = "1"
Output: "100"
interface CustomFunction {
public:
// Returns some positive integer f(x, y) for two positive integers x and y based on a formula.
int f(int x, int y);
};
```

## 题意
> ...
We will judge your solution as follows:

- 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`.
- The judge will receive two inputs: a `function_id` (to determine which implementation to test your code with), and the target `z`.
- The judge will call your `findSolution` and compare your results with the **answer key**.
- If your results match the **answer key**, your solution will be `Accepted`.

## 题解
**Example 1:**

### 思路1
> ...
Find Positive Integer Solution for a Given Equation
```go
```
Input: function_id = 1, z = 5
Output: [[1,4],[2,3],[3,2],[4,1]]
Explanation: The hidden formula for function_id = 1 is f(x, y) = x + y.
The following positive integer values of x and y make f(x, y) equal to 5:
x=1, y=4 -> f(1, 4) = 1 + 4 = 5.
x=2, y=3 -> f(2, 3) = 2 + 3 = 5.
x=3, y=2 -> f(3, 2) = 3 + 2 = 5.
x=4, y=1 -> f(4, 1) = 4 + 1 = 5.
```

**Example 2:**

```
Input: function_id = 2, z = 5
Output: [[1,5],[5,1]]
Explanation: The hidden formula for function_id = 2 is f(x, y) = x * y.
The following positive integer values of x and y make f(x, y) equal to 5:
x=1, y=5 -> f(1, 5) = 1 * 5 = 5.
x=5, y=1 -> f(5, 1) = 5 * 1 = 5.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(customFunction func(int, int) int, z int) [][]int {
ans := make([][]int, 0)
for y := 1; y <= 1000; y++ {
l, r := 1, 1000
if r := customFunction(1, y); r > z {
break
}
for l < r {
m := l + (r-l)/2
rr := customFunction(m, y)

if rr == z {
ans = append(ans, []int{m, y})
break
}
if rr < z {
l = m + 1
continue
}
r = m
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,45 @@ import (
"testing"
)

func f1(a, b int) int {
return a + b
}
func f2(a, b int) int {
return a * b
}
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs func(int, int) int
z int
expect [][]int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", f1, 5, [][]int{{4, 1}, {3, 2}, {2, 3}, {1, 4}}},
{"TestCase2", f2, 480, [][]int{
{480, 1}, {240, 2}, {160, 3}, {120, 4}, {96, 5}, {80, 6}, {60, 8}, {48, 10},
{40, 12}, {32, 15}, {30, 16}, {24, 20}, {20, 24}, {16, 30}, {15, 32},
{12, 40}, {10, 48}, {8, 60}, {6, 80}, {5, 96}, {4, 120}, {3, 160}, {2, 240}, {1, 480},
}},
{"TestCase3", f2, 5, [][]int{{5, 1}, {1, 5}}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.z)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
c.expect, got, c.z)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}