当前位置:   article > 正文

LeetCode:找出矩阵中第k个小的元素_矩阵中第k小的数

矩阵中第k小的数
  1. Given a nxn matrix where each of the rows and columns are sorted in ascending order,
  2. find the kth smallest element in the matrix.
  3. Note that it is the kth smallest element in the sorted order,not the kth distinct element.
  4. Example 1:
  5. Input:matrix=[[1,5,9],
  6. [10,11,13],
  7. [12,13,15]],k=8
  8. Output:13
  9. Note:You may assume k is always valid,1<=k<=n2.
  10. 题目大意:
  11. 给定一个nxn矩阵,其中每行每列元素都是按照升序进行排序,找出矩阵中第k小的元素.注意它是排序后的第k个小元素,而不是
  12. 第k个元素.
  13. 说明:你可以假设k的值永远是有效的,1<=k<=n2.
  14. 解题思路:
  15. 这道题想到的是优先队列,依次把矩阵中的元素推入到优先队列中,维护一个最小堆,一旦优先队列中里面有k个元素,则找到了
  16. 答案.
  17. 最优解是二分查找,从矩阵可以看出左上角的那个元素是最小的,右上角的元素是最大的.在这个解空间里面二分搜索所有值,找到
  18. 第k小的元素.判断是否找到的条件是,在矩阵中比mid小的元素个数等于k.不断的逼近low,使得low==high的时候,就是找到
  19. 了最k小的元素.
  20. 根据题例,作如下分析:
  21. Example :
  22. [[2,6,8],
  23. [3,7,10],
  24. [5,8,11]],k=5
  25. Step1:
  26. middle=(start+end)/2=(2+11)/2=6
  27. smallest number greater than the middle(6):7
  28. biggest number less than or equal to the middle(6):6
  29. number of elements less than or equal to the middle(6)=4
  30. As there are only 4 elements less than or equal to the middle,and we are looking for 5th
  31. smallest number,so let's search higher and update our 'start' to the smallest number
  32. greater than the middle.
  33. Step2:
  34. middle=(start+end)/2=(7+11)/2=9
  35. smallest number greater than the middle(9):10
  36. biggest number less than or equal to the middle(9):8
  37. number of elements less than or equal to the middle(9)=7
  38. As there are only 7 elements less than or equal to the middle,and we are looking for 5th
  39. smallest number,so let's search lower and update our 'end' to the biggest number
  40. less than or equal to the middle.
  41. Step3:
  42. middle=(start+end)/2=(7+8)/2=7
  43. smallest number greater than the middle(7):8
  44. biggest number less than or equal to the middle(7):8
  45. number of elements less than or equal to the middle(7)=5
  46. As there are only 5 elements less than or equal to the middle therefore '7',which is the
  47. biggest number less than or equal to the middle,is our required number.
  1. package main
  2. import (
  3. "container/heap"
  4. "fmt"
  5. )
  6. /*二分搜索*/
  7. func kthSmallest(matrix [][]int,k int)int{
  8. m,n,low:=len(matrix),len(matrix[0]),matrix[0][0]
  9. high,mid:=matrix[m-1][n-1],0
  10. for low<high{
  11. mid=low+(high-low)>>1
  12. /*若count比k小,则在大值的那一半继续二分查找*/
  13. if counterKthSmall(m,n,mid,matrix)>=k{
  14. high=mid
  15. }else{
  16. low=mid+1
  17. }
  18. }
  19. return low
  20. }
  21. func counterKthSmall(m,n,mid int,matrix [][]int)int{
  22. count,j:=0,n-1
  23. /*每次循环统计比mid值小的元素个数*/
  24. for i:=0;i<m;i++{
  25. for j>=0&&mid<matrix[i][j]{
  26. j--/*遍历每行中比mid小的元素的个数*/
  27. }
  28. count+=j+1
  29. }
  30. return count
  31. }
  32. /*优先队列*/
  33. func kthSmallest1(matrix [][]int,k int)int{
  34. if len(matrix)==0||len(matrix[0])==0{return 0}
  35. pq:=&pq{data:make([]interface{},k)}
  36. heap.Init(pq)
  37. for i:=0;i<len(matrix);i++{
  38. for j:=0;j<len(matrix[0]);j++{
  39. if pq.Len()<k{
  40. heap.Push(pq,matrix[i][j])
  41. }else if matrix[i][j]<pq.Head().(int){
  42. heap.Pop(pq)
  43. heap.Push(pq,matrix[i][j])
  44. }else{
  45. break
  46. }
  47. }
  48. }
  49. return heap.Pop(pq).(int)
  50. }
  51. type pq struct{
  52. data []interface{}
  53. len int
  54. }
  55. func (p *pq) Len()int{
  56. return p.len
  57. }
  58. func (p *pq) Less(a,b int) bool{
  59. return p.data[a].(int)>p.data[b].(int)
  60. }
  61. func (p *pq) Swap(a,b int){
  62. p.data[a],p.data[b]=p.data[b],p.data[a]
  63. }
  64. func (p *pq) Push(o interface{}){
  65. p.data[p.len]=o
  66. p.len++
  67. }
  68. func (p *pq) Head()interface{}{
  69. return p.data[0]
  70. }
  71. func (p *pq) Pop()interface{}{
  72. p.len--
  73. return p.data[p.len]
  74. }
  75. func main(){
  76. matrix:=[][]int{{1,5,9},{10,11,13},{12,13,15}}
  77. fmt.Println(kthSmallest1(matrix,8))
  78. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/982746
推荐阅读
相关标签
  

闽ICP备14008679号