Posts

Showing posts with the label Priority Queue

Merge two binary Max heaps Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/merge-two-binary-max-heap0144/1# It is also the problem of the day for  Solution: class Solution{     public int[] mergeHeaps(int[] a, int[] b, int n, int m) {         PriorityQueue<Integer>pq=new PriorityQueue<Integer>(Collections.reverseOrder());         for(int i=0;i<a.length;i++){             pq.add(a[i]);         }         for(int j=0;j<b.length;j++){             pq.add(b[j]);         }         int arr[]=new int[pq.size()];         int index=0;         while(!pq.isEmpty()){             arr[index++]=pq.remove();         }         return arr;     } } TOTAL TEST CASES:10037 TIME COMPLEXITY:O(Max...

Winner Of An Election Geeks For Geeks

 class Solution {     //Function to return the name of candidate that received maximum votes.     public static String[] winner(String arr[], int n)     {         TreeMap<String,Integer> map=new TreeMap<String,Integer>();         for(int i=0;i<arr.length;i++){             if(map.containsKey(arr[i])){                 map.put(arr[i],map.get(arr[i])+1);             }             else                 map.put(arr[i],1);         } PriorityQueue<Integer> pq=new PriorityQueue<Integer>(); int a=0; for (Map.Entry<String, Integer> entry : map.entrySet()) { a=entry.getValue(); if(pq.isEmpty()){ pq.add(a); } else{ if(pq.peek()<a){ pq.remove(); pq.ad...

Median Of Two Sorted Arrays LeetCode And Geeks For Geeks

 class Solution {     public double findMedianSortedArrays(int[] nums1, int[] nums2) {         int x=0;         int y=0;         int n=nums1.length;         int m=nums2.length;         PriorityQueue<Integer> pq=new PriorityQueue<Integer>();         while(x<nums1.length && y<nums2.length){             if(nums1[x]<nums2[y]){                 pq.add(nums1[x]);                 x++;             }             else if(nums1[x]>nums2[y]){                 pq.add(nums2[y]);                 y++;             }             else{ ...

Merge Without Using Extra Space Geeks For Geeks

class Solution {     public void merge(int arr1[], int arr2[], int n, int m) {                          PriorityQueue<Integer> pq=new PriorityQueue<Integer>(); int x=0; int y=0; while(x<n && y<m){ if(arr1[x]<arr2[y]){ pq.add(arr1[x]); x++; } else if(arr1[x]==arr2[y]){ pq.add(arr1[x]); pq.add(arr2[y]); x++; y++; } else{ pq.add(arr2[y]); y++; } } for(;x<n;x++){ pq.add(arr1[x]); } for(;y<m;y++){ pq.add(arr2[y]); } int index=0; while(index<n){ arr1[index++]=pq.remove(); } index=0; while(index<m){ arr2[index++]=pq.remove(); }     } } Approach: Here we will traverse both the list together and see for smaller element in both lists and simultaneously add it to the priority queue. Value for the pointer will be increased for the list ...

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[...

K Largest Element LeetCode

 Approach 1: Using ArrayList and Sorting    TIME : O(NLOGN) [LeetCode Time: 5 ms] SPACE: O(N)                [LeetCode Space 39.2MB] class Solution {     public int findKthLargest(int[] arr, int k) {         ArrayList<Integer> aa=new ArrayList<Integer>();         for(int i=0;i<arr.length;i++){             aa.add(arr[i]);         }         Collections.sort(aa);         return (aa.get(aa.size()-k));     } } ========================================================================= Approach 2: Using Priority Queue and getting the peak element TIME: O(NLOGK) [LeetCode Time: 2ms] SPACE: O(K)              [LeetCode Space: 39.8MB] class Solution {     public int findKthLargest(int[] arr, int k) {         ...

K largest Elements Geeks For Geeks

 class Solution {     //Function to return k largest elements from an array.     public static ArrayList<Integer> kLargest(int arr[], int n, int k)     {         PriorityQueue <Integer> p=new PriorityQueue<Integer>(); for(int i=0;i<k;i++){ p.add(arr[i]); } for(int i=k;i<n;i++){ if(p.peek()<arr[i]){ p.remove(); p.add(arr[i]); } } ArrayList<Integer> aa=new ArrayList<Integer>(); aa.addAll(p); Collections.sort(aa,Collections.reverseOrder()); return aa;     } } Using Priority Queue Time: O(NLOGK) [Sorting of K elements list] [GFG Time : 3.6/6.8] Space: O(K) [Using ArrayList additionally of size k] {Here we will be adding the first k elements in the priority queue. Then for the next (n-k) elements, we will compare with the peek element of the queue. If the peek element is small then we will remove it from the queue and add the element from ...