written on 28 May 2023
Views : Loading...
Given a continuous stream of numbers, design a data structure that efficiently finds the median of the numbers seen so far.
Algorithm:
import heapq
class MedianFinder:
def __init__(self):
self.lowerHeap = [] # max-heap
self.higherHeap = [] # min-heap
def addNum(self, num):
if len(self.lowerHeap) == len(self.higherHeap):
# Add the number to the max-heap (lowerHeap) and negate it to simulate a max-heap behavior
heapq.heappush(self.lowerHeap, -num)
else:
# Add the number to the min-heap (higherHeap)
heapq.heappush(self.higherHeap, num)
# Balance the heaps if necessary
if self.higherHeap and -self.lowerHeap[0] > self.higherHeap[0]:
# Swap the root elements of both heaps
heapq.heappush(self.higherHeap, -heapq.heappop(self.lowerHeap))
heapq.heappush(self.lowerHeap, -heapq.heappop(self.higherHeap))
def findMedian(self):
if len(self.lowerHeap) == len(self.higherHeap):
# Both heaps have the same size, so the median is the average of the roots
return (-self.lowerHeap[0] + self.higherHeap[0]) / 2.0
else:
# Max-heap (lowerHeap) has more elements, so the median is the root of the max-heap
return -self.lowerHeap[0]
Given an array of integers and a window size, find the median of each window as it slides through the array.
Algorithm:
Define two data structures, a max-heap (also known as a max-priority queue) and a min-heap (min-priority queue). The max-heap will store the smaller half of the numbers in the window, while the min-heap will store the larger half.
Initialize the heaps and create a window of the given size. Insert the first k elements of the array into the heaps. If the window size is odd, insert the first k/2 + 1 elements into the max-heap and the rest into the min-heap. If the window size is even, insert the first k/2 elements into both heaps.
For each subsequent window, do the following:
Find the median for each window:
Repeat step 3 and step 4 until you have processed all windows in the array.
void insert(int val, multiset<int>&low, multiset<int>&high,int &k){
int a = *low.rbegin(); // current median
if (a<val){
high.insert(val);
if (high.size()>k/2){
low.insert(*high.begin());
high.erase(high.find(*high.begin()));
}
} else {
low.insert(val);
if (low.size()>(k+1)/2){
high.insert(*low.rbegin());
low.erase(low.find(*low.rbegin()));
}
}
}
void erase(int val, multiset<int>&low, multiset<int>&high){
if (high.find(val) != high.end()) high.erase(high.find(val));
else low.erase(low.find(val));
if (low.empty()) {
low.insert(*high.begin());
high.erase(high.find(*high.begin()));
}
}
signed main()
{
multiset<int>low,high;
int n,k;
cin>>n>>k;
vector < int > a(n,0);
for (int i = 0; i < n; i++) cin>>a[i];
low.insert(a[0]);
for (int i = 1; i < k; i++) insert(a[i],low,high,k);
cout<<*low.rbegin()<<" ";
for (int i = k; i < n; i++) {
if (k==1){
insert(a[i],low,high,k);
erase(a[i-1],low,high);
}
else{
erase(a[i-k],low,high);
insert(a[i],low,high,k);
}
cout<<*low.rbegin()<<" ";
}
cout<<endl;
return 0;
}
Given a sorted matrix of integers, find the Kth smallest element.
Algorithm:
import heapq
def kthSmallest(matrix, k):
if not matrix or not matrix[0]:
return None
rows, cols = len(matrix), len(matrix[0])
min_heap = []
# Insert elements from the first row into the min-heap
for j in range(cols):
heapq.heappush(min_heap, (matrix[0][j], 0, j))
# Perform K iterations
for _ in range(k):
element, row, col = heapq.heappop(min_heap)
# If there is a valid element in the next column of the current row, insert it into the min-heap
if col + 1 < cols:
heapq.heappush(min_heap, (matrix[row][col + 1], row, col + 1))
# If it's not the last row, insert the element from the next row in the same column
if row + 1 < rows:
heapq.heappush(min_heap, (matrix[row + 1][col], row + 1, col))
return element
Given two sorted arrays, find the K pairs with the smallest sums.
Required Time Complexity : $O(k * log(min(k, n)))$
Algorithm:
import heapq
def kSmallestPairs(nums1, nums2, k):
if not nums1 or not nums2:
return []
heap = []
for i in range(min(k, len(nums1))):
heapq.heappush(heap, (nums1[i] + nums2[0], i, 0))
pairs = []
while k > 0 and heap:
_, i, j = heapq.heappop(heap)
pairs.append([nums1[i], nums2[j]])
if j + 1 < len(nums2):
heapq.heappush(heap, (nums1[i] + nums2[j+1], i, j+1))
k -= 1
return pairs
Given a list of workers with their respective qualities and minimum wage expectations, find the minimum cost to hire K workers such that the ratio of their total quality to total wage is minimized.
This problem is a variant of the classical "Minimum Cost Maximum Flow" problem. This problem can be solved using a combination of sorting and a priority queue.
Algorithm:
import heapq
def mincostToHireWorkers(qualities, wages, K):
workers = sorted([(q, w) for q, w in zip(qualities, wages)], key=lambda x: x[1]/x[0])
heap = []
sum_qualities = 0
min_cost = float('inf')
for q, w in workers:
heapq.heappush(heap, -q)
sum_qualities += q
if len(heap) > K:
sum_qualities += heapq.heappop(heap)
if len(heap) == K:
min_cost = min(min_cost, sum_qualities * w / q)
return min_cost
This problem statement might not be complete
Given a large file containing a sorted list of integers that cannot fit into memory and are split into chunks, find the median of the numbers efficiently.
Kindly Contact me to contribute a solution.
Other Problems :