Rohit Kumar
BAN USERIts Called Bucket Mapping where we put all code in different bucket and if there is another set which is equal to same bucket, wew dont change bucket insted we define same bucket value, So all the char representing same bucket automatially equals to another bucket.
May be I left any edge case but that can be covered using if else
public class ValidateBooleanStatements {
private HashMap<Character, Integer> storage= new HashMap<Character, Integer>();
private HashMap<Integer, Integer> bucketMapper= new HashMap<>();
public int getRandomId(){
Random random= new Random();
return random.nextInt();
}
public void validateStatements(List<Pair> equals, List<Pair> unequal) throws Exception{
for(Pair pair:equals){
int randomNumber= getRandomId();
Integer aa=storage.get(pair.a);
Integer bb=storage.get(pair.b);
// case: where a and b both are same
if(pair.a==pair.b){
if(aa==null){// it doesnt exist, add it first time
storage.put(pair.a,randomNumber);
bucketMapper.put(randomNumber, randomNumber);
}
}else{//case: where both are different
if(aa!=null && bb!=null){// where both have already entry
if(aa!=bb){// where both arent equal yet
//change bucket
bucketMapper.remove(bb);
bucketMapper.put(bb, bucketMapper.get(aa));// now in bucket mapper both have same source where in storage they have diffrent bucket ID
}// if aa==bb they already are in same bucket and equal
}else if(aa!=null){
storage.put(pair.b, storage.get(pair.a));
// if a has already entry than put it in the same bucket
}else if(bb!=null){
storage.put(pair.a, storage.get(pair.b));
//if b has already an entry, put a in same bucket
}else if(aa==null && bb==null){
storage.put(pair.a, randomNumber);
storage.put(pair.b, randomNumber);
bucketMapper.put(randomNumber, randomNumber);
}
}
}
// we are just validating unequals not entering in storage.
for(Pair pair: unequal){
if(bucketMapper.get(storage.get(pair.a)) == bucketMapper.get(storage.get(pair.b))){
System.out.println(pair.a+"-"+pair.b+" Error");
}
}
}
public static void main(String[] args) throws Exception{
List<Pair> equal= new ArrayList<>();
List<Pair> unequal= new ArrayList<>();
equal.add(new Pair('A', 'B'));
equal.add(new Pair('B', 'D'));
equal.add(new Pair('C', 'D'));
equal.add(new Pair('F', 'G'));
equal.add(new Pair('G', 'H'));
equal.add(new Pair('H', 'C'));
unequal.add(new Pair('A', 'C'));
new ValidateBooleanStatements().validateStatements(equal, unequal);
}
}
class Pair{
Character a;
Character b;
Pair(Character a, Character b){
this.a=a;
this.b=b;
}
}
Its Called Bucket Mapping where we put all code in different bucket and if there is another set which is equal to same bucket, wew dont change bucket insted we define same bucket value, So all the char representing same bucket automatially equals to another bucket.
May be I left any edge case but that can be covered using if else
public class ValidateBooleanStatements {
private HashMap<Character, Integer> storage= new HashMap<Character, Integer>();
private HashMap<Integer, Integer> bucketMapper= new HashMap<>();
public int getRandomId(){
Random random= new Random();
return random.nextInt();
}
public void validateStatements(List<Pair> equals, List<Pair> unequal) throws Exception{
for(Pair pair:equals){
int randomNumber= getRandomId();
Integer aa=storage.get(pair.a);
Integer bb=storage.get(pair.b);
// case: where a and b both are same
if(pair.a==pair.b){
if(aa==null){// it doesnt exist, add it first time
storage.put(pair.a,randomNumber);
bucketMapper.put(randomNumber, randomNumber);
}
}else{//case: where both are different
if(aa!=null && bb!=null){// where both have already entry
if(aa!=bb){// where both arent equal yet
//change bucket
bucketMapper.remove(bb);
bucketMapper.put(bb, bucketMapper.get(aa));// now in bucket mapper both have same source where in storage they have diffrent bucket ID
}// if aa==bb they already are in same bucket and equal
}else if(aa!=null){
storage.put(pair.b, storage.get(pair.a));
// if a has already entry than put it in the same bucket
}else if(bb!=null){
storage.put(pair.a, storage.get(pair.b));
//if b has already an entry, put a in same bucket
}else if(aa==null && bb==null){
storage.put(pair.a, randomNumber);
storage.put(pair.b, randomNumber);
bucketMapper.put(randomNumber, randomNumber);
}
}
}
// we are just validating unequals not entering in storage.
for(Pair pair: unequal){
if(bucketMapper.get(storage.get(pair.a)) == bucketMapper.get(storage.get(pair.b))){
System.out.println(pair.a+"-"+pair.b+" Error");
}
}
}
public static void main(String[] args) throws Exception{
List<Pair> equal= new ArrayList<>();
List<Pair> unequal= new ArrayList<>();
equal.add(new Pair('A', 'B'));
equal.add(new Pair('B', 'D'));
equal.add(new Pair('C', 'D'));
equal.add(new Pair('F', 'G'));
equal.add(new Pair('G', 'H'));
equal.add(new Pair('H', 'C'));
unequal.add(new Pair('A', 'C'));
new ValidateBooleanStatements().validateStatements(equal, unequal);
}
}
class Pair{
Character a;
Character b;
Pair(Character a, Character b){
this.a=a;
this.b=b;
}
}
Its Called Bucket Mapping where we put all code in different bucket and if there is another set which is equal to same bucket, wew dont change bucket insted we define same bucket value, So all the char representing same bucket automatially equals to another bucket.
May be I left any edge case but that can be covered using if else
public class ValidateBooleanStatements {
private HashMap<Character, Integer> storage= new HashMap<Character, Integer>();
private HashMap<Integer, Integer> bucketMapper= new HashMap<>();
public int getRandomId(){
Random random= new Random();
return random.nextInt();
}
public void validateStatements(List<Pair> equals, List<Pair> unequal) throws Exception{
for(Pair pair:equals){
int randomNumber= getRandomId();
Integer aa=storage.get(pair.a);
Integer bb=storage.get(pair.b);
// case: where a and b both are same
if(pair.a==pair.b){
if(aa==null){// it doesnt exist, add it first time
storage.put(pair.a,randomNumber);
bucketMapper.put(randomNumber, randomNumber);
}
}else{//case: where both are different
if(aa!=null && bb!=null){// where both have already entry
if(aa!=bb){// where both arent equal yet
//change bucket
bucketMapper.remove(bb);
bucketMapper.put(bb, bucketMapper.get(aa));// now in bucket mapper both have same source where in storage they have diffrent bucket ID
}// if aa==bb they already are in same bucket and equal
}else if(aa!=null){
storage.put(pair.b, storage.get(pair.a));
// if a has already entry than put it in the same bucket
}else if(bb!=null){
storage.put(pair.a, storage.get(pair.b));
//if b has already an entry, put a in same bucket
}else if(aa==null && bb==null){
storage.put(pair.a, randomNumber);
storage.put(pair.b, randomNumber);
bucketMapper.put(randomNumber, randomNumber);
}
}
}
// we are just validating unequals not entering in storage.
for(Pair pair: unequal){
if(bucketMapper.get(storage.get(pair.a)) == bucketMapper.get(storage.get(pair.b))){
System.out.println(pair.a+"-"+pair.b+" Error");
}
}
}
public static void main(String[] args) throws Exception{
List<Pair> equal= new ArrayList<>();
List<Pair> unequal= new ArrayList<>();
equal.add(new Pair('A', 'B'));
equal.add(new Pair('B', 'D'));
equal.add(new Pair('C', 'D'));
equal.add(new Pair('F', 'G'));
equal.add(new Pair('G', 'H'));
equal.add(new Pair('H', 'C'));
unequal.add(new Pair('A', 'C'));
new ValidateBooleanStatements().validateStatements(equal, unequal);
}
}
class Pair{
Character a;
Character b;
Pair(Character a, Character b){
this.a=a;
this.b=b;
}
}
It should be implemented as Parking space problem.
- Rohit Kumar February 02, 2017Decide minimum time span, such as 15 mins or 30 mins.
and add all the time span for the given day in hashmap. with some Id formed by meeting room and start+end time
Further it can be serialized to save and can be prototyped for a new day. where it doesnt found map in db for future dadays.
Just when ever some one books room cheeck in avialable rooms HashMap and shift it to Reserved...
Its just an idea.