LeetCode-Solutions icon indicating copy to clipboard operation
LeetCode-Solutions copied to clipboard

search-a-2d-matrix-ii

Open shashantsah opened this issue 1 year ago • 1 comments

Description

We need to efficiently search for a target value in a matrix where rows and columns are sorted in ascending order. The matrix is structured such that lower values are towards the top-left corner and higher values are towards the bottom-right corner.

Approach

The provided solution utilizes a two-pointer approach starting from the bottom-left corner of the matrix:

Begin from the bottom-left corner (matrix[n-1][0]).

  • If the current value is equal to the target, return true.

  • If the current value is greater than the target, move upwards to the next row (row--).

  • If the current value is less than the target, move to the right to the next column (col++).

  • This approach efficiently narrows down the search space by leveraging the sorted properties of both rows and columns.

Put check marks:

  • [x] Have you made changes in README file?
  • [x] Added problem & solution under correct topic.
  • [x] Specified Space & Time complexity.
  • [x] Specified difficulty level, tag & Note(if any).

How Has This Been Tested?

  • [x] Implemented test cases for matrix search.
    • Test Case 1:

      • Input 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
      • Expected Output: True
      • Actual Output: True (verified)
    • Test Case 2:

      • Input 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
      • Expected Output: False
      • Actual Output: False (verified)

Make sure all below guidelines are followed else PR will get Reject:

  • [x] My code follows the style guidelines of this project
  • [x] I have performed a self-review of my own code
  • [x] I have commented my code so that it is easy to understand
  • [x] I have made corresponding changes to the documentation
  • [x] My changes generate no new warnings
  • [x] Any dependent changes have been merged and published in downstream modules

shashantsah avatar Jun 03 '24 08:06 shashantsah

I can tell this is your first pull request! Thank you I'm so honored. :tada::tada::tada: I'll take a look at it ASAP!

welcome[bot] avatar Jun 03 '24 08:06 welcome[bot]