g0001_0100.s0062_unique_paths.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 uses a dynamic programming approach to fill in a 2D array `dp` of size `m`x`n` with values.
2. The first two loops (two separate loops, one for setting the values in the first column and one for setting the values in the first row) each run in O(max(m, n)) time because they iterate through the entire first column and first row.
3. The nested loops, which fill in the remaining cells of the `dp` array, run in O(m * n) time because they iterate through all the rows and columns.
4. Therefore, the overall time complexity of the program is O(m * n), where 'm' is the number of rows, and 'n' is the number of columns in the grid.
**Space Complexity (Big O Space):**
1. The program uses a 2D array `dp` of size `m`x`n` to store the intermediate results. Therefore, the space complexity is O(m * n), where 'm' is the number of rows, and 'n' is the number of columns in the grid.
2. The space complexity is dominated by the `dp` array, and it does not depend on the input values of 'm' and 'n' but only on the grid size.
In summary, the time complexity of the provided program is O(m * n), and the space complexity is also O(m * n), where 'm' is the number of rows, and 'n' is the number of columns in the grid. The program efficiently calculates the number of unique paths in the grid using dynamic programming.