Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 78d7799

Browse files
add binary search solution for 1237
1 parent db79189 commit 78d7799

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/main/java/com/fishercoder/solutions/_1237.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,25 @@ public static class Solution3 {
9696
* binary search
9797
*
9898
* Time: O(xlogy)
99-
* Space: O(1)*/
99+
* Space: O(1)
100+
* */
100101
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
101102
List<List<Integer>> result = new ArrayList<>();
103+
for (int x = 1; x <= 1000; x++) {
104+
int left = 1;
105+
int right = 1001;
106+
while (left < right) {
107+
int y = (left + right) / 2;
108+
if (customfunction.f(x, y) < z) {
109+
left = y + 1;
110+
} else {
111+
right = y;
112+
}
113+
}
114+
if (customfunction.f(x, left) == z) {
115+
result.add(Arrays.asList(x, left));
116+
}
117+
}
102118
return result;
103119
}
104120
}

0 commit comments

Comments
 (0)