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

g0201_0300.s0240_search_a_2d_matrix_ii.complexity.md Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
**Time Complexity (Big O Time):**

The time complexity of this program is primarily determined by the `while` loop. In the worst case, the `while` loop iterates until either `r` exceeds the number of rows (`matrix.length`) or `c` becomes negative. The loop condition ensures that we move either left (decreasing `c`) or down (increasing `r`) in each iteration. Since each iteration reduces the search space by one row or one column, the number of iterations in the worst case will be at most `n + m`, where `n` is the number of rows and `m` is the number of columns in the matrix.

Therefore, the time complexity of the program is O(n + m), where `n` is the number of rows and `m` is the number of columns in the matrix.

**Space Complexity (Big O Space):**

The program uses a few integer variables (`r`, `c`, `target`) that consume a constant amount of space regardless of the input size. These variables do not depend on the dimensions of the matrix.

Therefore, the space complexity of the program is O(1), which indicates constant space usage.

In summary, the provided program has a time complexity of O(n + m), where `n` is the number of rows and `m` is the number of columns in the matrix, and it has a space complexity of O(1), indicating constant space usage.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy