update README.md + update tests + add solution for hw2:3839
All checks were successful
Binary Search Test / test (push) Successful in 6s
All checks were successful
Binary Search Test / test (push) Successful in 6s
This commit is contained in:
@@ -14,4 +14,4 @@ jobs:
|
|||||||
javac -d out $(find java common -name "*.java")
|
javac -d out $(find java common -name "*.java")
|
||||||
- name: Run Binary Search tests
|
- name: Run Binary Search tests
|
||||||
run: |
|
run: |
|
||||||
java -ea -cp out search.BinarySearchTest Base 3637
|
java -ea -cp out search.BinarySearchTest Base 3637 3839
|
||||||
|
|||||||
@@ -15,6 +15,14 @@
|
|||||||
* Требуется вывести число элементов массива, равных `x`.
|
* Требуется вывести число элементов массива, равных `x`.
|
||||||
* Не допускается использование типов `long` и `BigInteger`.
|
* Не допускается использование типов `long` и `BigInteger`.
|
||||||
* Класс должен иметь имя `BinarySearch3637`
|
* Класс должен иметь имя `BinarySearch3637`
|
||||||
|
* *3839* ✅
|
||||||
|
* На вход подаётся число `x` и массив, отсортированный по невозрастанию.
|
||||||
|
* Требуется вывести два числа: начало и длину диапазона элементов, равных `x`.
|
||||||
|
Если таких элементов нет, то следует вывести
|
||||||
|
пустой диапазон, у которого левая граница совпадает с местом
|
||||||
|
вставки элемента `x`.
|
||||||
|
* Не допускается использование типов `long` и `BigInteger`.
|
||||||
|
* Класс должен иметь имя `BinarySearch3839`
|
||||||
|
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
|
|||||||
Binary file not shown.
77
java/search/BinarySearch3839.java
Normal file
77
java/search/BinarySearch3839.java
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package search;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikita Doschennikov (me@fymio.us)
|
||||||
|
*/
|
||||||
|
public class BinarySearch3839 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
IntList a = new IntList();
|
||||||
|
int x = Integer.parseInt(args[0]);
|
||||||
|
int n = args.length;
|
||||||
|
for (int i = 1; i < n; i++) {
|
||||||
|
a.put(Integer.parseInt(args[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
int leftBound = leftBoundIterative(x, a);
|
||||||
|
int rightBound = rightBoundIterative(x, a);
|
||||||
|
int range = rightBound - leftBound;
|
||||||
|
System.out.println(leftBound + " " + range);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int leftBoundIterative(int x, IntList a) {
|
||||||
|
int low = 0,
|
||||||
|
high = a.getLength() - 1;
|
||||||
|
while (low <= high) {
|
||||||
|
int mid = low + (high - low) / 2;
|
||||||
|
if (a.get(mid) > x) {
|
||||||
|
low = mid + 1;
|
||||||
|
} else {
|
||||||
|
high = mid - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return high + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int rightBoundIterative(int x, IntList a) {
|
||||||
|
int low = 0,
|
||||||
|
high = a.getLength() - 1;
|
||||||
|
while (low <= high) {
|
||||||
|
int mid = low + (high - low) / 2;
|
||||||
|
if (a.get(mid) >= x) {
|
||||||
|
low = mid + 1;
|
||||||
|
} else {
|
||||||
|
high = mid - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return high + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int leftBoundRecursive(int x, IntList a, int low, int high) {
|
||||||
|
if (low > high) {
|
||||||
|
return high + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mid = low + (high - low) / 2;
|
||||||
|
|
||||||
|
if (a.get(mid) > x) {
|
||||||
|
return leftBoundRecursive(x, a, mid + 1, high);
|
||||||
|
} else {
|
||||||
|
return leftBoundRecursive(x, a, low, mid - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int rightBoundRecursive(int x, IntList a, int low, int high) {
|
||||||
|
if (low > high) {
|
||||||
|
return high + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mid = low + (high - low) / 2;
|
||||||
|
|
||||||
|
if (a.get(mid) >= x) {
|
||||||
|
return rightBoundRecursive(x, a, mid + 1, high);
|
||||||
|
} else {
|
||||||
|
return rightBoundRecursive(x, a, low, mid - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,11 +37,21 @@ public final class BinarySearchTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// === 3839
|
||||||
|
|
||||||
|
private static String span(final int c, final int x, final int[] a) {
|
||||||
|
final int begin = range(0, a.length).filter(i -> a[i] == x || Integer.compare(x, a[i]) == c).findFirst().orElse(a.length);
|
||||||
|
final long length = range(0, a.length).filter(i -> a[i] == x).count();
|
||||||
|
return begin + " " + length;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// === Common code
|
// === Common code
|
||||||
|
|
||||||
public static final Selector SELECTOR = new Selector(BinarySearchTest.class)
|
public static final Selector SELECTOR = new Selector(BinarySearchTest.class)
|
||||||
.variant("Base", Solver.variant0("", Kind.DESC, BinarySearchTest::base))
|
.variant("Base", Solver.variant0("", Kind.DESC, BinarySearchTest::base))
|
||||||
.variant("3637", Solver.variant0("3637", Kind.DESC, BinarySearchTest::count))
|
.variant("3637", Solver.variant0("3637", Kind.DESC, BinarySearchTest::count))
|
||||||
|
.variant("3839", Solver.variant0("3839", Kind.DESC, BinarySearchTest::span))
|
||||||
;
|
;
|
||||||
|
|
||||||
public static void main(final String... args) {
|
public static void main(final String... args) {
|
||||||
|
|||||||
Reference in New Issue
Block a user