kirikanjoroge
BAN USERpublic class MinimumArrayInsertions {
public static void main(String[] args) {
MinimumArrayInsertions app = new MinimumArrayInsertions();
System.out.println(app.minimumInsertions(new int[]{1, 3, 4, 2, 5, 6}, new int[]{3, 4, 6, 5, 7}));
}
public int minimumInsertions(int[] arrA, int[] arrB) {
int i = 0;//min insertions
int al = arrA.length;//element counter in A
for (int n = 0; n < arrB.length;) {
if (n <= al - 1) {
//good to compare and shift until there is equivalence at this position and increment n++
if (arrB[n] == arrA[n]) {
//nice
n += 1;
} else {
//delete in A and shift and loop
for (int p = n; p < al - 1; p++) {
arrA[p] = arrA[p + 1];
}
//reduce arrA length
al -= 1;
//maintin value n and recheck again
}
} else {
//concat all arrB[n] to arrB[arrB.length-1] and count insertions
i = i + (arrB.length - n);
//set value of n to size of B
n = arrB.length;
}
}
return i;
}
}
public class ArrayCycle {
public static void main(String[] args){
ArrayCycle ac=new ArrayCycle();
System.out.println("Array Cycle Length: "+ac.detectArrayCycle(new int[]{1,0}));
}
public int detectArrayCycle(int[] a){
int s=a.length;
int c=0;//cycle length
int ni=0; //next arrary idx, start from a[0]
boolean cd=false;//cycle detected
int [] v=new int[s];//array idx visit tracker to calculate
do{
v[ni]=1;//mark a[n] as visited
ni=a[ni];
c+=1;
if(v[ni]==1){
cd=true;
}
}while(ni<s && cd!=true);
return cd?c:-1;
}
public int maxCardSelection(Object[][] deck) {
- kirikanjoroge January 28, 2021int ms = 0;//max selections
/*loop through the card deck*/
for (int i = 0; i < deck.length; i++) {
if (i == 0) {
//just pick the card
ms += 1;
} else {
/*check if the suite or number equals previous card, this is allowed*/
if ((deck[i][0].equals(deck[i - 1][0])) || (deck[i][1].equals(deck[i - 1][1]))) {
ms += 1;
} else {
/*we cannot continue*/
return ms;
}
}
}
return ms;
}