update the source from root pom and compile project
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.models;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class ListNode {
|
||||
public int val;
|
||||
public ListNode next;
|
||||
|
||||
public ListNode(int x) {
|
||||
val = x;
|
||||
next = null;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.models;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left, right;
|
||||
|
||||
public TreeNode(int val) {
|
||||
this.val = val;
|
||||
this.left = this.right = null;
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.ossez.lang.tutorial.usecases;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* variable arguments use case
|
||||
*/
|
||||
public class VarargsCase {
|
||||
private static final Logger logger = LoggerFactory.getLogger(VarargsCase.class);
|
||||
|
||||
/**
|
||||
* sumVarargs
|
||||
*
|
||||
* @param intArrays
|
||||
* @return
|
||||
*/
|
||||
static int sumVarargs(int... intArrays) {
|
||||
int sum, i;
|
||||
sum = 0;
|
||||
for (i = 0; i < intArrays.length; i++) {
|
||||
sum += intArrays[i];
|
||||
}
|
||||
return (sum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main Function
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
int sum = 0;
|
||||
sum = sumVarargs(new int[]{10, 12, 33, 7});
|
||||
logger.debug("The Sum of the arrays: {}", sum);
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package com.ossez.codebank.algorithml.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.ossez.codebank.algorithm.models.TreeNode;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class TreeUtils {
|
||||
|
||||
public static TreeNode initTree(String data) {
|
||||
// NULL CHECK
|
||||
if (data.equals("{}")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ArrayList<TreeNode> treeList = new ArrayList<TreeNode>();
|
||||
|
||||
data = data.replace("{", "");
|
||||
data = data.replace("}", "");
|
||||
String[] vals = data.split(",");
|
||||
|
||||
// INSERT ROOT
|
||||
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
|
||||
treeList.add(root);
|
||||
|
||||
int index = 0;
|
||||
boolean isLeftChild = true;
|
||||
for (int i = 1; i < vals.length; i++) {
|
||||
if (!vals[i].equals("#")) {
|
||||
TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
|
||||
if (isLeftChild) {
|
||||
treeList.get(index).left = node;
|
||||
} else {
|
||||
treeList.get(index).right = node;
|
||||
}
|
||||
treeList.add(node);
|
||||
}
|
||||
|
||||
// LEVEL
|
||||
if (!isLeftChild) {
|
||||
index++;
|
||||
}
|
||||
|
||||
// MOVE TO RIGHT OR NEXT LEVEL
|
||||
isLeftChild = !isLeftChild;
|
||||
}
|
||||
|
||||
return root;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
package com.ossez.codebank.interview;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Count+Up+Down
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class KayakCountUpDown {
|
||||
private final static Logger logger = LoggerFactory.getLogger(KayakCountUpDown.class);
|
||||
|
||||
static int minNumber = 0;
|
||||
static int maxNumber = 0;
|
||||
int tmpN = 0;
|
||||
List<Integer> retList = new ArrayList<Integer>();
|
||||
|
||||
/**
|
||||
*
|
||||
* @param start
|
||||
* @param end
|
||||
* @return
|
||||
*/
|
||||
public List<Integer> countUp(int start, int end) {
|
||||
logger.debug("BEGIN");
|
||||
maxNumber = end;
|
||||
tmpN = start;
|
||||
moveUp(0);
|
||||
retList.add(end);
|
||||
return retList;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param start
|
||||
* @param end
|
||||
* @return
|
||||
*/
|
||||
public List<Integer> countUpDown(int start, int end) {
|
||||
logger.debug("BEGIN");
|
||||
minNumber = start;
|
||||
maxNumber = end;
|
||||
tmpN = start;
|
||||
|
||||
moveUp(0);
|
||||
retList.add(end);
|
||||
|
||||
moveDown(1);
|
||||
return retList;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param n
|
||||
*/
|
||||
private void moveUp(int n) {
|
||||
retList.add(tmpN);
|
||||
tmpN++;
|
||||
if (tmpN != maxNumber) {
|
||||
moveUp(tmpN + 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param n
|
||||
*/
|
||||
private void moveDown(int n) {
|
||||
tmpN = (maxNumber - n);
|
||||
retList.add(tmpN);
|
||||
|
||||
if (tmpN != minNumber) {
|
||||
moveDown(n + 1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.ossez.codebank.interview;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Robot+Movement
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class KayakRobotMovement {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(KayakRobotMovement.class);
|
||||
|
||||
/**
|
||||
* Get coordinates for Robot Movement
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
public static String getCoordinates(String data) {
|
||||
logger.debug("BEGIN");
|
||||
|
||||
String retStr = "";
|
||||
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int[][] move = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
|
||||
int dir = 0;
|
||||
|
||||
for (char ch : data.toCharArray()) {
|
||||
if (ch == 'F') {
|
||||
x += move[dir][0];
|
||||
y += move[dir][1];
|
||||
} else if (ch == 'L') {
|
||||
dir--;
|
||||
} else if (ch == 'R') {
|
||||
dir++;
|
||||
}
|
||||
dir = (dir + 4) % 4;
|
||||
}
|
||||
retStr = x + "," + y;
|
||||
|
||||
return retStr;
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package com.ossez.codebank.interview;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Next+Fibonacci+Number
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class ManNextFibonacciNumber {
|
||||
private final static Logger logger = LoggerFactory.getLogger(ManNextFibonacciNumber.class);
|
||||
|
||||
public static void main(String[] args) throws java.lang.Exception {
|
||||
int fArray[] = new int[60];
|
||||
|
||||
for (int i = 0; i < 60; i++) {
|
||||
fArray[i] = getFib(i);
|
||||
}
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
String input = br.readLine();
|
||||
// System.out.println(fib(Integer.valueOf(input)));
|
||||
|
||||
for (int i = 0; i < Integer.valueOf(input); i++) {
|
||||
Integer inputInt = Integer.valueOf(br.readLine());
|
||||
// System.out.println(inputInt);
|
||||
for (int j = 0; j < fArray.length; j++) {
|
||||
if (fArray[j] > inputInt) {
|
||||
// System.out.println(fArray[j]);
|
||||
logger.debug("{} Next Fibonacci [{}]", inputInt, fArray[j]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Fibonacci Number
|
||||
*
|
||||
* @param n
|
||||
* @return
|
||||
*/
|
||||
private static int getFib(int n) {
|
||||
if (n < 0) {
|
||||
return -1;
|
||||
} else if (n == 0) {
|
||||
return 0;
|
||||
} else if (n == 1 || n == 2) {
|
||||
return 1;
|
||||
} else {
|
||||
int[] fibAry = new int[n + 1];
|
||||
fibAry[0] = 0;
|
||||
fibAry[1] = fibAry[2] = 1;
|
||||
for (int i = 3; i <= n; i++) {
|
||||
fibAry[i] = fibAry[i - 1] + fibAry[i - 2];
|
||||
}
|
||||
return fibAry[n];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.tests;
|
||||
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class BitOperationTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(BitOperationTest.class);
|
||||
|
||||
/**
|
||||
* 35 https://www.lintcode.com/problem/reverse-linked-list/description
|
||||
*/
|
||||
@Test
|
||||
public void testInt2Bit() {
|
||||
logger.debug("BEGIN");
|
||||
System.out.println(Integer.toBinaryString(5));
|
||||
System.out.println(Integer.toBinaryString(2));
|
||||
|
||||
System.out.println(Integer.toBinaryString(2 << 2));
|
||||
|
||||
System.out.println(Integer.parseInt(Integer.toBinaryString(2 << 2), 2));
|
||||
|
||||
System.out.println(5 / 3);
|
||||
System.out.println(5 % 3);
|
||||
FastMath.pow(2, 3);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,293 +0,0 @@
|
||||
package com.ossez.lang.tutorial.tests;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.TreeSet;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.management.ListenerNotFoundException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ossez.lang.tutorial.models.ListNode;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class LintcodeTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(LintcodeTest.class);
|
||||
|
||||
/**
|
||||
* 35 https://www.lintcode.com/problem/reverse-linked-list/description
|
||||
*/
|
||||
@Test
|
||||
public void test0035Reverse() {
|
||||
// INIT LINKED LIST
|
||||
ListNode head = new ListNode(1);
|
||||
head.next = new ListNode(2);
|
||||
head.next.next = new ListNode(3);
|
||||
|
||||
// CHECK BEFORE
|
||||
System.out.println(head.val);
|
||||
System.out.println(head.next.val);
|
||||
System.out.println(head.next.next.val);
|
||||
|
||||
// REVERSE
|
||||
ListNode prev = null;
|
||||
while (head != null) {
|
||||
ListNode temp = head.next;
|
||||
head.next = prev;
|
||||
prev = head;
|
||||
head = temp;
|
||||
}
|
||||
|
||||
// CHECK AFTER
|
||||
System.out.println(prev.val);
|
||||
System.out.println(prev.next.val);
|
||||
System.out.println(prev.next.next.val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 1480 https://www.lintcode.com/problem/dot-product/description
|
||||
*/
|
||||
@Test
|
||||
public void test0044minSubArray() {
|
||||
|
||||
List<Integer> nums = new ArrayList<Integer>();
|
||||
nums.add(1);
|
||||
nums.add(1);
|
||||
|
||||
int min_ending_here = 0;
|
||||
int retStatus = 0;
|
||||
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
if (min_ending_here > 0) {
|
||||
min_ending_here = nums.get(i);
|
||||
} else {
|
||||
min_ending_here += nums.get(i);
|
||||
}
|
||||
retStatus = Math.min(retStatus, min_ending_here);
|
||||
}
|
||||
|
||||
System.out.println(retStatus);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 53 https://www.lintcode.com/problem/reverse-words-in-a-string/description
|
||||
*/
|
||||
@Test
|
||||
public void test0053ReverseWords() {
|
||||
|
||||
String s = " Life doesn't always give us the joys we want.";
|
||||
|
||||
String retStr = "";
|
||||
String[] inStr = s.split(" ");
|
||||
|
||||
for (int i = inStr.length - 1; i >= 0; i--) {
|
||||
String cStr = inStr[i].trim();
|
||||
if (!cStr.isEmpty()) {
|
||||
retStr = retStr + " " + cStr;
|
||||
}
|
||||
}
|
||||
retStr = retStr.trim();
|
||||
System.out.println(retStr);
|
||||
// return retStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 56 https://www.lintcode.com/problem/two-sum/description
|
||||
*/
|
||||
@Test
|
||||
public void test0056TwoSum() {
|
||||
int[] numbers = { 2, 7, 11, 15 };
|
||||
int target = 9;
|
||||
|
||||
int[] retArray = new int[2];
|
||||
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
int intA = numbers[i];
|
||||
int intB = 0;
|
||||
|
||||
for (int j = 1 + i; j < numbers.length; j++) {
|
||||
intB = numbers[j];
|
||||
// SUM CHECK
|
||||
if (target == intA + intB && i < j) {
|
||||
retArray[0] = i;
|
||||
retArray[1] = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(Arrays.toString(retArray));
|
||||
}
|
||||
|
||||
/**
|
||||
* 209 https://www.lintcode.com/problem/first-unique-character-in-a-string
|
||||
*/
|
||||
@Test
|
||||
public void test0209FirstUniqChar() {
|
||||
String str = "ddjdz";
|
||||
|
||||
char retStatus = 0;
|
||||
|
||||
// LOOP CHECK
|
||||
for (int i = 0; i < 30; i++) {
|
||||
char c = str.charAt(0);
|
||||
if (str.indexOf(Character.toString(c)) == str.lastIndexOf(Character.toString(c))) {
|
||||
retStatus = c;
|
||||
break;
|
||||
}
|
||||
str = str.replaceAll(Character.toString(c), "");
|
||||
}
|
||||
|
||||
System.out.println("" + retStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* 411
|
||||
*
|
||||
* <p>
|
||||
* <ul>
|
||||
* <li>@see
|
||||
* <a href="https://www.cwiki.us/display/ITCLASSIFICATION/Gray+Code">https://www.cwiki.us/display/ITCLASSIFICATION/Gray+Code</a>
|
||||
* <li>@see<a href="https://www.lintcode.com/problem/gray-code/description">https://www.lintcode.com/problem/gray-code/description</a>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void test0411GrayCode() {
|
||||
int n = 2;
|
||||
|
||||
List<Integer> retArray = new ArrayList<>();
|
||||
|
||||
if (n == 0) {
|
||||
retArray.add(0);
|
||||
}
|
||||
|
||||
for (int i = 0; i < (2 << (n - 1)); i++) {
|
||||
int g = i ^ (i / 2);
|
||||
retArray.add(g);
|
||||
}
|
||||
|
||||
System.out.println(retArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* 1480 https://www.lintcode.com/problem/dot-product/description
|
||||
*/
|
||||
@Test
|
||||
public void test0423IsValidParentheses() {
|
||||
String s = "([)]";
|
||||
|
||||
boolean retStatus = false;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
s = s.replace("()", "");
|
||||
s = s.replace("{}", "");
|
||||
s = s.replace("[]", "");
|
||||
|
||||
if (s.length() == 0) {
|
||||
retStatus = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(retStatus);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 646 https://www.lintcode.com/problem/first-position-unique-character/description
|
||||
*/
|
||||
@Test
|
||||
public void test0646FirstUniqChar() {
|
||||
String s = "saau";
|
||||
|
||||
int retStatus = -1;
|
||||
boolean breakLoop = false;
|
||||
|
||||
int[] iArray = new int[256];
|
||||
|
||||
// NULL CHECK
|
||||
if (s == null || s.length() == 0) {
|
||||
retStatus = -1;
|
||||
}
|
||||
|
||||
// LOOP CHECK
|
||||
for (char c : s.toCharArray()) {
|
||||
iArray[c]++;
|
||||
}
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
if (iArray[s.charAt(i)] == 1) {
|
||||
retStatus = i;
|
||||
breakLoop = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// LOOP BREAK CHECK
|
||||
if (!breakLoop) {
|
||||
retStatus = -1;
|
||||
}
|
||||
|
||||
System.out.println(retStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* 767 https://www.lintcode.com/problem/reverse-array/description
|
||||
*/
|
||||
@Test
|
||||
public void test0767ReverseArray() {
|
||||
int[] nums = { 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
for (int i = 0; i < nums.length / 2; i++) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[nums.length - 1 - i];
|
||||
nums[nums.length - 1 - i] = tmp;
|
||||
}
|
||||
|
||||
System.out.println(Arrays.toString(nums));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 1480 https://www.lintcode.com/problem/dot-product/description
|
||||
*/
|
||||
@Test
|
||||
public void test1377findSubstring() {
|
||||
String str = "";
|
||||
int k = 5;
|
||||
|
||||
HashSet<String> strSet = new HashSet<String>();
|
||||
|
||||
for (int i = 0; i <= str.length() - k; i++) {
|
||||
String subStr = str.substring(i, i + k);
|
||||
|
||||
String pattern = ".*(.).*\\1.*";
|
||||
|
||||
Pattern r = Pattern.compile(pattern);
|
||||
|
||||
Matcher m = r.matcher(subStr);
|
||||
if (!m.find()) {
|
||||
strSet.add(subStr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
System.out.println(strSet.size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.tests;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Eager Singleton
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
class EagerSingleton {
|
||||
private static final EagerSingleton INSTANCE = new EagerSingleton();
|
||||
|
||||
// Private constructor suppresses
|
||||
private EagerSingleton() {
|
||||
}
|
||||
|
||||
// default public constructor
|
||||
public static EagerSingleton getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazy Singleton
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
class LazySingleton {
|
||||
private static volatile LazySingleton INSTANCE = null;
|
||||
|
||||
// Private constructor suppresses
|
||||
// default LazySingleton constructor
|
||||
private LazySingleton() {
|
||||
}
|
||||
|
||||
// thread safe and performance promote
|
||||
public static LazySingleton getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
synchronized (LazySingleton.class) {
|
||||
// when more than two threads run into the first null check same time, to avoid instanced more than one time, it needs to be
|
||||
// checked again.
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new LazySingleton();
|
||||
}
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class SingletonTest {
|
||||
private final static Logger logger = LoggerFactory.getLogger(SingletonTest.class);
|
||||
|
||||
@Test
|
||||
public void testSingleton() {
|
||||
logger.debug("TEST Singleton");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.tests;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ossez.codebank.algorithm.models.TreeNode;
|
||||
import com.ossez.codebank.algorithml.utils.TreeUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class TreeTest {
|
||||
private final static Logger logger = LoggerFactory.getLogger(TreeTest.class);
|
||||
private static List<Integer> loopList = new ArrayList<Integer>();
|
||||
|
||||
@Test
|
||||
public void testMain() {
|
||||
logger.debug("TREE TEST");
|
||||
String data = "{1,2,3,4,5,#,6,#,#,7,8,#,#}";
|
||||
TreeNode treeNode = TreeUtils.initTree(data);
|
||||
|
||||
// PRE
|
||||
loopList = new ArrayList<Integer>();
|
||||
preOrderTraverselRecursion(treeNode);
|
||||
System.out.println(loopList);
|
||||
|
||||
// IN
|
||||
loopList = new ArrayList<Integer>();
|
||||
inOrderTraverselRecursion(treeNode);
|
||||
System.out.println(loopList);
|
||||
|
||||
// POST
|
||||
loopList = new ArrayList<Integer>();
|
||||
postOrderTraversalRecursion(treeNode);
|
||||
System.out.println(loopList);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
public void preOrderTraverselRecursion(TreeNode root) {
|
||||
if (root != null) {
|
||||
loopList.add(root.val);
|
||||
preOrderTraverselRecursion(root.left);
|
||||
preOrderTraverselRecursion(root.right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
public void inOrderTraverselRecursion(TreeNode root) {
|
||||
if (root != null) {
|
||||
inOrderTraverselRecursion(root.left);
|
||||
loopList.add(root.val);
|
||||
inOrderTraverselRecursion(root.right);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
public void postOrderTraversalRecursion(TreeNode root) {
|
||||
if (root != null) {
|
||||
postOrderTraversalRecursion(root.left);
|
||||
postOrderTraversalRecursion(root.right);
|
||||
loopList.add(root.val);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.tests.codility;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* More details about question see link below
|
||||
* <ul>
|
||||
* <li>@see <a href= "https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Gap">https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Gap</a>
|
||||
* </li>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class CodilityBinaryGapTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(CodilityBinaryGapTest.class);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testMain() {
|
||||
logger.debug("BEGIN");
|
||||
|
||||
int N = 529;
|
||||
String intStr = Integer.toBinaryString(N);
|
||||
|
||||
intStr = intStr.replace("1", "#1#");
|
||||
|
||||
String[] strArray = intStr.split("1");
|
||||
|
||||
int maxCount = 0;
|
||||
for (int i = 0; i < strArray.length; i++) {
|
||||
String checkStr = strArray[i];
|
||||
int countLength = 0;
|
||||
|
||||
if (checkStr.length() > 2 && checkStr.startsWith("#") && checkStr.endsWith("#")) {
|
||||
checkStr = checkStr.replace("#", "");
|
||||
countLength = checkStr.length();
|
||||
|
||||
if (maxCount < countLength) {
|
||||
maxCount = countLength;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("MAX COUNT: [{}]", maxCount);
|
||||
}
|
||||
|
||||
}
|
||||
-131
@@ -1,131 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.tests.lintcode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ossez.codebank.algorithm.models.TreeNode;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 7
|
||||
* <ul>
|
||||
* <li>@see <a href=
|
||||
* "https://www.cwiki.us/display/ITCLASSIFICATION/Serialize+and+Deserialize+Binary+Tree">https://www.cwiki.us/display/ITCLASSIFICATION/Serialize+and+Deserialize+Binary+Tree</a>
|
||||
* <li>@see<a href=
|
||||
* "https://www.lintcode.com/problem/serialize-and-deserialize-binary-tree">https://www.lintcode.com/problem/serialize-and-deserialize-binary-tree</a>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class LintCode0007SerializeAndDeserializeTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(LintCode0007SerializeAndDeserializeTest.class);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testMain() {
|
||||
logger.debug("BEGIN");
|
||||
String data = "{3,9,20,#,#,15,7}";
|
||||
|
||||
System.out.println(serialize(deserialize(data)));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize from array to tree
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
private TreeNode deserialize(String data) {
|
||||
// NULL CHECK
|
||||
if (data.equals("{}")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ArrayList<TreeNode> treeList = new ArrayList<TreeNode>();
|
||||
|
||||
data = data.replace("{", "");
|
||||
data = data.replace("}", "");
|
||||
String[] vals = data.split(",");
|
||||
|
||||
// INSERT ROOT
|
||||
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
|
||||
treeList.add(root);
|
||||
|
||||
int index = 0;
|
||||
boolean isLeftChild = true;
|
||||
for (int i = 1; i < vals.length; i++) {
|
||||
if (!vals[i].equals("#")) {
|
||||
TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
|
||||
if (isLeftChild) {
|
||||
treeList.get(index).left = node;
|
||||
} else {
|
||||
treeList.get(index).right = node;
|
||||
}
|
||||
treeList.add(node);
|
||||
}
|
||||
|
||||
// LEVEL
|
||||
if (!isLeftChild) {
|
||||
index++;
|
||||
}
|
||||
|
||||
// MOVE TO RIGHT OR NEXT LEVEL
|
||||
isLeftChild = !isLeftChild;
|
||||
}
|
||||
|
||||
return root;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
public String serialize(TreeNode root) {
|
||||
// write your code here
|
||||
if (root == null) {
|
||||
return "{}";
|
||||
}
|
||||
|
||||
ArrayList<TreeNode> queue = new ArrayList<TreeNode>();
|
||||
queue.add(root);
|
||||
|
||||
for (int i = 0; i < queue.size(); i++) {
|
||||
TreeNode node = queue.get(i);
|
||||
if (node == null) {
|
||||
continue;
|
||||
}
|
||||
queue.add(node.left);
|
||||
queue.add(node.right);
|
||||
}
|
||||
|
||||
while (queue.get(queue.size() - 1) == null) {
|
||||
queue.remove(queue.size() - 1);
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{");
|
||||
sb.append(queue.get(0).val);
|
||||
for (int i = 1; i < queue.size(); i++) {
|
||||
if (queue.get(i) == null) {
|
||||
sb.append(",#");
|
||||
} else {
|
||||
sb.append(",");
|
||||
sb.append(queue.get(i).val);
|
||||
}
|
||||
}
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
-124
@@ -1,124 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.tests.lintcode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ossez.codebank.algorithm.models.TreeNode;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 69
|
||||
* <ul>
|
||||
* <li>@see <a href=
|
||||
* "https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Tree+Level+Order+Traversal">https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Tree+Level+Order+Traversal</a>
|
||||
* <li>@see<a href=
|
||||
* "https://www.lintcode.com/problem/binary-tree-level-order-traversal">https://www.lintcode.com/problem/binary-tree-level-order-traversal</a>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class LintCode0069LevelOrderTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(LintCode0069LevelOrderTest.class);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testMain() {
|
||||
logger.debug("BEGIN");
|
||||
String data = "{3,9,20,#,#,15,7}";
|
||||
|
||||
TreeNode tn = deserialize(data);
|
||||
System.out.println(levelOrder(tn));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize from array to tree
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
private TreeNode deserialize(String data) {
|
||||
// NULL CHECK
|
||||
if (data.equals("{}")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ArrayList<TreeNode> treeList = new ArrayList<TreeNode>();
|
||||
|
||||
data = data.replace("{", "");
|
||||
data = data.replace("}", "");
|
||||
String[] vals = data.split(",");
|
||||
|
||||
// INSERT ROOT
|
||||
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
|
||||
treeList.add(root);
|
||||
|
||||
int index = 0;
|
||||
boolean isLeftChild = true;
|
||||
for (int i = 1; i < vals.length; i++) {
|
||||
if (!vals[i].equals("#")) {
|
||||
TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
|
||||
if (isLeftChild) {
|
||||
treeList.get(index).left = node;
|
||||
} else {
|
||||
treeList.get(index).right = node;
|
||||
}
|
||||
treeList.add(node);
|
||||
}
|
||||
|
||||
// LEVEL
|
||||
if (!isLeftChild) {
|
||||
index++;
|
||||
}
|
||||
|
||||
// MOVE TO RIGHT OR NEXT LEVEL
|
||||
isLeftChild = !isLeftChild;
|
||||
}
|
||||
|
||||
return root;
|
||||
|
||||
}
|
||||
|
||||
private List<List<Integer>> levelOrder(TreeNode root) {
|
||||
Queue<TreeNode> queue = new LinkedList<TreeNode>();
|
||||
List<List<Integer>> rs = new ArrayList<List<Integer>>();
|
||||
|
||||
// NULL CHECK
|
||||
if (root == null) {
|
||||
return rs;
|
||||
}
|
||||
|
||||
queue.offer(root);
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
int length = queue.size();
|
||||
List<Integer> list = new ArrayList<Integer>();
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
TreeNode curTN = queue.poll();
|
||||
list.add(curTN.val);
|
||||
if (curTN.left != null) {
|
||||
queue.offer(curTN.left);
|
||||
}
|
||||
if (curTN.right != null) {
|
||||
queue.offer(curTN.right);
|
||||
}
|
||||
}
|
||||
|
||||
rs.add(list);
|
||||
}
|
||||
|
||||
return rs;
|
||||
}
|
||||
}
|
||||
-63
@@ -1,63 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.tests.lintcode;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ossez.codebank.algorithm.models.ListNode;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 102
|
||||
* <ul>
|
||||
* <li>@see <a href=
|
||||
* "https://www.cwiki.us/display/ITCLASSIFICATION/Linked+List+Cycle">https://www.cwiki.us/display/ITCLASSIFICATION/Linked+List+Cycle</a>
|
||||
* <li>@see<a href= "https://www.lintcode.com/problem/linked-list-cycle/">https://www.lintcode.com/problem/linked-list-cycle/</a>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class LintCode0102HasCycleTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(LintCode0102HasCycleTest.class);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testMain() {
|
||||
logger.debug("BEGIN");
|
||||
// INIT LINKED LIST
|
||||
ListNode head = new ListNode(1);
|
||||
head.next = new ListNode(2);
|
||||
head.next.next = new ListNode(3);
|
||||
head.next.next.next = new ListNode(4);
|
||||
|
||||
// CREATE A LOOP
|
||||
head.next.next.next.next = head.next.next.next;
|
||||
|
||||
boolean retResult = false;
|
||||
|
||||
// LIKED LIST MAY NULL:
|
||||
if (!(head == null || head.next == null)) {
|
||||
ListNode s = head;
|
||||
ListNode f = head.next;
|
||||
|
||||
while (f.next != null && f.next.next != null) {
|
||||
|
||||
s = s.next;
|
||||
f = f.next.next;
|
||||
|
||||
if (f == s) {
|
||||
retResult = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(retResult);
|
||||
|
||||
}
|
||||
}
|
||||
-89
@@ -1,89 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.tests.lintcode;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ossez.codebank.algorithm.models.ListNode;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 102
|
||||
* <ul>
|
||||
* <li>@see <a href=
|
||||
* "https://www.cwiki.us/display/ITCLASSIFICATION/Linked+List+Cycle">https://www.cwiki.us/display/ITCLASSIFICATION/Linked+List+Cycle</a>
|
||||
* <li>@see<a href= "https://www.lintcode.com/problem/linked-list-cycle/">https://www.lintcode.com/problem/linked-list-cycle/</a>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class LintCode0165MergeTwoListsTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(LintCode0165MergeTwoListsTest.class);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testMain() {
|
||||
logger.debug("BEGIN");
|
||||
// INIT LINKED LIST l1
|
||||
ListNode l1 = new ListNode(1);
|
||||
l1.next = new ListNode(3);
|
||||
l1.next.next = new ListNode(8);
|
||||
l1.next.next.next = new ListNode(11);
|
||||
l1.next.next.next.next = new ListNode(15);
|
||||
|
||||
// INIT LINKED LIST l2
|
||||
ListNode l2 = new ListNode(2);
|
||||
|
||||
// RETURN RESULT
|
||||
ListNode retResult = new ListNode(0);
|
||||
|
||||
// NULL CHECK
|
||||
// if (l1 == null && l2 == null) {
|
||||
// retResult = null;
|
||||
// }
|
||||
//
|
||||
// if (l1 == null) {
|
||||
// retResult = l2;
|
||||
// }
|
||||
//
|
||||
// if (l2 == null) {
|
||||
// retResult = l1;
|
||||
// }
|
||||
|
||||
// MERGE
|
||||
retResult = new ListNode(0);
|
||||
ListNode tmpNode = new ListNode(0);
|
||||
retResult = tmpNode;
|
||||
while (l1 != null & l2 != null) {
|
||||
if (l1.val <= l2.val) {
|
||||
tmpNode.next = l1;
|
||||
l1 = l1.next;
|
||||
} else {
|
||||
tmpNode.next = l2;
|
||||
l2 = l2.next;
|
||||
}
|
||||
|
||||
tmpNode = tmpNode.next;
|
||||
}
|
||||
|
||||
if (l1 == null) {
|
||||
tmpNode.next = l2;
|
||||
}
|
||||
|
||||
if (l2 == null) {
|
||||
tmpNode.next = l1;
|
||||
}
|
||||
|
||||
retResult = retResult.next;
|
||||
|
||||
System.out.println(retResult.val);
|
||||
System.out.println(retResult.next.val);
|
||||
System.out.println(retResult.next.next.val);
|
||||
|
||||
}
|
||||
}
|
||||
-78
@@ -1,78 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.tests.lintcode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 425
|
||||
* <ul>
|
||||
* <li>@see <a href=
|
||||
* "https://www.cwiki.us/display/ITCLASSIFICATION/Letter+Combinations+of+a+Phone+Number">https://www.cwiki.us/display/ITCLASSIFICATION/Letter+Combinations+of+a+Phone+Number</a>
|
||||
* <li>@see<a href=
|
||||
* "https://www.lintcode.com/problem/letter-combinations-of-a-phone-number/description">https://www.lintcode.com/problem/letter-combinations-of-a-phone-number/description</a>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class LintCode0425LetterCombinationsTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(LintCode0425LetterCombinationsTest.class);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testMain() {
|
||||
logger.debug("LetterCombinationsTest");
|
||||
String digits = "23";
|
||||
|
||||
HashMap<String, String> phoneKeyMap = new HashMap<String, String>();
|
||||
phoneKeyMap.put("0", "");
|
||||
phoneKeyMap.put("1", "");
|
||||
phoneKeyMap.put("2", "abc");
|
||||
phoneKeyMap.put("3", "def");
|
||||
phoneKeyMap.put("4", "ghi");
|
||||
phoneKeyMap.put("5", "jkl");
|
||||
phoneKeyMap.put("6", "mno");
|
||||
phoneKeyMap.put("7", "pqrs");
|
||||
phoneKeyMap.put("8", "tuv");
|
||||
phoneKeyMap.put("9", "wxyz");
|
||||
|
||||
List<String> retStatus = new ArrayList<>();
|
||||
|
||||
if (digits != null && digits.length() != 0) {
|
||||
phoneRecursive(digits, retStatus, phoneKeyMap, "", 0);
|
||||
}
|
||||
|
||||
System.out.println(retStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* phoneRecursive
|
||||
*
|
||||
* @param digits
|
||||
* @param retStatus
|
||||
* @param phoneKeyMap
|
||||
* @param comb
|
||||
* @param index
|
||||
*/
|
||||
private void phoneRecursive(String digits, List<String> retStatus, HashMap<String, String> phoneKeyMap, String comb, int index) {
|
||||
if (index == digits.length()) {
|
||||
retStatus.add(comb);
|
||||
return;
|
||||
}
|
||||
|
||||
char pos = digits.charAt(index);
|
||||
for (char c : ((String) phoneKeyMap.get(String.valueOf(pos))).toCharArray()) {
|
||||
phoneRecursive(digits, retStatus, phoneKeyMap, comb + c, index + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
-94
@@ -1,94 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.tests.lintcode;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 433
|
||||
* <ul>
|
||||
* <li>@see <a href=
|
||||
* "https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islands">https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islands</a>
|
||||
* <li>@see<a href="https://www.lintcode.com/problem/number-of-islands/">https://www.lintcode.com/problem/number-of-islands/</a>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class LintCode0433NumIslandsTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(LintCode0433NumIslandsTest.class);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testMain() {
|
||||
logger.debug("BEGIN");
|
||||
// INIT GRID
|
||||
boolean[][] grid = { { true, true, false, false, false }, { false, true, false, false, true }, { false, false, false, true, true },
|
||||
{ false, false, false, false, false }, { false, false, false, false, true }
|
||||
|
||||
};
|
||||
|
||||
// NULL CHECK
|
||||
if (grid.length == 0 || grid[0].length == 0) {
|
||||
System.out.println("NULL");
|
||||
// return 0;
|
||||
}
|
||||
|
||||
// GET SIZE
|
||||
int n = grid.length;
|
||||
int m = grid[0].length;
|
||||
|
||||
// ARRAY FOR VISITED LOG
|
||||
boolean[][] visited = new boolean[n][m];
|
||||
|
||||
int count = 0;
|
||||
|
||||
// LOOP FOR GRID
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
if (grid[i][j] && !visited[i][j]) {
|
||||
numIslandsDFS(grid, visited, i, j);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(count);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param grid
|
||||
* @param visited
|
||||
* @param x
|
||||
* @param y
|
||||
*/
|
||||
public void numIslandsDFS(boolean[][] grid, boolean[][] visited, int x, int y) {
|
||||
if (x < 0 || x >= grid.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (y < 0 || y >= grid[0].length) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (grid[x][y] != true || visited[x][y]) {
|
||||
return;
|
||||
}
|
||||
|
||||
visited[x][y] = true;
|
||||
|
||||
// Recursive call
|
||||
numIslandsDFS(grid, visited, x - 1, y);
|
||||
numIslandsDFS(grid, visited, x + 1, y);
|
||||
numIslandsDFS(grid, visited, x, y - 1);
|
||||
numIslandsDFS(grid, visited, x, y + 1);
|
||||
|
||||
}
|
||||
}
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
package com.ossez.codebank.algorithm.tests.lintcode;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 1480
|
||||
* <ul>
|
||||
* <li>@see
|
||||
* <a href= "https://www.cwiki.us/display/ITCLASSIFICATION/Dot+Product">https://www.cwiki.us/display/ITCLASSIFICATION/Dot+Product</a>
|
||||
* <li>@see<a href= "https://www.lintcode.com/problem/dot-product/">https://www.lintcode.com/problem/dot-product/</a>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class LintCode1480DotProductTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(LintCode1480DotProductTest.class);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testMain() {
|
||||
logger.debug("BEGIN");
|
||||
int t = 87;
|
||||
int[] dur = { 20, 25, 19, 37 };
|
||||
// Write your code here
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,210 +0,0 @@
|
||||
package com.ossez.codebank.interview.tests;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Amazon
|
||||
*
|
||||
* <pre>
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Flatten+Nested+Arrays
|
||||
* </pre>
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class AmazonTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(AmazonTest.class);
|
||||
|
||||
List<Integer> returnList = new ArrayList<Integer>();
|
||||
|
||||
/**
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Flatten+Nested+Arrays
|
||||
*
|
||||
* FlattenNestedArrays
|
||||
*/
|
||||
@Test
|
||||
public void testFlattenNestedArrays() {
|
||||
logger.debug("Test FlattenNestedArrays");
|
||||
int numRows = 3;
|
||||
int numColumns = 3;
|
||||
List<List<Integer>> area = new ArrayList<List<Integer>>();
|
||||
List<Integer> a1 = new ArrayList<Integer>();
|
||||
List<Integer> a2 = new ArrayList<Integer>();
|
||||
List<Integer> a3 = new ArrayList<Integer>();
|
||||
|
||||
a1.add(1);
|
||||
a1.add(0);
|
||||
a1.add(0);
|
||||
|
||||
a2.add(1);
|
||||
a2.add(0);
|
||||
a2.add(0);
|
||||
|
||||
a3.add(1);
|
||||
a3.add(9);
|
||||
a3.add(1);
|
||||
area.add(a1);
|
||||
area.add(a2);
|
||||
area.add(a3);
|
||||
|
||||
int countD = 0;
|
||||
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
for (int i = 0; i < area.size(); i++) {
|
||||
boolean found9 = false;
|
||||
y = 0;
|
||||
|
||||
List<Integer> xList = area.get(i);
|
||||
for (int j = 0; j < xList.size(); j++) {
|
||||
if (xList.get(j) != 9) {
|
||||
y++;
|
||||
} else {
|
||||
found9 = true;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (found9) {
|
||||
break;
|
||||
}
|
||||
|
||||
x++;
|
||||
|
||||
}
|
||||
|
||||
int carX = 0;
|
||||
int carY = 0;
|
||||
|
||||
for (int k = 0; k < numRows * numRows; k++) {
|
||||
|
||||
String command = makeMove(carX, carY, numRows, numColumns, area);
|
||||
|
||||
if (command != null) {
|
||||
|
||||
if (command.equals("U")) {
|
||||
carY = carY + 1;
|
||||
}
|
||||
if (command.equals("R")) {
|
||||
carX = carX + 1;
|
||||
}
|
||||
if (command.equals("L")) {
|
||||
carX = carX - 1;
|
||||
}
|
||||
if (command.equals("D")) {
|
||||
carY = carY + 1;
|
||||
}
|
||||
|
||||
countD = countD + 1;
|
||||
|
||||
if (carX == x && carY == y) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("---" + x + y);
|
||||
System.out.println("--COUNT-" + countD );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Java 8 Stream to Flatten array.
|
||||
*
|
||||
* @param array
|
||||
* @return
|
||||
*/
|
||||
private static String makeMove(int carX, int carY, int numRows, int numColumns, List<List<Integer>> area) {
|
||||
|
||||
|
||||
|
||||
if ((carX + 1 <numRows) && area.get(carX + 1).get(carY) !=9 )
|
||||
if (area.get(carX + 1).get(carY) == 1)
|
||||
return "R";
|
||||
else
|
||||
return "F";
|
||||
|
||||
|
||||
if ((carY + 1 <numColumns) && area.get(carX + 1).get(carY) !=9)
|
||||
if (area.get(carX).get(carY + 1) == 1)
|
||||
return "U";
|
||||
else
|
||||
return "F";
|
||||
|
||||
if (carX > 0 && area.get(carX + 1).get(carY) !=9 )
|
||||
if (area.get(carX - 1).get(carY) == 1)
|
||||
return "L";
|
||||
else
|
||||
return "F";
|
||||
|
||||
else if (carX > 0 && carY > 0 && area.get(carX - 1).get(carY - 1) == 1)
|
||||
return "D";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loop And Recursive
|
||||
*
|
||||
* @param inputArray
|
||||
* @return
|
||||
* @throws IllegalArgumentException
|
||||
*/
|
||||
private List<List<Integer>> nearestVegetarianRestaurant(int totalRestaurants, List<List<Integer>> allLocations, int numRestaurants) {
|
||||
// WRITE YOUR CODE HERE
|
||||
|
||||
List<List<Integer>> ret = new ArrayList<List<Integer>>();
|
||||
HashMap<Double, List<Integer>> mp = new HashMap<Double, List<Integer>>();
|
||||
Double[] keyArray = new Double[totalRestaurants];
|
||||
|
||||
int i = 0;
|
||||
for (List<Integer> al : allLocations) {
|
||||
Double dis = getDis(al.get(0), al.get(1));
|
||||
mp.put(dis, al);
|
||||
keyArray[i] = dis;
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
Arrays.sort(keyArray);
|
||||
for (int j = 0; j < numRestaurants; j++) {
|
||||
ret.add(mp.get(keyArray[j]));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Double getDis(int a, int b) {
|
||||
// WRITE YOUR CODE HERE
|
||||
|
||||
return Math.sqrt(a * a + b * b);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Java 8 Stream to Flatten array.
|
||||
*
|
||||
* @param array
|
||||
* @return
|
||||
*/
|
||||
private static Stream<Object> java8Flatten(Object[] array) {
|
||||
// int[] flatInt = java8Flatten(array).mapToInt(Integer.class::cast).toArray();
|
||||
return Arrays.stream(array).flatMap(o -> o instanceof Object[] ? java8Flatten((Object[]) o) : Stream.of(o));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.ossez.codebank.interview.tests;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ossez.codebank.interview.KayakCountUpDown;
|
||||
import com.ossez.codebank.interview.KayakRobotMovement;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class KayakTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(KayakTest.class);
|
||||
|
||||
/**
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Robot+Movement
|
||||
*
|
||||
* testGetCoordinates
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testGetCoordinates() {
|
||||
logger.debug("TEST Get Coordinat ");
|
||||
logger.debug("LFFF - [{}]", KayakRobotMovement.getCoordinates("FF"));
|
||||
logger.debug("LFFFRFFFRRFFF - [{}]", KayakRobotMovement.getCoordinates("LFFFRFFFRRFFF"));
|
||||
|
||||
assertEquals(KayakRobotMovement.getCoordinates("FF"), "0,2");
|
||||
assertEquals(KayakRobotMovement.getCoordinates("LFFFRFFFRRFFF"), "-3,0");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Count+Up+Down
|
||||
*
|
||||
* CountUpDown
|
||||
*/
|
||||
@Test
|
||||
public void testCountUpDown() {
|
||||
logger.debug("TEST Count Up and Down ");
|
||||
|
||||
// 2 -5
|
||||
logger.debug("[2 -> 5]");
|
||||
logger.debug("UP - {}", new KayakCountUpDown().countUp(2, 5));
|
||||
logger.debug("UP & DOWN - {}", new KayakCountUpDown().countUpDown(2, 5));
|
||||
|
||||
// 0 - 5
|
||||
logger.debug("[0 -> 5]");
|
||||
logger.debug("UP - {}", new KayakCountUpDown().countUp(0, 5));
|
||||
logger.debug("UP & DOWN - {}", new KayakCountUpDown().countUpDown(0, 5));
|
||||
|
||||
// -1 - 5
|
||||
logger.debug("[-1 -> 5]");
|
||||
logger.debug("UP - {}", new KayakCountUpDown().countUp(-1, 5));
|
||||
logger.debug("UP & DOWN - {}", new KayakCountUpDown().countUpDown(-1, 5));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
package com.ossez.codebank.interview.tests;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.filefilter.TrueFileFilter;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
/**
|
||||
* MassMutual
|
||||
*
|
||||
* @author Yucheng
|
||||
*
|
||||
*/
|
||||
public class MassMutual {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(MassMutual.class);
|
||||
|
||||
@Test
|
||||
public void loadOBJ() {
|
||||
logger.debug("Test JSON LOAD TO OBJ");
|
||||
List<WoodChuck> woodChuckList = new ArrayList<WoodChuck>();
|
||||
List<File> dataFileList = new ArrayList<File>();
|
||||
|
||||
try {
|
||||
dataFileList = (List<File>) FileUtils.listFiles(new File("C:\\Users\\Yucheng\\Documents\\Data-Sample\\massmutual"),
|
||||
TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
|
||||
|
||||
for (File file : dataFileList) {
|
||||
|
||||
woodChuckList.add(processWoodChuck(FileUtils.readFileToString(file, StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
for (WoodChuck woodChuck : woodChuckList) {
|
||||
logger.debug("\r\n");
|
||||
logger.debug("name - [{}]", woodChuck.getName());
|
||||
logger.debug("aliases0 - [{}]", woodChuck.getAliases0());
|
||||
logger.debug("aliases1 - [{}]", woodChuck.getAliases1());
|
||||
logger.debug("height - [{}]", woodChuck.getHeight());
|
||||
logger.debug("weight - [{}]", woodChuck.getWeight());
|
||||
logger.debug("woodChuckedWeight - [{}]", woodChuck.getWoodChuckedWeight());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
logger.error("API Data Table Process Error {}", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param jData
|
||||
* @return
|
||||
*/
|
||||
private WoodChuck processWoodChuck(String jData) {
|
||||
WoodChuck woodChuck = new WoodChuck();
|
||||
|
||||
JsonElement jsonElement = new JsonParser().parse(jData);
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
|
||||
if (jsonObject.get("aliases") != null && jsonObject.get("aliases").isJsonArray()) {
|
||||
|
||||
// name
|
||||
woodChuck.setName(jsonObject.get("name").getAsString());
|
||||
|
||||
// aliases
|
||||
JsonArray aliases = jsonObject.getAsJsonArray("aliases");
|
||||
woodChuck.setAliases0(aliases.get(0).getAsString());
|
||||
woodChuck.setAliases1(aliases.get(1).getAsString());
|
||||
|
||||
// physical
|
||||
woodChuck.setHeight(jsonObject.get("physical").getAsJsonObject().get("height_in").getAsString());
|
||||
woodChuck.setWeight(jsonObject.get("physical").getAsJsonObject().get("weight_lb").getAsString());
|
||||
|
||||
// woodChuckedWeight
|
||||
woodChuck.setWoodChuckedWeight(jsonObject.get("wood_chucked_lbs").getAsString());
|
||||
|
||||
} else {
|
||||
// name
|
||||
woodChuck.setName(jsonObject.get("name").getAsString());
|
||||
|
||||
// aliases
|
||||
woodChuck.setAliases0(jsonObject.get("aliases.0").getAsString());
|
||||
woodChuck.setAliases1(jsonObject.get("aliases.1").getAsString());
|
||||
|
||||
// physical
|
||||
woodChuck.setHeight(jsonObject.get("physical.height_in").getAsString());
|
||||
woodChuck.setWeight(jsonObject.get("physical.weight_lb").getAsString());
|
||||
|
||||
// woodChuckedWeight
|
||||
woodChuck.setWoodChuckedWeight(jsonObject.get("wood_chucked_lbs").getAsString());
|
||||
}
|
||||
|
||||
return woodChuck;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* + WoodChuck OBJ for JSON process. +
|
||||
*/
|
||||
class WoodChuck {
|
||||
private String name = null;
|
||||
private String aliases0 = null;
|
||||
private String aliases1 = null;
|
||||
private String height = null;
|
||||
private String weight = null;
|
||||
private String woodChuckedWeight = null;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAliases0() {
|
||||
return aliases0;
|
||||
}
|
||||
|
||||
public void setAliases0(String aliases0) {
|
||||
this.aliases0 = aliases0;
|
||||
}
|
||||
|
||||
public String getAliases1() {
|
||||
return aliases1;
|
||||
}
|
||||
|
||||
public void setAliases1(String aliases1) {
|
||||
this.aliases1 = aliases1;
|
||||
}
|
||||
|
||||
public String getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(String height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public String getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(String weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public String getWoodChuckedWeight() {
|
||||
return woodChuckedWeight;
|
||||
}
|
||||
|
||||
public void setWoodChuckedWeight(String woodChuckedWeight) {
|
||||
this.woodChuckedWeight = woodChuckedWeight;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package com.ossez.codebank.interview.tests;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* PillPack
|
||||
*
|
||||
* <pre>
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Flatten+Nested+Arrays
|
||||
* </pre>
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class PillPackTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(PillPackTest.class);
|
||||
|
||||
List<Integer> returnList = new ArrayList<Integer>();
|
||||
|
||||
/**
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Flatten+Nested+Arrays
|
||||
*
|
||||
* FlattenNestedArrays
|
||||
*/
|
||||
@Test
|
||||
public void testFlattenNestedArrays() {
|
||||
logger.debug("Test FlattenNestedArrays");
|
||||
|
||||
Object[] array = { 1, 2, new Object[] { 3, 4, new Object[] { 5, new Object[] { new Object[] { 6 } } }, 7 }, 8, 9, 10 };
|
||||
logger.debug("LOOP: {} - > {}", Arrays.deepToString(array), Arrays.toString(loopFlatten(array)));
|
||||
|
||||
logger.debug("Java 8: {} - > {}", Arrays.deepToString(array), Arrays.toString(java8Flatten(array).toArray()));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Loop And Recursive
|
||||
*
|
||||
* @param inputArray
|
||||
* @return
|
||||
* @throws IllegalArgumentException
|
||||
*/
|
||||
private static Integer[] loopFlatten(Object[] inputArray) throws IllegalArgumentException {
|
||||
// NULL CHECK
|
||||
if (inputArray == null)
|
||||
return null;
|
||||
|
||||
List<Integer> flatList = new ArrayList<Integer>();
|
||||
|
||||
for (Object element : inputArray) {
|
||||
if (element instanceof Integer) {
|
||||
flatList.add((Integer) element);
|
||||
} else if (element instanceof Object[]) {
|
||||
// Recursive
|
||||
flatList.addAll(Arrays.asList(loopFlatten((Object[]) element)));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Input must be an array of Integers or nested arrays of Integers");
|
||||
}
|
||||
}
|
||||
return flatList.toArray(new Integer[flatList.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Java 8 Stream to Flatten array.
|
||||
*
|
||||
* @param array
|
||||
* @return
|
||||
*/
|
||||
private static Stream<Object> java8Flatten(Object[] array) {
|
||||
// int[] flatInt = java8Flatten(array).mapToInt(Integer.class::cast).toArray();
|
||||
return Arrays.stream(array).flatMap(o -> o instanceof Object[] ? java8Flatten((Object[]) o) : Stream.of(o));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.ossez.codebank.interview.tests;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* For Tenna
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class TennaTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(TennaTest.class);
|
||||
|
||||
/**
|
||||
* Optimized Math
|
||||
*/
|
||||
@Test
|
||||
public void testOptimizedMath() {
|
||||
|
||||
HashMap<Integer, String> outMap = new HashMap<Integer, String>();
|
||||
|
||||
// LOOP SET VALUE TO MAP
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
if (outMap.get(i) == null && i % 2 == 0 && i % 3 == 0) {
|
||||
outMap.put(i, "divisible by two and three.");
|
||||
} else if (outMap.get(i) == null && i % 3 == 0) {
|
||||
outMap.put(i, "divisible by three.");
|
||||
} else if (outMap.get(i) == null && i % 2 == 0) {
|
||||
outMap.put(i, "even.");
|
||||
} else {
|
||||
outMap.put(i, "odd.");
|
||||
}
|
||||
}
|
||||
|
||||
// LOOP FOR OUTPUT
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
logger.debug("The number '{}' is {} ", i, outMap.get(i));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
package com.ossez.codebank.interview.tests;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math3.util.CombinatoricsUtils;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* For Wayfair
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class WayfairTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(WayfairTest.class);
|
||||
|
||||
/**
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Build+Castles
|
||||
*/
|
||||
@Test
|
||||
public void testBuildCastles() {
|
||||
|
||||
// int[] A = { -3, -3 };
|
||||
int[] A = { 2, 2, 3, 4, 3, 3, 2, 2, 1, 1, 2, 5 };
|
||||
|
||||
int h = 0;
|
||||
int v = 0;
|
||||
|
||||
List<Integer> nList = new ArrayList<Integer>();
|
||||
|
||||
// Rebuild List
|
||||
nList.add(A[0]);
|
||||
for (int i = 0; i < A.length - 1; i++) {
|
||||
|
||||
if (A[i] != A[i + 1]) {
|
||||
nList.add(A[i + 1]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// LOOP List to find right location
|
||||
for (int i = 0; i < nList.size() - 1; i++) {
|
||||
|
||||
// COUNT 0
|
||||
if (i == 0) {
|
||||
if (nList.get(i) < nList.get(i + 1)) {
|
||||
v++;
|
||||
}
|
||||
} else {
|
||||
if (nList.get(i) < nList.get(i - 1) && nList.get(i) < nList.get(i + 1)) {
|
||||
v++;
|
||||
}
|
||||
|
||||
if (nList.get(i) > nList.get(i - 1) && nList.get(i) > nList.get(i + 1)) {
|
||||
h++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nList.size() == 1) {
|
||||
h++;
|
||||
} else if (nList.size() > 2 && nList.get(nList.size() - 1) > nList.get(nList.size() - 2)) {
|
||||
h++;
|
||||
}
|
||||
|
||||
// CHECK
|
||||
logger.debug("V - [{}]", v);
|
||||
logger.debug("H - [{}]", h);
|
||||
|
||||
logger.debug("H + V - [{}]", (h + v));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Binomial+Coefficient
|
||||
*
|
||||
* Binomial Coefficient
|
||||
*/
|
||||
@Test
|
||||
public void testBinomialCoefficient() {
|
||||
int n = 40;
|
||||
int k = 20;
|
||||
|
||||
BigDecimal bc = factorial(n).divide(factorial(k).multiply(factorial(n - k)));
|
||||
// a.compareTo(new BigDecimal(1000000000))
|
||||
logger.debug("{}", bc);
|
||||
logger.debug("Check for Compare To - [{}]", bc.compareTo(new BigDecimal(1000000000)));
|
||||
logger.debug("Value - [{}]", bc);
|
||||
|
||||
logger.debug("Apache CombinatoricsUtils Factorial - [{}]", CombinatoricsUtils.factorialDouble(20));
|
||||
logger.debug("Apache CombinatoricsUtils Binomial Coefficient - [{}]", CombinatoricsUtils.binomialCoefficientDouble(40, 20));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* for factorial
|
||||
*
|
||||
* @param x
|
||||
* @return
|
||||
*/
|
||||
private static BigDecimal factorial(int x) {
|
||||
if (x == 1 || x == 0) {
|
||||
return BigDecimal.valueOf(1);
|
||||
} else {
|
||||
return BigDecimal.valueOf(x).multiply(factorial(x - 1));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.ossez.codebank.interview.tests.others;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* For Lambda Evens
|
||||
*
|
||||
* @author YuCheng
|
||||
*/
|
||||
public class LambdaEvensTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(LambdaEvensTest.class);
|
||||
|
||||
/**
|
||||
* Lambda Function
|
||||
*/
|
||||
|
||||
interface Arithmetic {
|
||||
Long operation(Long a, Long b);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Lambda+Evens
|
||||
*/
|
||||
@Test
|
||||
public void testLambdaEvents() {
|
||||
|
||||
String line = "1 2 3 4 5 6 ";
|
||||
|
||||
String[] lineArray = line.split(" ");
|
||||
List<Long> numbers = new ArrayList<>();
|
||||
Arithmetic division = (Long a, Long b) -> (a % b);
|
||||
|
||||
|
||||
for (String lineChar : lineArray) {
|
||||
if (division.operation(Long.parseLong(lineChar), 2L) == 0) {
|
||||
System.out.print(lineChar + " ");
|
||||
// logger.debug(lineChar + " ");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.ossez.codebank.interview.tests.others;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* For Minimum Coins
|
||||
*
|
||||
* @author YuCheng
|
||||
*/
|
||||
public class MinimumCoinsTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(MinimumCoinsTest.class);
|
||||
|
||||
/**
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Minimum+Coins
|
||||
*/
|
||||
@Test
|
||||
public void testMinimumCoins() {
|
||||
|
||||
String line = "20";
|
||||
|
||||
long coinsCount = 0;
|
||||
|
||||
Long coinsValue = Long.parseLong(line);
|
||||
coinsCount = coinsValue / 5;
|
||||
Long coinsValueAfter5 = coinsValue % 5;
|
||||
|
||||
if (coinsValueAfter5 == 4 || coinsValueAfter5 == 2)
|
||||
coinsCount = coinsCount + 2;
|
||||
else if (coinsValueAfter5 == 3 || coinsValueAfter5 == 1)
|
||||
coinsCount = coinsCount + 1;
|
||||
|
||||
logger.debug("count Number > {}",coinsCount);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user