有序矩阵中查找第k小的元素 Kth smallest element in a row-wise and column-wise sorted 2D array
来源:互联网 发布:做网页用什么软件 编辑:程序博客网 时间:2024/05/20 11:20
相关问题:[LeetCode] Merge K Sorted Linked Lists
有序矩阵中查找第k小的元素 Kth smallest element in a row-wise and column-wise sorted 2D array
http://www.geeksforgeeks.org/kth-smallest-element-in-a-row-wise-and-column-wise-sorted-2d-array-set-1/
Given an n x n matrix, where every row and column is sorted in non-decreasing order. Find the kth smallest element in the given 2D array.
For example, consider the following 2D array.
10, 20, 30, 40 15, 25, 35, 45 24, 29, 37, 48 32, 33, 39, 50The 3rd smallest element is 20 and 7th smallest element is 30
The idea is to use min heap. Following are detailed step.1) Build a min heap of elements from first row. A heap entry also stores row number and column number.2) Do following k times.…a) Get minimum element (or root) from min heap.…b) Find row number and column number of the minimum element.…c) Replace root with the next element from same column and min-heapify the root.3) Return the last extracted root.
Following is C++ implementation of above algorithm.
// kth largest element in a 2d array sorted row-wise and column-wise#include<iostream>#include<climits>using namespace std; // A structure to store an entry of heap. The entry contains// a value from 2D array, row and column numbers of the valuestruct HeapNode { int val; // value to be stored int r; // Row number of value in 2D array int c; // Column number of value in 2D array}; // A utility function to swap two HeapNode items.void swap(HeapNode *x, HeapNode *y) { HeapNode z = *x; *x = *y; *y = z;} // A utility function to minheapify the node harr[i] of a heap// stored in harr[]void minHeapify(HeapNode harr[], int i, int heap_size){ int l = i*2 + 1; int r = i*2 + 2; int smallest = i; if (l < heap_size && harr[l].val < harr[i].val) smallest = l; if (r < heap_size && harr[r].val < harr[smallest].val) smallest = r; if (smallest != i) { swap(&harr[i], &harr[smallest]); minHeapify(harr, smallest, heap_size); }} // A utility function to convert harr[] to a max heapvoid buildHeap(HeapNode harr[], int n){ int i = (n - 1)/2; while (i >= 0) { minHeapify(harr, i, n); i--; }} // This function returns kth smallest element in a 2D array mat[][]int kthSmallest(int mat[4][4], int n, int k){ // k must be greater than 0 and smaller than n*n if (k <= 0 || k > n*n) return INT_MAX; // Create a min heap of elements from first row of 2D array HeapNode harr[n]; for (int i = 0; i < n; i++) harr[i] = {mat[0][i], 0, i}; buildHeap(harr, n); HeapNode hr; for (int i = 0; i < k; i++) { // Get current heap root hr = harr[0]; // Get next value from column of root's value. If the // value stored at root was last value in its column, // then assign INFINITE as next value int nextval = (hr.r < (n-1))? mat[hr.r + 1][hr.c]: INT_MAX; // Update heap root with next value harr[0] = {nextval, (hr.r) + 1, hr.c}; // Heapify root minHeapify(harr, 0, n); } // Return the value at last extracted root return hr.val;} // driver program to test above functionint main(){ int mat[4][4] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {25, 29, 37, 48}, {32, 33, 39, 50}, }; cout << "7th smallest element is " << kthSmallest(mat, 4, 7); return 0;}Output:
7th smallest element is 30Time Complexity: The above solution involves following steps.
1) Build a min heap which takes O(n) time
2) Heapify k times which takes O(kLogn) time.
Therefore, overall time complexity is O(n + kLogn) time.The above code can be optimized to build a heap of size k when k is smaller than n. In that case, the kth smallest element must be in first k rows and k columns.
- 有序矩阵中查找第k小的元素 Kth smallest element in a row-wise and column-wise sorted 2D array
- 378. Kth Smallest Element in a Sorted Matrix(查找矩阵中第k小的数)
- 230. Kth Smallest Element in a BST | 二叉排序树中查找第K小的数
- Kth Smallest Element in a BST 寻找二叉排序树中第k小元素
- Kth Smallest Element in a BST 找二叉搜索树中的第k小的元素
- LeetCode OJ 之 Kth Smallest Element in a BST(BST中的第k小元素)
- leetcode_230. Kth Smallest Element in a BST 求二叉搜索树中的第k小的元素,中序遍历法
- 378.leetcode Kth Smallest Element in a Sorted Matrix(medium)[堆求第K小的 ]
- [C++]Kth Smallest Element in a BST 在一个二叉排序树中找第k小的元素
- leetcode230---Kth Smallest Element in a BST(BST中寻找第K小)
- LeetCode-230. Kth Smallest Element in a BST (JAVA)二叉树第k小的数字
- 编程之 最小的K个数 216 Kth Largest Element in an Array 378. Kth Smallest Element in a Sorted Matrix
- Leetcode 215 Kth Largest Element in an Array 数组中第k大的元素
- 【二叉树】BST第K小值【230. Kth Smallest Element in a BST】
- [leetcode 230]Kth Smallest Element in a BST----求二叉搜索树的第K小值
- Kth smallest element (in a sorted matrix ; In a BST tree ; In two sorted array)
- Kth Smallest Element in a Sorted Matrix
- Kth Smallest Element in a Sorted Matrix
- android developer
- 我的第一个FPGA软核: Small Simple FPU
- UIApplication解析
- Mathematical aspects of finite element I (有限元的数学概念)
- 【编程之美】微软技面心得
- 有序矩阵中查找第k小的元素 Kth smallest element in a row-wise and column-wise sorted 2D array
- [leetcode] Decode Ways
- Remove Duplicates from Sorted List II
- Reverse Linked List
- appcompat_v7 与 actionbarsherlock 冲突解决/移除项目中的appcompat_v7
- python 函数小结
- R apply, lapply, sapply, mapply, tapply functions
- OpenWrt的差异化特性介绍
- Reverse Linked List II