public class MaxSeatTest {
public static void main(String[] args) {
MaxSeatTest test = new MaxSeatTest();
Boolean[] seatArray = test.initializeSeatArray(5);
List occupiedList = new ArrayList();
// occupiedList = test.specifyFirst(occupiedList, seatArray);
test.findNextPlace(occupiedList, seatArray);
}
/*
* step1: Initialize 25 seats as emptyArray, boolean[] b==new boolean[25]; each obj is initalized to true.
* 1.1 empty seat = true; occupied seat = false;
* 1.2 For each 25 from index=0, call findNextPlace() to identity next Place;
*
* step2: findNextPlace(occupiedList); return occupiedSeatNumber;
* 2.1 for each emptyArray, assume 1st person seat from index = 0, 1, 2...
* occupiedList.add(0) == emptyArray[i];
* 2.2 Calculate nextSeat place based on 1st,..., minSeat, maxSeat
*
* for each occupiedList;
*
* if occupiedList.size()==1; then last = 1st;
* next person seat range: max of ((1st-minSeat),(2nd-1st)...(maxSeat-last));
* if((1st-minSeat)<(maxSeat-last)){
* nextSeat = (last+max)/2;
* }else{
* nextSeat = (min+1st)/2;
* }
*
* if(nextSeat is odd)
* {break loop;}
* else{
* emptyArray[index] = false;
* occupiedList.add(nextSeat);
* occupiedSeatNumber++;
* }
*
* if(occupiedSeatNumber>=emptyArray.length/2){
* "SUCCESS";
* }
*
*/
// Intialize Boolean[] array, set each boolean element to true; true means it's available to be occupied;
public Boolean[] initializeSeatArray(int totalSeatNumber){
Boolean[] seatArray = new Boolean[totalSeatNumber];
for(int i=0; i<totalSeatNumber; i++){
seatArray[i] = true;
}
return seatArray;
}
public void findNextPlace(List occupiedList, Boolean[] seatArray){
int minSeat = 0;
int maxSeat = seatArray.length;
int maxPlaceholder = maxSeat/2 + 1;
int occupiedSeatNumber = occupiedList.size();
int emptySeatNumber = maxSeat-occupiedSeatNumber;
int nextSeat = 0;
// recursion exit if maxNumber reaches;
if(occupiedSeatNumber==maxPlaceholder){
return;
}else{
// List all occupied Seat inclusive minSeat and maxSeat;
List<Integer> comparePointList = new ArrayList<Integer>();
comparePointList.add(minSeat);
comparePointList.add(maxSeat);
// calculate the farthest distance between any adjacent 2 seats; The next person will sit startPoint+maxDistance/2;
// comparePointList is the list containing all seats' index;
List<Integer> distanceList = new ArrayList<Integer>();
for(int i=0; i<comparePointList.size()-1; i++){
int startPoint = comparePointList.get(i);
int endPoint = comparePointList.get(i+1);
distanceList.add(endPoint-startPoint);
}
}
}
public class MaxSeatTest {
public static void main(String[] args) {
MaxSeatTest test = new MaxSeatTest();
Boolean[] seatArray = test.initializeSeatArray(5);
/*
* step1: Initialize 25 seats as emptyArray, boolean[] b==new boolean[25]; each obj is initalized to true.
* 1.1 empty seat = true; occupied seat = false;
* 1.2 For each 25 from index=0, call findNextPlace() to identity next Place;
*
* step2: findNextPlace(occupiedList); return occupiedSeatNumber;
* 2.1 for each emptyArray, assume 1st person seat from index = 0, 1, 2...
* occupiedList.add(0) == emptyArray[i];
* 2.2 Calculate nextSeat place based on 1st,..., minSeat, maxSeat
*
* for each occupiedList;
*
* if occupiedList.size()==1; then last = 1st;
* next person seat range: max of ((1st-minSeat),(2nd-1st)...(maxSeat-last));
* if((1st-minSeat)<(maxSeat-last)){
* nextSeat = (last+max)/2;
* }else{
* nextSeat = (min+1st)/2;
* }
*
* if(nextSeat is odd)
* {break loop;}
* else{
* emptyArray[index] = false;
* occupiedList.add(nextSeat);
* occupiedSeatNumber++;
* }
*
* if(occupiedSeatNumber>=emptyArray.length/2){
* "SUCCESS";
* }
*
*/
// Intialize Boolean[] array, set each boolean element to true; true means it's available to be occupied;
public Boolean[] initializeSeatArray(int totalSeatNumber){
Boolean[] seatArray = new Boolean[totalSeatNumber];
for(int i=0; i<totalSeatNumber; i++){
seatArray[i] = true;
}
return seatArray;
}
public void findNextPlace(List occupiedList, Boolean[] seatArray, int firstSeat){
int minSeatIndex = 0;
int maxSeatIndex = seatArray.length-1;
int maxPlaceholder = (seatArray.length+1)/2;
int occupiedSeatNumber = occupiedList.size();
int emptySeatNumber = maxSeatIndex-occupiedSeatNumber;
// List all occupied Seat inclusive minSeat and maxSeat;
List<Integer> comparePointList = new ArrayList<Integer>();
comparePointList.add(minSeatIndex);
comparePointList.add(maxSeatIndex);
comparePointList.add(firstSeat);
Collections.sort(comparePointList);
// calculate the farthest distance between any adjacent 2 seats; The next person will sit startPoint+maxDistance/2;
// comparePointList is the list containing all seats' index;