|
| 1 | +''' |
| 2 | +Date:28/6/23 |
| 3 | +About:Search in 2D sorted array in Python |
| 4 | +
|
| 5 | +Input: |
| 6 | +You are given an m x n integer matrix matrix with the following two properties: |
| 7 | +Each row is sorted in non-decreasing order. |
| 8 | +The first integer of each row is greater than the last integer of the previous row. |
| 9 | +Given an integer target, return true if target is in matrix or false otherwise. |
| 10 | +
|
| 11 | +Time Complexity: |
| 12 | +You must write a solution in O(log(m * n)) time complexity. |
| 13 | +Space Complexity:O(1) |
| 14 | +
|
| 15 | +//Example 1: |
| 16 | +Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 |
| 17 | +Output: true |
| 18 | +//Example 2: |
| 19 | +Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 |
| 20 | +Output: false |
| 21 | +
|
| 22 | +//Explanation |
| 23 | +The method takes two parameters: matrix, which represents the sorted matrix, and target, which is the value we want to find in the matrix. |
| 24 | +
|
| 25 | +The code initializes variables nRows and nCols to store the number of rows and columns in the matrix, respectively. |
| 26 | +
|
| 27 | +The starting position for the search is set to the bottom-left corner of the matrix (row = nRows - 1, col = 0). |
| 28 | +
|
| 29 | +The code enters a while loop that continues as long as the current row index (row) is within the bounds of the matrix (0 to nRows - 1 |
| 30 | +and the current column index (col) is within the bounds of the matrix (0 to nCols - 1). |
| 31 | +
|
| 32 | +Inside the loop, the code retrieves the value at the current position in the matrix (val = matrix[row][col]). |
| 33 | +
|
| 34 | +If the current value (val) == value, the method returns True, indicating that the target is found in the matrix. |
| 35 | +
|
| 36 | +If the current value (val) < target value, it means the target can only be found in the rows above the current row. |
| 37 | +Therefore, the column index (col) is incremented to move to the next column. |
| 38 | +
|
| 39 | +If the current value (val) >target value, it means the target can only be found in the columns to the left of the current column. |
| 40 | +Therefore, the row index (row) is decremented to move to the previous row. |
| 41 | +
|
| 42 | +If the loop completes without finding the target value, the method returns False. |
| 43 | +
|
| 44 | +''' |
| 45 | +class Solution(object): |
| 46 | + def searchMatrix(self, matrix, target): |
| 47 | + nRows = len(matrix) |
| 48 | + nCols = len(matrix[0]) |
| 49 | + row = nRows - 1 |
| 50 | + col = 0 |
| 51 | + while 0 <= row < nRows and 0 <= col < nCols: |
| 52 | + val = matrix[row][col] |
| 53 | + if val == target: |
| 54 | + return True |
| 55 | + elif val < target: |
| 56 | + col += 1 |
| 57 | + else: |
| 58 | + row -= 1 |
| 59 | + return False |
| 60 | + |
| 61 | + |
| 62 | +# Create an instance of the Solution class |
| 63 | +solution = Solution() |
| 64 | + |
| 65 | +# Define the matrix and target value |
| 66 | +matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]] |
| 67 | +target = 3 |
| 68 | + |
| 69 | +# Call the searchMatrix method and print the result |
| 70 | +result = solution.searchMatrix(matrix, target) |
| 71 | +print(result) |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
0 commit comments