g0001_0100.s0074_search_a_2d_matrix.complexity.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
**Time Complexity (Big O Time):**
1. The program first determines the number of rows (`endRow`) and columns (`endCol`) in the matrix, which takes O(1) time.
2. It then iterates through the rows once to find the target row where the last element of each row is greater than or equal to the target. This loop runs in O(endRow) time, where `endRow` is the number of rows in the matrix.
3. Once the target row is identified, the program iterates through the elements in that row to check if the target exists in that row. This loop runs in O(endCol) time, where `endCol` is the number of columns in the matrix.
4. Therefore, the overall time complexity of the program is O(endRow + endCol), where `endRow` is the number of rows and `endCol` is the number of columns in the matrix.
**Space Complexity (Big O Space):**
1. The program uses a few integer variables (such as `endRow`, `endCol`, and `targetRow`) and a boolean variable (`result`) to store intermediate values. These variables consume constant space, which does not depend on the size of the input matrix.
2. The program does not use any additional data structures or arrays that scale with the size of the input matrix.
3. Therefore, the space complexity of the program is O(1) or constant space.
In summary, the time complexity of the provided program is O(endRow + endCol), and the space complexity is O(1). The program efficiently searches for a target element in a 2D matrix, considering the properties of the matrix, without using additional memory for data structures.