在矩阵中搜索值的算法设计
这个问题不是作业,只是出于个人兴趣,主要是我的好奇心。
我的教授在课堂上谈到了这个问题一会儿,但他没有谈太多。下面是问题:
给定一个 mxn 矩阵 A,其整数元素沿水平和垂直方向排序 分别方向。我需要开发一个递归程序来搜索查询值是否为 位于 A 中。讨论您的设计的时间复杂度。
所以我想了一会儿这个问题。我的第一种方法是检查基本情况:第一个元素和最后一个元素
检查第一个元素是否> >项目检查最后一个元素是否< item
item 是我想要找到的
这是虚数矩阵:( x 可以是任何数字,但是这个矩阵是垂直和水平排序的)
first x x x x
x x x x x
x x mid x x
x x x x x
x x x x last
在我检查基本情况并确保我想要找到的“item”在里面之后这个矩阵的范围,我不知道是否可以从矩阵中的“中间”检查(如二分搜索)。如果项目<中,然后关注左半部分。如果项目>中,然后关注右半部分。
但是,然后我尝试制作一个如下所示的矩阵,我的“项目”是 10
1 2 3 4 5
2 4 7 8 9
3 6 10 11 12
如果我按照我之前所说的方式操作:由于 10 比中间的“7”大,所以我专注于右侧部分。然后我检查“8”,因为10比“8”大,所以我搜索正确的部分。但 10 不正确......
任何人都可以给我想法或见解如何解决这个问题吗?多谢
This question is not a homework, it is just out of personal interest and mainly my curiosity
My professor talked about this question for a little while during class, but he didn't talk much about this. And below is the question:
Given an m x n matrix A whose integer elements are sorted along the horizontal and vertical
direction respectively. I need to develop a recursive program to search if a query value a
is in A. Discuss the time complexity of your design.
So I thought about this for a while. My first approach is to check the base case: the first element and last element
check if first element > item check if last element < item
item is what I want to find
This is the imaginary matrix: ( x can be any number, but this matrix is sort vertically and horizontally)
first x x x x
x x x x x
x x mid x x
x x x x x
x x x x last
After I check the base case and make sure the "item" I want to find is inside the range of this matrix, I don't know if it is alright to check from the "mid" in the matrix ( like binary search). If item < mid , then focus on left half. If item > mid, then focus on right half.
But, then I tried to make a matrix like below and my "item" is 10
1 2 3 4 5
2 4 7 8 9
3 6 10 11 12
If I follow the way I said before: since 10 is larger than the middle "7", I focus on right part. then I check "8", since 10 is larger than "8", I search for right part. But 10 is not in the right ...
Can anyone give me idea or insight how to solve this question? Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从左下角开始(在您的示例中包含 3 的那个)。
true
。false
。这是
O(n + m)
,其中n
和m
是矩阵中的行数和列数。这是因为在每一步中,您都完全排除了整行或整列。Start at the lower left corner (the one with 3 in it in your example).
true
.false
.This is
O(n + m)
, wheren
andm
are the number of lines and columns in your matrix. This is because at each step, you completely rule out an entire row or column.时间上的最佳解决方案是
O(1)
,并且是使用哈希表来跟踪矩阵中的哪些元素。如果空间有问题,也可以使用布隆过滤器。然而,由于它们已分类,因此可能不值得添加所有机器。
我认为问题正在寻找的解决方案是一个
O(N)
解决方案(其中矩阵的大小为N
xN
);从左上角到左下角再到右下角,直到找到大于查询的元素。然后,您通过向右上角蠕动来搜索“水平曲线”,每次执行比较以查看是否超出或低于查询(向右或向上,具体取决于)。您可以考虑的另一种方法是跟踪窗口
lower_c
lower_c查询<每列
c
的higher_c
,从左到右。该窗口的大小始终为 2。Best solution time-wise is
O(1)
and is to just keep track of which elements are in the matrix using a hash table. Could also use a Bloom filter if space is an issue.However because they're sorted, it may not be worth adding all the machinery.
The solution the problem is looking for I think is an
O(N)
solution (where the matrix is sizeN
xN
); where you proceed from the top-left to bottom-left to bottom-right until you find an element larger than your query. Then you search the "level curve" by squirming towards the top-right, each time performing a comparison to see if you've overshot or undershot your query (going right or up depending).Another way you can think about this is keeping track of the window
lower_c
< query <higher_c
for each columnc
, going left to right. This window is always of size 2.