bhanu
BAN USERi will use 2 maps and a queue.
map - I :{key->(fieldName,fieldvalue),value->list(workerIds)};
map - II :{key->workerId,value->list((fieldName,fieldValue))};
queue is priority queue with key as count and value as list of (fieldName,fieldvalue)
for any query with filedName= fname and field value = fvalue
v = Map.contains((fname,fvalue));
wobjects = list();
if(v){
foreach val in Map.get((fname,fvalue)){
wobjects.add(mapIII.get(val));
}
}
else{
make a db call get the worker objects .
foreach(workerObject){
if(queue is not full){
if(worker object not in map 2){
add worker object to map 2 and add to queue with key 1.
}
else(worker object in map 2){
update worker object in queue with key = key +1.
}
}
else(queue is full){
delete worker object from queue also from map 2 and also delete all field mappings from map 1 of the element
push the new element
}
}
}
}
i implemented with a bit of dp complexity would be n^2*m
class Q1 {
/**
* @param args
*/
public static void main(String[] args) {
int[] arr = {1,2,3,-1,9,8};
int n =6;
int m=3;
if(m==1){
System.out.println(0);
return;
}
int[] decP = new int[n];
int[] incP = new int[n];
int[] dec = new int[n];
int[] inc = new int[n];
for(int i=0;i<n;i++){
decP[i]= 1;
incP[i]=1;
}
for(int l=2;l<=m;l++){
for(int i=l-1;i<n;i++){
dec[i] = 0;
inc[i] = 0;
for(int j=l-2;j<i;j++){
if((arr[i]<=arr[j]))
dec[i] = decP[j]+dec[i];
if(arr[i]>=arr[j]){
inc[i] = incP[j]+inc[i];
}
}
}
decP = dec.clone();
incP = inc.clone();
}
int diff=0;
for(int i =(m-1);i<n;i++)
diff =diff+inc[i]-dec[i];
System.out.println(diff);
}
}
recursive function
}
- bhanu July 26, 2016