Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search-a-2d-matrix-ii #485

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

shashantsah
Copy link

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:

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

How Has This Been Tested?

  • 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:

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

Copy link

welcome bot commented Jun 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant