<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p>

<ul>
	<li>Integers in each row are sorted in ascending from left to right.</li>
	<li>Integers in each column are sorted in ascending from top to bottom.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[i].length</code></li>
	<li><code>1 &lt;= n, m &lt;= 300</code></li>
	<li><code>-10<sup>9</sup> &lt;= matrix[i][j] &lt;= 10<sup>9</sup></code></li>
	<li>All the integers in each row are <strong>sorted</strong> in ascending order.</li>
	<li>All the integers in each column are <strong>sorted</strong> in ascending order.</li>
	<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        # start with brute force approach -
        # say we're looking for 5: 
        # 1. starting from 0, 0, go right as far as number < target
        # 2. then, go down as far as number < target
        # 3. our ans
        # Try another number - 13
        # 1. go to 11
        # 2. end at 12
        # 3. go left to 8, then down to 9
        # 4. go left to 6, then down to 13
        # binary search can speed up the intial process, but maybe not get us to solution
        # another idea - what if we use two pointers to start
        # max possible on top and side
        # then, go down/left depending on if greater/smaller/equal to target
        # ex: 5
        # 4 and 3
        # go down from 4 - 5 is there
        # ex: 13
        # 11 and 10
        # second pointer doesn't seem to contribute much
        # unless... maybe it gives us the row/col we need
        # and we can bin search to opitimize
        # fails with 16 
        # lets code the trickle down approach



        x, y = 0, 0
        if matrix[x][y] == target:
            return True
        # optimize with bin search later
        # iterate along first row until greatest < target (or == target)
        while matrix[x][y] < target and x < len(matrix) - 1:
            print(x, y)
            x += 1
            print(x, y)
            if matrix[x][y] == target:
                return True
        y += 1
        while True:
            if x < 0 or y >= len(matrix[0]):
                return False
            elif matrix[x][y] == target:
                return True
            elif matrix[x][y] > target:
                x -= 1
            elif matrix[x][y] < target:
                y += 1
        return False
        
        

23100