ELECTRONICS SHOP HACKERRANK
APPROACH 1:
TIME: O(N^2)
SPACE:O(NLOG(K) ie NLOG(1) as k=1)[ArrayList and Priority Queue]
static int getMoneySpent(int[] arr1, int[] arr2, int a) {
int b=arr1.length;
int c=arr2.length;
int start=0;
int sum=0;
int end=b-1;
ArrayList<Integer> aa=new ArrayList<Integer>();
while(start<=end){
for(int i=0;i<c;i++){
sum=arr1[start]+arr2[i];
aa.add(sum);
sum=0;
}
start++;
}
//System.out.print(aa+" ");
PriorityQueue<Integer> q=new PriorityQueue<Integer>();
q.add(-1);
//System.out.println(q+" ");
for(int i=0;i<aa.size();i++){
//System.out.println("Peek is: "+q.peek());
//System.out.println("Element is: "+aa.get(i));
if((q.peek()<aa.get(i)) && (aa.get(i)<=a)){
//System.out.println("New Element is: "+aa.get(i));
q.remove();
q.add(aa.get(i));
//System.out.println("Queue is : "+q);
}
//else
//System.out.println("No change");
}
if(q.peek()==-1){
// System.out.print("No Element");
//System.out.print(q+" ");
return -1;
}
else{
//System.out.print(q+" ");
int m=q.peek();
return m;
}
}
Thanks for Reading 😇.
Comments
Post a Comment