Urvashi
BAN USER
Comments (2)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
public int countUneatenLeaves(int numberLeaves, int[] catArray) {
int uneatenLeaves = 0;
int catArraySize = catArray.length;
int countEaten = 0;
// this will store which leaves are already eaten
HashMap<Integer,Integer> positionEatenHash = new HashMap<Integer,Integer>();
//take one element from caterpillar array and find leave position it will eat. Put eaten in hashmap
for( int i = 0; i < catArraySize; i++ ){
int catervalue = catArray[i];
for(int j = 1; j*catervalue <= numberLeaves; j++){
if(!positionEatenHash.containsKey(catArray[i]*j)){
positionEatenHash.put(catArray[i]*j, 1);
countEaten++;
}
}
}
uneatenLeaves = numberLeaves - countEaten;
return uneatenLeaves;
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
I think its pretty simple approach with time complexity of O(c*n/2) in worst case.
- Urvashi October 18, 2014