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

Given a nxn matrix where each of the rows and columns are sorted in ascending order,
find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order,not the kth distinct element.
Example 1:
Input:matrix=[[1,5,9],
              [10,11,13],
              [12,13,15]],k=8
Output:13
Note:You may assume k is always valid,1<=k<=n2.
题目大意:
给定一个nxn矩阵,其中每行每列元素都是按照升序进行排序,找出矩阵中第k小的元素.注意它是排序后的第k个小元素,而不是
第k个元素.
说明:你可以假设k的值永远是有效的,1<=k<=n2.

解题思路:
这道题想到的是优先队列,依次把矩阵中的元素推入到优先队列中,维护一个最小堆,一旦优先队列中里面有k个元素,则找到了
答案.
最优解是二分查找,从矩阵可以看出左上角的那个元素是最小的,右上角的元素是最大的.在这个解空间里面二分搜索所有值,找到
第k小的元素.判断是否找到的条件是,在矩阵中比mid小的元素个数等于k.不断的逼近low,使得low==high的时候,就是找到
了最k小的元素.

根据题例,作如下分析:
Example :
[[2,6,8],
 [3,7,10],
 [5,8,11]],k=5
Step1:
middle=(start+end)/2=(2+11)/2=6
smallest number greater than the middle(6):7
biggest number less than or equal to the middle(6):6
number of elements less than or equal to the middle(6)=4
As there are only 4 elements less than or equal to the middle,and we are looking for 5th
smallest number,so let's search higher and update our 'start' to the smallest number
greater than the middle.

Step2:
middle=(start+end)/2=(7+11)/2=9
smallest number greater than the middle(9):10
biggest number less than or equal to the middle(9):8
number of elements less than or equal to the middle(9)=7
As there are only 7 elements less than or equal to the middle,and we are looking for 5th
smallest number,so let's search lower and update our 'end' to the biggest number
less than or equal to the middle.

Step3:
middle=(start+end)/2=(7+8)/2=7
smallest number greater than the middle(7):8
biggest number less than or equal to the middle(7):8
number of elements less than or equal to the middle(7)=5
As there are only 5 elements less than or equal to the middle therefore '7',which is the
biggest number less than or equal to the middle,is our required number.
package main

import (
	"container/heap"
	"fmt"
)

/*二分搜索*/
func kthSmallest(matrix [][]int,k int)int{
	m,n,low:=len(matrix),len(matrix[0]),matrix[0][0]
	high,mid:=matrix[m-1][n-1],0
	for low<high{
		mid=low+(high-low)>>1
		/*若count比k小,则在大值的那一半继续二分查找*/
		if counterKthSmall(m,n,mid,matrix)>=k{
			high=mid
		}else{
			low=mid+1
		}
	}
	return low
}

func counterKthSmall(m,n,mid int,matrix [][]int)int{
	count,j:=0,n-1
	/*每次循环统计比mid值小的元素个数*/
	for i:=0;i<m;i++{
		for j>=0&&mid<matrix[i][j]{
			j--/*遍历每行中比mid小的元素的个数*/
		}
		count+=j+1
	}
	return count
}

/*优先队列*/
func kthSmallest1(matrix [][]int,k int)int{
	if len(matrix)==0||len(matrix[0])==0{return 0}
	pq:=&pq{data:make([]interface{},k)}
	heap.Init(pq)
	for i:=0;i<len(matrix);i++{
		for j:=0;j<len(matrix[0]);j++{
			if pq.Len()<k{
				heap.Push(pq,matrix[i][j])
			}else if matrix[i][j]<pq.Head().(int){
				heap.Pop(pq)
				heap.Push(pq,matrix[i][j])
			}else{
				break
			}
		}
	}
	return heap.Pop(pq).(int)
}

type pq struct{
	data []interface{}
	len int
}

func (p *pq) Len()int{
	return p.len
}

func (p *pq) Less(a,b int) bool{
	return p.data[a].(int)>p.data[b].(int)
}

func (p *pq) Swap(a,b int){
	p.data[a],p.data[b]=p.data[b],p.data[a]
}

func (p *pq) Push(o interface{}){
	p.data[p.len]=o
	p.len++
}

func (p *pq) Head()interface{}{
	return p.data[0]
}

func (p *pq) Pop()interface{}{
	p.len--
	return p.data[p.len]
}
func main(){
	matrix:=[][]int{{1,5,9},{10,11,13},{12,13,15}}
	fmt.Println(kthSmallest1(matrix,8))
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
给定一个整组 nums 和一个目标值 target,要求在找出两个的和等于目标值,并返回这两个的索引。 思路1:暴力法 最简单的思路是使用两层循环遍历组的所有组合,判断两个的和是否等于目标值。如果等于目标值,则返回这两个的索引。 此方法的时间复杂度为O(n^2),空间复杂度为O(1)。 思路2:哈希表 为了优化时间复杂度,可以使用哈希表来存储元素和对应的索引。遍历组,对于每个元素nums[i],我们可以通过计算target - nums[i]的值,查找哈希表是否存在这个差值。 如果存在,则说明找到了两个的和等于目标值,返回它们的索引。如果不存在,将当前元素nums[i]和它的索引存入哈希表。 此方法的时间复杂度为O(n),空间复杂度为O(n)。 思路3:双指针 如果组已经排序,可以使用双指针的方法来求解。假设组从小到大排序,定义左指针left指向组的第一个元素,右指针right指向组的最后一个元素。 如果当前两个指针指向的的和等于目标值,则返回它们的索引。如果和小于目标值,则将左指针右移一位,使得和增大;如果和大于目标值,则将右指针左移一位,使得和减小。 继续移动指针,直到找到两个的和等于目标值或者左指针超过了右指针。 此方法的时间复杂度为O(nlogn),空间复杂度为O(1)。 以上三种方法都可以解决问题,选择合适的方法取决于具体的应用场景和要求。如果组规模较小并且不需要考虑额外的空间使用,则暴力法是最简单的方法。如果组较大或者需要优化时间复杂度,则哈希表或双指针方法更合适。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路上的追梦人

您的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值