Skip to content

Commit

Permalink
Added weekend Contest
Browse files Browse the repository at this point in the history
  • Loading branch information
neerazz committed Jun 22, 2020
1 parent 283c3bd commit 201fcf2
Show file tree
Hide file tree
Showing 34 changed files with 1,318 additions and 434 deletions.
43 changes: 31 additions & 12 deletions Algorithms/Neeraj/EPIJudge/epi_judge_java/epi/ABSqrt2.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
package epi;

import epi.test_framework.EpiTest;
import epi.test_framework.GenericTest;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

public class ABSqrt2 {
@EpiTest(testDataFile = "a_b_sqrt2.tsv")

public static List<Double> generateFirstKABSqrt2(int k) {
// TODO - you fill in here.
return null;
}
@EpiTest(testDataFile = "a_b_sqrt2.tsv")
public static List<Double> generateFirstKABSqrt2(int k) {
SortedSet<int[]> set = new TreeSet<>((i1, i2) -> Double.compare(getABSqrt2(i1), getABSqrt2(i2)));
set.add(new int[]{0, 0});
List<Double> op = new ArrayList<>();
while (op.size() < k) {
int[] first = set.first();
op.add(getABSqrt2(first));
set.add(new int[]{first[0] + 1, first[1]});
set.add(new int[]{first[0], first[1] + 1});
set.remove(first);
}
return op;
}

private static double getABSqrt2(int[] val) {
return val[0] + (val[1] * Math.sqrt(2));
}

public static void main(String[] args) {
System.exit(
GenericTest
.runFromAnnotations(args, "ABSqrt2.java",
new Object() {}.getClass().getEnclosingClass())
.ordinal());
}
public static void main(String[] args) {
System.exit(
GenericTest
.runFromAnnotations(args, "ABSqrt2.java",
new Object() {
}.getClass().getEnclosingClass())
.ordinal());
}
}
35 changes: 22 additions & 13 deletions Algorithms/Neeraj/EPIJudge/epi_judge_java/epi/BstFromPreorder.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package epi;

import epi.test_framework.EpiTest;
import epi.test_framework.GenericTest;

import java.util.List;

public class BstFromPreorder {
@EpiTest(testDataFile = "bst_from_preorder.tsv")
@EpiTest(testDataFile = "bst_from_preorder.tsv")

public static BstNode<Integer>
rebuildBSTFromPreorder(List<Integer> preorderSequence) {
// TODO - you fill in here.
return null;
}
public static BstNode<Integer> rebuildBSTFromPreorder(List<Integer> list) {
if (list.isEmpty()) return null;
BstNode<Integer> node = new BstNode<>(list.get(0));
int right = 1, len = list.size();
while (right < len && list.get(0) >= list.get(right)) {
right++;
}
node.left = rebuildBSTFromPreorder(list.subList(1, right));
node.right = rebuildBSTFromPreorder(list.subList(right, len));
return node;
}

public static void main(String[] args) {
System.exit(
GenericTest
.runFromAnnotations(args, "BstFromPreorder.java",
new Object() {}.getClass().getEnclosingClass())
.ordinal());
}
public static void main(String[] args) {
System.exit(
GenericTest
.runFromAnnotations(args, "BstFromPreorder.java",
new Object() {
}.getClass().getEnclosingClass())
.ordinal());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,32 @@
import java.util.List;
public class BstFromSortedArray {

public static BstNode<Integer>
buildMinHeightBSTFromSortedArray(List<Integer> A) {
// TODO - you fill in here.
return null;
}
@EpiTest(testDataFile = "bst_from_sorted_array.tsv")
public static int
buildMinHeightBSTFromSortedArrayWrapper(TimedExecutor executor,
List<Integer> A) throws Exception {
BstNode<Integer> result =
executor.run(() -> buildMinHeightBSTFromSortedArray(A));
public static BstNode<Integer>
buildMinHeightBSTFromSortedArray(List<Integer> A) {
// TODO - you fill in here.
return null;
}

List<Integer> inorder = BinaryTreeUtils.generateInorder(result);
@EpiTest(testDataFile = "bst_from_sorted_array.tsv")
public static int
buildMinHeightBSTFromSortedArrayWrapper(TimedExecutor executor,
List<Integer> A) throws Exception {
BstNode<Integer> result =
executor.run(() -> buildMinHeightBSTFromSortedArray(A));

TestUtils.assertAllValuesPresent(A, inorder);
BinaryTreeUtils.assertTreeIsBst(result);
return BinaryTreeUtils.binaryTreeHeight(result);
}
List<Integer> inorder = BinaryTreeUtils.generateInorder(result);

public static void main(String[] args) {
System.exit(
GenericTest
.runFromAnnotations(args, "BstFromSortedArray.java",
new Object() {}.getClass().getEnclosingClass())
.ordinal());
}
TestUtils.assertAllValuesPresent(A, inorder);
BinaryTreeUtils.assertTreeIsBst(result);
return BinaryTreeUtils.binaryTreeHeight(result);
}

public static void main(String[] args) {
System.exit(
GenericTest
.runFromAnnotations(args, "BstFromSortedArray.java",
new Object() {
}.getClass().getEnclosingClass())
.ordinal());
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,58 @@
package epi;

import epi.test_framework.EpiTest;
import epi.test_framework.EpiTestComparator;
import epi.test_framework.GenericTest;

import java.util.Collections;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.*;

public class EnumerateBalancedParentheses {
@EpiTest(testDataFile = "enumerate_balanced_parentheses.tsv")

public static List<String> generateBalancedParentheses(int numPairs) {
// TODO - you fill in here.
return null;
}
@EpiTestComparator
public static boolean comp(List<String> expected, List<String> result) {
if (result == null) {
return false;

static Set<String> set;
static Map<String, List<String>> memo;

@EpiTest(testDataFile = "enumerate_balanced_parentheses.tsv")
public static List<String> generateBalancedParentheses(int numPairs) {
set = new HashSet<>();
memo = new HashMap<>();
helper_topToBottom(numPairs, 0, 0, "");
return new ArrayList<>(set);
// return helper_buttomToTop(numPairs, 0, 0);
}

private static void helper_buttomToTop(int numPairs, int open, int close) {
}

private static void helper_topToBottom(int numPairs, int open, int close, String soFar) {
if (numPairs < open || numPairs < close) return;
if (numPairs == open && numPairs == close) {
set.add(soFar);
} else {
if (open < numPairs) {
helper_topToBottom(numPairs, open + 1, close, soFar + "(");
}
if (close < open) {
helper_topToBottom(numPairs, open, close + 1, soFar + ")");
}
}
}

@EpiTestComparator
public static boolean comp(List<String> expected, List<String> result) {
if (result == null) {
return false;
}
Collections.sort(expected);
Collections.sort(result);
return expected.equals(result);
}

public static void main(String[] args) {
System.exit(
GenericTest
.runFromAnnotations(args, "EnumerateBalancedParentheses.java",
new Object() {
}.getClass().getEnclosingClass())
.ordinal());
}
Collections.sort(expected);
Collections.sort(result);
return expected.equals(result);
}

public static void main(String[] args) {
System.exit(
GenericTest
.runFromAnnotations(args, "EnumerateBalancedParentheses.java",
new Object() {}.getClass().getEnclosingClass())
.ordinal());
}
}
28 changes: 28 additions & 0 deletions Algorithms/Neeraj/EPIJudge/epi_judge_java/epi/HIndexII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package epi;

/**
* Created on: Jun 18, 2020
* Questions: https://leetcode.com/problems/h-index/
*/
public class HIndexII {
public static void main(String[] args) {
System.out.println(hIndex(new int[]{100}) + " should be [100]");
}

public static int hIndex(int[] c) {
int n = c.length;
int s = 0, e = n - 1;

while (s < e) {
int m = (s + e) / 2;
if (c[m] < n - m) {
s = m + 1;
} else {
e = m;
}
}

if (s < n && c[s] >= n - s) return n - s;
else return 0;
}
}
Loading

0 comments on commit 201fcf2

Please sign in to comment.