17. Binary Search (on matrices)

 1. Find index of 1st row with max 1s-

given each row is sorted & matrix is a binary matrix.
if none exists, return -1
---------
Brute- time: O(n^2)
scan each row & keep a count of no. of 1s & highest is answer

--------
Optimal- time: O(NlogN)
iterate for each row in outer loop linearly
in inner loop, use binary search to find first occurrence of 1
--------------------------------------------------------------------------------------------------------------
2. Search a 2d Matrix-1
(Find K in a sorted 2d matrix)
--------------------------------------------------------
Brute-        time: O(r*c)                 space: O(1)

scan through entire matrix searching for the target
---------------------------------------------------------
Better-      time: O(R + C)              space: O(1)

1. instead of scanning each row.
2. first, we will linearly check which is the row which may contain target element, by checking target element fits properly within bounds of which row.
3. then, we linearly search for target element in this row.
---------------------------------------------------------
Optimal-  time: O(logR + logC)    space: O(1)

in better solution where we do 2 linear search scans,
instead we can do 2 binary search scans.
Further, we can reduce these 2 binary search scans into 1
Below is code for 2 binary searches reduced into 1
--------------------------------------------------------------------------------------------------------------
Note-
Conversion between a 2d matrix and a flattened array formed from this matrix-
say, array has indices starting from low upto high & mid is an index in between then:
                                          arr[mid] => mat[mid / c][mid % c]
---------------------------------------------------------------------------------------------------------------
3. Search a 2d matrix-2
(rows & cols are sorted individually)
--------------------------------------------------------
Brute-  time: O(rc)
simple linear scan through entire matrix

-------------------------------------------------------
Better-  time: O(RlogC)
use binary search in each row
--------------------------------------------------
Optimal- time: O(R+C)⭐

1. we start from either of the two positions: bottom-left or top-right, out of the 4 corner positions.
2. This is because in binary search we need place where can go in both directions- increasing as well as decreasing and in bottom left: elements inc in right and dec on top & at top right, elements decrease in left and increase in bottom.
3. Which is not the case in top left or bottom right corner

---------------------------------------------------------------------------------------------------------------
4. Find Peak element in matrix-⭐

Brute- time: O(RC)

Do a linear scan & for each element compare it with its 4 neighbours.
or, find max element in matrix.
--------------------------------------------------------
Optimal- time: O(RlogC) ⭐

Binary search on col indices
For each col=mid, find max element in that column
Check if this max element in this column is peak or not by comparing with left & right.

if this element < left => you are near green dot => peak is leftwards =>  shift to left
else , shift right as peak maybe beyond orange dot

-------------------------------------------------------------------------------------------------------------------------
5. Matrix Median-⭐
(Row wise sorted)
r=odd & c=odd, always
Brute-  time : O(RC)  space: O(RC)
store all elements in a vector
sort them & now return element at n/2th position

--------------------------
Optimal- time: O[ RlogC*log(maxVal) ] ⭐⭐

use binary search on median
------------------------------------------
in 1-based indexing , median(say, x) is (n/2 +1)th element, thus it has (n/2 + 1) elements which are <= x
thus, we guess a median (let x)
if x < correct median => x has fewer than (n/2 + 1) elements which are <=x
if x > correct median => x has atleast (n/2 + 1) elements which are <=x
hence, monotonic quantity => count(elements <= guessed median, x) 
           check function => checks if this count >= (n/2 +1) or not

if x is larger than all values >x also satisfy condition => hence look for smaller x for ans

search space for a median is minimum and maximum element in matrix.

for each x we find how many elements are <=x using the fact: each row is sorted
hence use Upper bound
----------------------------------
---------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

30.) DP on subsequences

19. Strings (LB)

32. Binary Trees-2 (bfs/dfs med problems)