Easy to understand C++ Solution ✅ | Beginner Friendly 🔥🔥🔥
Problem Breakdown\nThe problem is to search for a target value in a 2D matrix where:\n\n1. Each row is sorted in ascending order from left to right.\n2. Each co
# Problem Breakdown\nThe problem is to search for a target value in a 2D matrix where:\n\n1. Each row is sorted in ascending order from left to right.\n2. Each column is sorted in ascending order from top to bottom.\n\nGiven these properties, we can take advantage of the matrix\'s structure to devise an efficient search strategy.\n\n# Intuition\n- The top-right corner of the matrix is a strategic starting point because:\n    - It allows us to leverage the matrix\'s sorted properties effectively.\n    - If the current element is greater than the target, all elements in the same column below it will also be greater, so we move left.\n    - If the current element is less than the target, all elements in the same row to the left of it will also be less, so we move down.\n- This method avoids unnecessary traversal and directly narrows down the search space efficiently.\n\n# Approach\n1. Start from the Top-Right Corner:\n\n    - Initialize two indices: `rowindex` to `0` (the first row) and `colindex` to `col-1` (the last column).\n    - This starting point is chosen because it allows us to make a decision at each step:\n        - If the current element is greater than the target, move left (decrease the column index).\n        - If the current element is less than the target, move down (increase the row index).\n\n2. Iterate through the Matrix:\n\n    - While `rowindex` is within the bounds of the number of rows and `colindex` is within the bounds of the number of columns:\n        - Check the current element (`matrix[rowindex][colindex]`).\n        - If the current element equals the target, return `true` (found the target).\n        - If the current element is less than the target, increment `rowindex` to move to the next row.\n        - If the current element is greater than the target, decrement `colindex` to move to the previous column.\n\n3. Termination:\n\n    - If we exhaust the while loop without finding the target, return `false` (target not found).\n\n\n# Complexity\n- Time complexity: O(m+n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n`Upvote! It only takes 1 click\uD83D\uDE09`\n# Code\n```\nclass Solution {\npublic:\n    bool searchMatrix(vector<vector<int>>& matrix, int target) {\n        int row = matrix.size();\n        int col = matrix[0].size();\n        int rowindex = 0;\n        int colindex = col-1;\n        while(rowindex<row && colindex>=0){\n            int element = matrix[rowindex][colindex];\n            if (element == target){\n                return 1; \n            }\n            if (element < target){\n                rowindex++;\n            }\n            else{\n                colindex--;\n            }\n        }\n        return 0;\n    }\n};\n```\n\n![upvote.jpeg](https://assets.leetcode.com/users/images/48b755ae-c41b-474d-966e-aa5674e41ff4_1720943081.9620614.jpeg)
['Array', 'C++']