All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0201_0300.s0240_search_a_2d_matrix_ii.Solution.c Maven / Gradle / Ivy

The newest version!
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Divide_and_Conquer
// #Data_Structure_II_Day_4_Array #Binary_Search_II_Day_8 #Big_O_Time_O(n+m)_Space_O(1)
// #2024_11_07_Time_42_ms_(96.34%)_Space_11.9_MB_(56.70%)

#include 
#include 

bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target) {
    int r = 0;
    int c = matrixColSize[0] - 1; // Start from the top-right corner

    while (r < matrixSize && c >= 0) {
        if (matrix[r][c] == target) {
            return true;
        } else if (matrix[r][c] > target) {
            c--; // Move left
        } else {
            r++; // Move down
        }
    }
    return false;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy