Using Binary search on 2D matrix
Anonymous
We can envision matrix as a array that's broken down. If we have 3x3 it goes from indexes 0 to 8. (Because it has 9 values total). Formula for getting some matrix index (list index) is: columns * x + y. With constraints of x < M and y < N we can derive values. In short it is for any valid given matrix index the x is x / columns and y is y % columns. In short: To convert into list index use: columns * i + j To convert from list index into matrix indices it's (x: l_idx / columns, l_idx % columns) We can now use binary search. Go from l = 0, to r = largest l_idx When we get middle list index, convert it into indices. Get value of matrix and do condition. If equal target, exit otherwise binary search.
Check out your Company Bowl for anonymous work chats.