forked from prajwal126/FAANG
-
Notifications
You must be signed in to change notification settings - Fork 0
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
34 changed files
with
1,318 additions
and
434 deletions.
There are no files selected for viewing
43 changes: 31 additions & 12 deletions
43
Algorithms/Neeraj/EPIJudge/epi_judge_java/epi/ABSqrt2.java
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 |
---|---|---|
@@ -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
35
Algorithms/Neeraj/EPIJudge/epi_judge_java/epi/BstFromPreorder.java
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 |
---|---|---|
@@ -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()); | ||
} | ||
} |
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
75 changes: 50 additions & 25 deletions
75
Algorithms/Neeraj/EPIJudge/epi_judge_java/epi/EnumerateBalancedParentheses.java
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 |
---|---|---|
@@ -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
28
Algorithms/Neeraj/EPIJudge/epi_judge_java/epi/HIndexII.java
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,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; | ||
} | ||
} |
Oops, something went wrong.