0% completed
Why is it that the BFS solution has a space complexity of O(min(M, N))? Why does...
Runyao Fan
Aug 21, 2022
Why is it that the BFS solution has a space complexity of O(min(M, N))? Why doesn't the queue size grow beyond min(M, N)?
3
0
Comments
Runyao Fan3 years ago
to add to my question, suppose there is a 2*2 matrix, when we pop (0,0) the queue will have (1, 0), (-1, 0), (0, 1), (0, -1) and a length of 4
Design Gurus3 years ago
Yes, this is due to the constant factor.
You can assume the space complexity will be something like: O( C * min( M, N) ) where 'C' is a constant.
Since we ignore constants for asymptotic analysis, that's why the space complexity is O( min(M, N)).
stephen 3 years ago
to be honest you should write out the entire solution before reducing and simplifying. You do it for the sliding window solution where O(26) is approximated as O(1). Took me a while to write out a 2x2 matrix to understand this.
Douglas 3 years ago
I still don't understand why if someone could share an explanation / visualization or an intuition I would appreciate
Alexander Yang3 years ago
Salah Osman3 years ago
For those who were initially confused like myself,
min( m, n) denotes the smaller of the two numbers m and n (or their common value if m = n). For example, the rank of a 3 x 5 matrix can be no more than 3, and the rank of a 4 x 2 matrix can be no more than 2
This help...
Daniel 3 years ago
Thanks Alexander Yang
On this page
Problem Statement
Try it yourself