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
public class IndexElementArray { | |
public static void main(String[] args) { | |
System.out.println(indexElement(new int[] {-5, -3, 0, 3, 5})); //Expect True | |
System.out.println(indexElement(new int[] {-5, -3, 0, 1, 2})); //Expect false | |
System.out.println(indexElement(new int[] {0, 2, 3, 4, 5})); //Expect True | |
System.out.println(indexElement(new int[] {-1, 0, 1, 2, 4})); //Expect True | |
System.out.println(indexElement(new int[] {-10, 10, 20, 30, 40})); //Expect false | |
} | |
private static boolean indexElement(int []arr) { |
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
public class Main { | |
public static void main(String[] args) { | |
System.out.println(maxElementInUnimodalArray(new int[]{1, 2, 5, 4, 3})); //Expected 5 | |
System.out.println(maxElementInUnimodalArray(new int[]{1, 2, 3, 4, 5})); //Expected 5 | |
System.out.println(maxElementInUnimodalArray(new int[]{5, 4, 3, 2, 1})); //Expected 5 | |
System.out.println(maxElementInUnimodalArray(new int[]{6})); //Expected 6 | |
} | |
private static int maxElementInUnimodalArray(int[] arr) { | |
if (arr == null || arr.length == 0) { |
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
import java.util.Stack; | |
public class ExpressionEvaluation { | |
public static void main(String[] args) { | |
String expression = "add( 1, 2)"; | |
int result = evaluateExpression(expression); | |
System.out.println(result); | |
expression = "add(sub(2, 1), 3)"; | |
System.out.println(evaluateExpression(expression)); | |
expression = "add(3, sub(22, 20))"; |
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
import java.util.HashMap; | |
import java.util.Map; | |
public class RomanNumbers { | |
public static void main(String[] args) { | |
for (int i = 1; i <= 50; i++) { | |
String roman = intToRoman(i); | |
System.out.println(roman + " = " + romanToInt(roman)); | |
} |
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
public class RotatedSortedArray { | |
public static void main(String[] args) { | |
testFind(); | |
} | |
public static void testFind() { | |
System.out.println(find(null, 10));// Expect -1 | |
int[] arr1 = new int[]{}; | |
System.out.println(find(arr1, 100));// Expect -1 | |
int[] arr2 = new int[]{10}; |
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
import java.util.HashSet; | |
import java.util.Map; | |
import java.util.Objects; | |
import java.util.Set; | |
public class PairsTest { | |
static class IntPair { | |
int x, y; | |
public IntPair(int a, int b) { | |
this.x = a; |
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
def permutations(lst): | |
return permutations_util(lst, 0) | |
def permutations_util(lst, index): | |
perms = [] | |
if index == len(lst) - 1: | |
perms.append(lst.copy()) | |
for i in range(index, len(lst)): | |
lst[i], lst[index] = lst[index], lst[i] |
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
import java.util.HashMap; | |
import java.util.Map; | |
public class LongestSubarrayUnique { | |
public static void main(String[] args) { | |
System.out.println(longestDistinctSubarray(null)); //Expected 0 | |
System.out.println(longestDistinctSubarray(new int[]{})); //Expected 0 | |
System.out.println(longestDistinctSubarray(new int[]{1})); //Expected 1 | |
System.out.println(longestDistinctSubarray(new int[]{1, 2, 3, 4, 5})); //Expected 5 | |
System.out.println(longestDistinctSubarray(new int[]{23, 9, 36, 23, 4})); //Expected 4 |
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
def countBST(n): | |
""" | |
:param n: a positive integer | |
:return: Number of all possible binary search trees (BST) with n unique numbers | |
The number is called a Catalan number F(n) = C(2*n, n) / (n+1) = (2*n)! / (n+1)! * n! | |
As the above formula involves calculating factorial which is laborious and can overflow in some cases, | |
We can use the following recurrence relation to make the calculation simple. | |
F(0) = 1 | |
F(n+1) = (2*(2*n + 1) / (n+2)) * F(n) | |
""" |
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
import java.util.Arrays; | |
public class LongestIncreasingSubsequence { | |
public static void main(String[] args) { | |
System.out.println(longestIncreasingSubsequence(null)); //Expected 0 | |
System.out.println(longestIncreasingSubsequence(new int[]{})); //Expected 0 | |
System.out.println(longestIncreasingSubsequence(new int[]{1})); //Expected 1 | |
System.out.println(longestIncreasingSubsequence(new int[]{1, 5, 10})); //Expected 3 | |
System.out.println(longestIncreasingSubsequence(new int[]{1, 5, 2, 7, 6, 8})); //Expected 4 | |
System.out.println(longestIncreasingSubsequence(new int[]{6, 1, 7, 3, 9, 4, 11, 13})); //Expected 5 |
NewerOlder