Skip to content

Commit c7fdcf3

Browse files
committed
feat: add solutions to lc problem: No.1237. Find Solution
1 parent 2724be6 commit c7fdcf3

File tree

6 files changed

+370
-2
lines changed

6 files changed

+370
-2
lines changed

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/README.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,148 @@ x=5, y=1 -> f(5, 1) = 5 * 1 = 5</pre>
7474

7575
<!-- 这里可写通用的实现逻辑 -->
7676

77+
二分查找。
78+
7779
<!-- tabs:start -->
7880

7981
### **Python3**
8082

8183
<!-- 这里可写当前语言的特殊实现逻辑 -->
8284

8385
```python
84-
86+
"""
87+
This is the custom function interface.
88+
You should not implement it, or speculate about its implementation
89+
class CustomFunction:
90+
# Returns f(x, y) for any given positive integers x and y.
91+
# Note that f(x, y) is increasing with respect to both x and y.
92+
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
93+
def f(self, x, y):
94+
95+
"""
96+
97+
class Solution:
98+
def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
99+
res = []
100+
for x in range(1, 1001):
101+
left, right = 1, 1000
102+
while left < right:
103+
mid = (left + right) >> 1
104+
if customfunction.f(x, mid) >= z:
105+
right = mid
106+
else:
107+
left = mid + 1
108+
if customfunction.f(x, left) == z:
109+
res.append([x, left])
110+
return res
85111
```
86112

87113
### **Java**
88114

89115
<!-- 这里可写当前语言的特殊实现逻辑 -->
90116

91117
```java
118+
/*
119+
* // This is the custom function interface.
120+
* // You should not implement it, or speculate about its implementation
121+
* class CustomFunction {
122+
* // Returns f(x, y) for any given positive integers x and y.
123+
* // Note that f(x, y) is increasing with respect to both x and y.
124+
* // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
125+
* public int f(int x, int y);
126+
* };
127+
*/
128+
129+
class Solution {
130+
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
131+
List<List<Integer>> res = new ArrayList<>();
132+
for (int i = 1; i <= 1000; ++i) {
133+
int left = 1, right = 1000;
134+
while (left < right) {
135+
int mid = (left + right) >> 1;
136+
if (customfunction.f(i, mid) >= z) {
137+
right = mid;
138+
} else {
139+
left = mid + 1;
140+
}
141+
}
142+
if (customfunction.f(i, left) == z) {
143+
res.add(Arrays.asList(i, left));
144+
}
145+
}
146+
return res;
147+
}
148+
}
149+
```
150+
151+
### **C++**
152+
153+
```cpp
154+
/*
155+
* // This is the custom function interface.
156+
* // You should not implement it, or speculate about its implementation
157+
* class CustomFunction {
158+
* public:
159+
* // Returns f(x, y) for any given positive integers x and y.
160+
* // Note that f(x, y) is increasing with respect to both x and y.
161+
* // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
162+
* int f(int x, int y);
163+
* };
164+
*/
165+
166+
class Solution {
167+
public:
168+
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
169+
vector<vector<int>> res;
170+
for (int i = 1; i <= 1000; ++i) {
171+
int left = 1, right = 1000;
172+
while (left < right) {
173+
int mid = left + right >> 1;
174+
if (customfunction.f(i, mid) >= z) {
175+
right = mid;
176+
} else {
177+
left = mid + 1;
178+
}
179+
}
180+
if (customfunction.f(i, left) == z) {
181+
res.push_back({i, left});
182+
}
183+
}
184+
return res;
185+
}
186+
};
187+
```
92188
189+
### **Go**
190+
191+
```go
192+
/**
193+
* This is the declaration of customFunction API.
194+
* @param x int
195+
* @param x int
196+
* @return Returns f(x, y) for any given positive integers x and y.
197+
* Note that f(x, y) is increasing with respect to both x and y.
198+
* i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
199+
*/
200+
201+
func findSolution(customFunction func(int, int) int, z int) [][]int {
202+
res := [][]int{}
203+
for i := 1; i <= 1000; i++ {
204+
left, right := 1, 1000
205+
for left < right {
206+
mid := (left + right) >> 1
207+
if customFunction(i, mid) >= z {
208+
right = mid
209+
} else {
210+
left = mid + 1
211+
}
212+
}
213+
if customFunction(i, left) == z {
214+
res = append(res, []int{i, left})
215+
}
216+
}
217+
return res
218+
}
93219
```
94220

95221
### **...**

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/README_EN.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,144 @@ x=5, y=1 -&gt; f(5, 1) = 5 * 1 = 5.
7070

7171
## Solutions
7272

73+
Binary search.
74+
7375
<!-- tabs:start -->
7476

7577
### **Python3**
7678

7779
```python
78-
80+
"""
81+
This is the custom function interface.
82+
You should not implement it, or speculate about its implementation
83+
class CustomFunction:
84+
# Returns f(x, y) for any given positive integers x and y.
85+
# Note that f(x, y) is increasing with respect to both x and y.
86+
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
87+
def f(self, x, y):
88+
89+
"""
90+
91+
class Solution:
92+
def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
93+
res = []
94+
for x in range(1, 1001):
95+
left, right = 1, 1000
96+
while left < right:
97+
mid = (left + right) >> 1
98+
if customfunction.f(x, mid) >= z:
99+
right = mid
100+
else:
101+
left = mid + 1
102+
if customfunction.f(x, left) == z:
103+
res.append([x, left])
104+
return res
79105
```
80106

81107
### **Java**
82108

83109
```java
110+
/*
111+
* // This is the custom function interface.
112+
* // You should not implement it, or speculate about its implementation
113+
* class CustomFunction {
114+
* // Returns f(x, y) for any given positive integers x and y.
115+
* // Note that f(x, y) is increasing with respect to both x and y.
116+
* // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
117+
* public int f(int x, int y);
118+
* };
119+
*/
120+
121+
class Solution {
122+
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
123+
List<List<Integer>> res = new ArrayList<>();
124+
for (int i = 1; i <= 1000; ++i) {
125+
int left = 1, right = 1000;
126+
while (left < right) {
127+
int mid = (left + right) >> 1;
128+
if (customfunction.f(i, mid) >= z) {
129+
right = mid;
130+
} else {
131+
left = mid + 1;
132+
}
133+
}
134+
if (customfunction.f(i, left) == z) {
135+
res.add(Arrays.asList(i, left));
136+
}
137+
}
138+
return res;
139+
}
140+
}
141+
```
142+
143+
### **C++**
144+
145+
```cpp
146+
/*
147+
* // This is the custom function interface.
148+
* // You should not implement it, or speculate about its implementation
149+
* class CustomFunction {
150+
* public:
151+
* // Returns f(x, y) for any given positive integers x and y.
152+
* // Note that f(x, y) is increasing with respect to both x and y.
153+
* // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
154+
* int f(int x, int y);
155+
* };
156+
*/
157+
158+
class Solution {
159+
public:
160+
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
161+
vector<vector<int>> res;
162+
for (int i = 1; i <= 1000; ++i) {
163+
int left = 1, right = 1000;
164+
while (left < right) {
165+
int mid = left + right >> 1;
166+
if (customfunction.f(i, mid) >= z) {
167+
right = mid;
168+
} else {
169+
left = mid + 1;
170+
}
171+
}
172+
if (customfunction.f(i, left) == z) {
173+
res.push_back({i, left});
174+
}
175+
}
176+
return res;
177+
}
178+
};
179+
```
84180
181+
### **Go**
182+
183+
```go
184+
/**
185+
* This is the declaration of customFunction API.
186+
* @param x int
187+
* @param x int
188+
* @return Returns f(x, y) for any given positive integers x and y.
189+
* Note that f(x, y) is increasing with respect to both x and y.
190+
* i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
191+
*/
192+
193+
func findSolution(customFunction func(int, int) int, z int) [][]int {
194+
res := [][]int{}
195+
for i := 1; i <= 1000; i++ {
196+
left, right := 1, 1000
197+
for left < right {
198+
mid := (left + right) >> 1
199+
if customFunction(i, mid) >= z {
200+
right = mid
201+
} else {
202+
left = mid + 1
203+
}
204+
}
205+
if customFunction(i, left) == z {
206+
res = append(res, []int{i, left})
207+
}
208+
}
209+
return res
210+
}
85211
```
86212

87213
### **...**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* // This is the custom function interface.
3+
* // You should not implement it, or speculate about its implementation
4+
* class CustomFunction {
5+
* public:
6+
* // Returns f(x, y) for any given positive integers x and y.
7+
* // Note that f(x, y) is increasing with respect to both x and y.
8+
* // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
9+
* int f(int x, int y);
10+
* };
11+
*/
12+
13+
class Solution {
14+
public:
15+
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
16+
vector<vector<int>> res;
17+
for (int i = 1; i <= 1000; ++i) {
18+
int left = 1, right = 1000;
19+
while (left < right) {
20+
int mid = left + right >> 1;
21+
if (customfunction.f(i, mid) >= z) {
22+
right = mid;
23+
} else {
24+
left = mid + 1;
25+
}
26+
}
27+
if (customfunction.f(i, left) == z) {
28+
res.push_back({i, left});
29+
}
30+
}
31+
return res;
32+
}
33+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* This is the declaration of customFunction API.
3+
* @param x int
4+
* @param x int
5+
* @return Returns f(x, y) for any given positive integers x and y.
6+
* Note that f(x, y) is increasing with respect to both x and y.
7+
* i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
8+
*/
9+
10+
func findSolution(customFunction func(int, int) int, z int) [][]int {
11+
res := [][]int{}
12+
for i := 1; i <= 1000; i++ {
13+
left, right := 1, 1000
14+
for left < right {
15+
mid := (left + right) >> 1
16+
if customFunction(i, mid) >= z {
17+
right = mid
18+
} else {
19+
left = mid + 1
20+
}
21+
}
22+
if customFunction(i, left) == z {
23+
res = append(res, []int{i, left})
24+
}
25+
}
26+
return res
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* // This is the custom function interface.
3+
* // You should not implement it, or speculate about its implementation
4+
* class CustomFunction {
5+
* // Returns f(x, y) for any given positive integers x and y.
6+
* // Note that f(x, y) is increasing with respect to both x and y.
7+
* // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
8+
* public int f(int x, int y);
9+
* };
10+
*/
11+
12+
class Solution {
13+
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
14+
List<List<Integer>> res = new ArrayList<>();
15+
for (int i = 1; i <= 1000; ++i) {
16+
int left = 1, right = 1000;
17+
while (left < right) {
18+
int mid = (left + right) >> 1;
19+
if (customfunction.f(i, mid) >= z) {
20+
right = mid;
21+
} else {
22+
left = mid + 1;
23+
}
24+
}
25+
if (customfunction.f(i, left) == z) {
26+
res.add(Arrays.asList(i, left));
27+
}
28+
}
29+
return res;
30+
}
31+
}

0 commit comments

Comments
 (0)