dhiralpandya
BAN USERpublic static boolean isSomeoneWonGame(char[][] game) {
int top = 1;
int bottom = 1;
int left = 1;
int right = 1;
for (int i = 1; i < 3; i++) {
if (game[0][i] == game[0][i - 1]) {
top++;
}
if (game[i][0] == game[i - 1][0]) {
left++;
}
if (game[2][i] == game[2][i - 1]) {
bottom++;
}
if (game[i][2] == game[i - 1][2]) {
right++;
}
if (top == 3 || left == 3 || right == 3 || bottom == 3) {
return true;
}
}
int topLeft = 1;
int topRight = 1;
for (int i = 1; i < 3; i++) {
if (game[i][i] == game[i - 1][i - 1]) {
topLeft++;
}
if (game[i][2 - i] == game[i - 1][2 - i + 1]) {
topRight++;
}
if (topLeft == 3 || topRight == 3) {
return true;
}
}
return false;
}
public static boolean isContainsDuplicateTrees(TreeNode root) {
return isContainsDuplicateTrees(new TreeSet<>(), root);
}
public static boolean isContainsDuplicateTrees(Set<String> trees, TreeNode root) {
if (root != null) {
String tree = "";
if (root.leftNode != null) {
tree += root.leftNode.nodeName;
}
tree += root.nodeName;
if (root.rightNode != null) {
tree += root.rightNode.nodeName;
}
if (trees.contains(tree)) {
System.out.println(tree);
return true;
}
if (tree.length() == 3) {
System.out.println(tree);
trees.add(tree);
}
boolean isLeftHave = false;
if (root.leftNode != null) {
isLeftHave = isContainsDuplicateTrees(trees, root.leftNode);
}
boolean isRightHave = false;
if (root.rightNode != null) {
isRightHave = isContainsDuplicateTrees(trees, root.rightNode);
}
return (isLeftHave || isRightHave);
}
return false;
}
package com.omt.learn.algo;
import java.util.Set;
import java.util.TreeSet;
public class FindAnagrams {
public static void main(String[] args) {
String[] input = new String[] { "tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc" };
Set<String> output = new TreeSet<>();
for (String s : input) {
if (output.contains(s)) {
System.out.println(s);
output.remove(s);
} else {
Set<String> set = getAllPossibleAnagram("", s);
set.remove(s);
out: for (String st : set) {
for (String inp : input) {
if (set.contains(inp)) {
System.out.println(s);
output.addAll(set);
break out;
}
}
}
}
}
}
public static Set<String> getAllPossibleAnagram(String prefix, String s) {
Set<String> set = new TreeSet<>();
// System.out.println(prefix);
if (s.length() == 0) {
set.add(prefix);
return set;
}
for (int i = 0; i < s.length(); i++) {
set.addAll(getAllPossibleAnagram(prefix + s.charAt(i), s.substring(0, i) + s.substring(i + 1)));
}
return set;
}
}
int[] i = { 5, 25, 4, 8, 32, 6 };
System.out.println(startGame('a', i, 0, i.length - 1, new ArrayList<>()));
private static List<Integer> startGame(char startWith, int[] coins, int start, int end, List coinsCollectedByA) {
if (start >= end) {
if (startWith == 'a') {
coinsCollectedByA.add(coins[end]);
}
return coinsCollectedByA;
}
int coinCollected = ((coins[start] + coins[end - 1]) > (coins[start + 1] + coins[end])) ? coins[start]
: coins[end];
if (((coins[start] + coins[end - 1]) > (coins[start + 1] + coins[end]))) {
start++;
} else {
end--;
}
if (startWith == 'a') {
coinsCollectedByA.add(coinCollected);
startWith = 'b';
} else {
startWith = 'a';
}
return startGame(startWith, coins, start, end, coinsCollectedByA);
}
Below is a my solution of this problem. Here i used three method
1. combineSortedArray : it is just to combine all the arrays and perform sorting on it.
2. isCoverAllArrayCount : is just to check our selected array contains at least one digit from all arrays.
3. calculateMinDistance this is our main logic method. It contacts main logic for this algorithm
import java.util.Set;
import java.util.TreeSet;
public class LengthOfArray {
public static void main(String args[]) {
int[][] arrayOfArray = { { 2, 10, 15, 24, 26 }, { 0, 1, 9, 12, 20 }, { 3, 4, 18, 22, 30 } };
int[] selectedArray = calculateMinDistance(arrayOfArray);
System.out.print("[ ");
for (int i : selectedArray) {
System.out.print(i + " ");
}
System.out.println("]");
}
private static int[] calculateMinDistance(int[][] arrays) {
Integer arrayOfSet[] = combineSortedArray(arrays);
int minRangeFoundForSelectedArray = arrayOfSet[arrayOfSet.length - 1] - arrayOfSet[0];
int arrayLength = arrays.length;
int selectedArray[] = new int[arrayLength];
int validCount = (arrayLength) * (arrayLength + 1) / 2;
int count = 0;
while ((count + arrayLength) < arrayOfSet.length) {
int[] tempArray = new int[arrayLength];
for (int i = 0; i < arrayLength; i++) {
tempArray[i] = arrayOfSet[i + count];
}
if (((tempArray[arrayLength - 1] - tempArray[0]) <= minRangeFoundForSelectedArray)
&& isCoverAllArrayCount(tempArray, arrays) == validCount) {
selectedArray = tempArray;
minRangeFoundForSelectedArray = tempArray[arrayLength - 1] - tempArray[0];
}
count++;
}
return selectedArray;
}
private static int isCoverAllArrayCount(int[] selectedArray, int[][] arrays) {
int count = 0;
for (int i = 0; i < arrays.length; i++) {
int[] singleArray = arrays[i];
loopJ: for (int j : singleArray) {
for (int selectedInt : selectedArray) {
if (j == selectedInt) {
count += i + 1;
break loopJ;
}
}
}
}
return count;
}
private static Integer[] combineSortedArray(int arrays[][]) {
Set<Integer> set = new TreeSet<Integer>();
for (int[] a : arrays) {
for (int number : a) {
set.add(number);
}
}
Integer arrayOfSet[] = set.toArray(new Integer[set.size()]);
return arrayOfSet;
}
}
- dhiralpandya May 13, 2016