rodrigo.cubillos
BAN USERThat's right, I do not consider the condition C in my algorithm, thank you.
My corrected version here:
public class Horses {
public class HorsesData{
boolean white;
int secuence;
public HorsesData(boolean w,int sec){
this.white=w;
this.secuence=sec;
}
@Override
public String toString() {
return "["+Integer.toString(secuence)+","+white+"]" ;
}
}
public static void main(String args[]){
Horses instance = new Horses();
instance.execute();
}
public void execute(){
// HorsesData horses[]={new HorsesData(true, 1),new HorsesData(false, 2),new HorsesData(true, 3),new HorsesData(false, 4),new HorsesData(false, 5),new HorsesData(true, 6),new HorsesData(true, 7),new HorsesData(false, 8),new HorsesData(true, 9),new HorsesData(true, 10)};
// List stables[]= {new ArrayList(),new ArrayList(),new ArrayList(),new ArrayList(),new ArrayList()};
HorsesData horses[]={new HorsesData(true, 1),new HorsesData(true, 2),new HorsesData(true, 3),new HorsesData(false, 4),new HorsesData(false, 5),new HorsesData(false, 6)};
List stables[]= {new ArrayList(),new ArrayList(),new ArrayList()};
int addedhorses=0;
int changesCount = getChanges(horses);
int minMulti = ((changesCount+1)/stables.length)+(((changesCount+1)%stables.length) != 0?1:0);
//TODO:ver decimales en caso de que la divsion no sea exacta
for(int i =0;i<stables.length;i++){
int currentHorses[]={0,0};
while((addedhorses<horses.length
&& ((horses.length-addedhorses)>=(stables.length-i))
&& (currentHorses[0]+(horses[addedhorses].white?1:0))*(currentHorses[1]+(horses[addedhorses].white?0:1))<minMulti )) {
currentHorses[horses[addedhorses].white?0:1]++;
stables[i].add(horses[addedhorses++]);
}
System.out.println("stables ["+i+"] ="+stables[i]);
}
}
private int getChanges(HorsesData[] horses) {
// TODO Auto-generated method stub
int result=0;
boolean current=horses[0].white;
for (int i=1; i< horses.length;i++){
if (horses[i].white!=current){
result++;
current=horses[i].white;
}
}
return result;
}
}
public class Horses {
public class HorsesData{
boolean white;
int secuence;
public HorsesData(boolean w,int sec){
this.white=w;
this.secuence=sec;
}
@Override
public String toString() {
return "HorsesData [white=" + white + ", secuence=" + secuence
+ "]";
}
}
public static void main(String args[]){
Horses instance = new Horses();
instance.execute();
}
public void execute(){
HorsesData horses[]={new HorsesData(true, 1),new HorsesData(false, 2),new HorsesData(true, 3),new HorsesData(false, 4),new HorsesData(false, 5),new HorsesData(true, 6),new HorsesData(true, 7),new HorsesData(false, 8),new HorsesData(true, 9),new HorsesData(true, 10)};
List stables[]= {new ArrayList(),new ArrayList(),new ArrayList()};
int minHorsesPerStable = horses.length/stables.length;
int numOfStableWithMaximum =horses.length % stables.length;
int addedhorses=0;
for(int i =0;i<numOfStableWithMaximum;i++){
for(int j=0;j<minHorsesPerStable+1;j++){
stables[i].add(horses[addedhorses++]);
}
System.out.println("stables ["+i+"] ="+stables[i]);
}
for (int i=numOfStableWithMaximum;i<stables.length;i++){
for(int j=0;j<minHorsesPerStable;j++){
stables[i].add(horses[addedhorses++]);
}
System.out.println("stables ["+i+"] ="+stables[i]);
}
}
}
point A is satisfied by adding horses in order to the stables but the point C not be addressed.
my new code considering C , the algorithm is :
1. get count of changes of colors, scrolling the horses in order (A)
2. calculate the minimum product between white and black horses.
3. add horses to stables while meet with the "minimum product between white and black horses" and number of the horses remaining > to free stables (condition B and C)
- rodrigo.cubillos June 16, 2014