This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
74 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package company.vk.polis.ads.heap; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
/** | ||
* Class that allows us to get k max elements ordered descending in source list. | ||
*/ | ||
public final class TopK { | ||
/** | ||
* Top-K with O(n log(k)) complexity. | ||
* | ||
* @param elems input elements iterator | ||
* @param k amount of max values to carry out | ||
* @param <T> type of elements | ||
* @return list with k max elements ordered descending | ||
*/ | ||
public <T extends Comparable<T>> List<T> topK(Iterator<T> elems, int k) { | ||
throw new UnsupportedOperationException("Implement me"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package company.vk.polis.ads.heap; | ||
|
||
import it.unimi.dsi.fastutil.ints.IntArrayList; | ||
import it.unimi.dsi.fastutil.ints.IntComparators; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class TopKTest { | ||
private static final int SEQUENCE_SIZE = nextInt(150_000_000, 200_000_000); | ||
private static final int TOP_K = nextInt(7_500_000, 10_000_000); | ||
private static final int TOP_K_MIN_VALUE_BOUND = nextInt(Integer.MAX_VALUE / 2 - 1000, Integer.MAX_VALUE / 2); | ||
|
||
@Test | ||
void testTopK() { | ||
var expected = new IntArrayList(TOP_K); | ||
var sequence = IntStream.concat( | ||
IntStream.generate(() -> nextInt(0, TOP_K_MIN_VALUE_BOUND)) | ||
.limit(SEQUENCE_SIZE - TOP_K), | ||
IntStream.generate(() -> { | ||
int i = nextInt(TOP_K_MIN_VALUE_BOUND, Integer.MAX_VALUE); | ||
expected.add(i); | ||
return i; | ||
}).limit(TOP_K) | ||
).iterator(); | ||
var actual = new TopK().topK(sequence, TOP_K); | ||
expected.sort(IntComparators.OPPOSITE_COMPARATOR); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
private static int nextInt(int from, int to) { | ||
return ThreadLocalRandom.current().nextInt(to - from) + from; | ||
} | ||
} |