Nice. I did it recursively using a binary search type thing, searching smaller matrices until result is found. Was more complicated but did get it done. Yours is simpler, glad a checked your channel
MY MIND IS BLOWN!!!!!. This algo works on search 2d matrix 1 as well. In my understanding, it works as long as the columns are sorted, if not we would have to check if the target exists in the range of each row, then binary search through it to find it.
You could also start from the upper right. But I don't think it would work if you started from upper left or lower right. At the lower left and upper right, it is certain that at least one element will be smaller or one element will be greater from your current spot - this isn't the case with the upper left and lower right. rows = len(matrix) cols = len(matrix[0]) #start upper right i,j = 0, cols-1 while 0
Nice. I did it recursively using a binary search type thing, searching smaller matrices until result is found. Was more complicated but did get it done. Yours is simpler, glad a checked your channel
pretty amazing how simplicity can be so beautiful yet powerful, super clever approach.
MY MIND IS BLOWN!!!!!. This algo works on search 2d matrix 1 as well. In my understanding, it works as long as the columns are sorted, if not we would have to check if the target exists in the range of each row, then binary search through it to find it.
You could also start from the upper right. But I don't think it would work if you started from upper left or lower right. At the lower left and upper right, it is certain that at least one element will be smaller or one element will be greater from your current spot - this isn't the case with the upper left and lower right.
rows = len(matrix)
cols = len(matrix[0])
#start upper right
i,j = 0, cols-1
while 0
@@janmichaelaustria620 is time O(m + n) or m*n
@@sneezygibz6403 in the worst case we go all the way up (n) then all the way left (m). So upper bounded by O(m+n).
glad that I found your channel!
can't believe it, it just abruptly ends and solved the problem
thank you for this
can I switch the order of the if statements? like I check if > then < then ==.
U a God
Could you please your thought when you first see this type of problem?
WOW
niceeee but why is it O(N+M) ??
taking N as number of rows, M as number of columns, the worst case complexity occurs if target is at top right
FUCK. SO SIMPLE.