Sponsored
Sponsored
By using a min-heap (priority queue), you can efficiently extract the smallest elements one by one. Initially, insert the first element of each row into the heap. Then, repeat the process of extracting the smallest element and inserting the next element from the corresponding row. Continue this until you extract the k-th smallest element.
Time Complexity: O(k log n)
, where n
is the size of each row/column.
Space Complexity: O(n)
, due to the maintained heap.
1#include <stdio.h>
2#include <stdlib.h>
3#include <limits.h>
4
5struct HeapNode {
6 int val;
7 int r;
8 int c;
9};
10
11void minHeapify(struct HeapNode heap[], int i, int heap_size) {
12 int smallest = i;
13 int left = 2 * i + 1;
14 int right = 2 * i + 2;
15
16 if (left < heap_size && heap[left].val < heap[smallest].val)
17 smallest = left;
18 if (right < heap_size && heap[right].val < heap[smallest].val)
19 smallest = right;
20 if (smallest != i) {
21 struct HeapNode temp = heap[i];
22 heap[i] = heap[smallest];
23 heap[smallest] = temp;
24 minHeapify(heap, smallest, heap_size);
25 }
26}
27
28int kthSmallest(int** matrix, int n, int k) {
29 struct HeapNode* heap = malloc(n * sizeof(struct HeapNode));
30 for (int i = 0; i < n; i++) {
31 heap[i].val = matrix[i][0];
32 heap[i].r = i;
33 heap[i].c = 0;
34 }
35
36 for (int i = n / 2 - 1; i >= 0; i--)
37 minHeapify(heap, i, n);
38
39 struct HeapNode hr;
40 for (int i = 0; i < k; i++) {
41 hr = heap[0];
42 int nextVal = (hr.c < n - 1) ? matrix[hr.r][hr.c + 1] : INT_MAX;
43 heap[0].val = nextVal;
44 heap[0].c += 1;
45 minHeapify(heap, 0, n);
46 }
47
48 int result = hr.val;
49 free(heap);
50 return result;
51}
52
53int main() {
54 int* matrix[3];
55 int a[] = {1, 5, 9};
56 int b[] = {10, 11, 13};
57 int c[] = {12, 13, 15};
58 matrix[0] = a;
59 matrix[1] = b;
60 matrix[2] = c;
61 printf("%d\n", kthSmallest(matrix, 3, 8));
62 return 0;
63}
In this C solution, we maintain a min-heap (priority queue) of the smallest elements from each row of the matrix. Initially, we insert the first element of each row into the heap. We then extract the smallest element from the heap and insert the next element from the same row into the heap. We repeat this process until we find the k-th smallest element.
This approach uses binary search over the range of possible values in the matrix to find the k-th smallest element. The idea is to repeatedly narrow the range by counting the number of elements in the matrix that are less than or equal to the current middle value. Depending on this count, adjust the binary search range to hone in on the k-th smallest element.
Time Complexity: O(n log(max-min))
, resulting from binary search log sweeps invoking count linear phase.
Space Complexity: O(1)
, since alterations occur in-place within computation limits of primitive variables.
In this JavaScript solution, the binary search method revolves around engaging midpoint checks against sorted elements formed in the matrix. Precise counts adjust mid-focus undercurrents quickly converging sectional settings for completion bound at minimal redundancy detected, offering scalable identification geared towards conclusive k-th valuation.