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

74-Search-A-2D-Matrix.java is not a Binary Search #442

Closed
AmonKip opened this issue Jul 10, 2022 · 2 comments · Fixed by #448
Closed

74-Search-A-2D-Matrix.java is not a Binary Search #442

AmonKip opened this issue Jul 10, 2022 · 2 comments · Fixed by #448
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@AmonKip
Copy link

AmonKip commented Jul 10, 2022

The Java solution is a linear search and NOT a binary search. Worst case runtime for the code is O(m+n) which occurs if the target value is at the bottom left corner of the matrix. Binary search solution should be O(logm) + O(logn) as described in the video solution.

@Ahmad-A0
Copy link
Collaborator

Ahmad-A0 commented Jul 10, 2022

I think 74-Search-A-2D-Matrix.java is the solution in question

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int i = 0;
        int j = matrix[0].length - 1;
        while(i >= 0 && i <= matrix.length - 1 && j >= 0 && j <= matrix[0].length - 1){
            if(matrix[i][j] == target){
                return true;
            } else if (matrix[i][j] > target){
                j--;
            } else {
                i++;
            }
        }
        return false;
    }
}

If anyone knows java and would like to submit a pull request to fix this please go ahead. I think this is also #424

@Ahmad-A0 Ahmad-A0 added bug Something isn't working good first issue Good for newcomers labels Jul 10, 2022
@Ahmad-A0 Ahmad-A0 changed the title Java solution is linear search NOT binary search. 74-Search-A-2D-Matrix.java is not a Binary Search Jul 10, 2022
@AmonKip
Copy link
Author

AmonKip commented Jul 10, 2022

Yes it's leetcode problem #74. Sorry, I was specific enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants