Day24-BackTracking03
93. Restore IP Addresses
Node:
Time Complexity && Space Complexity
- Time Complexity:
O(3^n)
- In every digital, there are 3 options, str.slice(index, index+1), str.slice(index, index+2), str.slice(index, index+3)
- There are 12 digital at most
- Space Complexity:
O(m)
, where m is the number of valid IP addresses stored in the result array.- Recursion Depth: 4
- Result Storage:O(m), where m is the number of valid IP addresses.
- Auxiliary Space: Each recursive call stores a copy of the current path, which can have at most 4 parts (since an IP address has 4 segments)
1 | var restoreIpAddresses = function (s) { |
78. Subsets
Node:
Time Complexity && Space Complexity
- Time Complexity:
O(n * 2^n)
- There are
2^n
recusive- There are two choices (include or exclude) for each of the
n
elements, the total number of different combinations (subsets) we can create is2^n
- There are two choices (include or exclude) for each of the
- Copy one array in every recusive
- Compare to backtracking question: generating all k-item subsets (combinations of k elements) from an array nums.length=n
O( C(n,k) * k)
- combinations problem, and the number of ways to choose k elements from an array of n elements
- copy a array is
O(k)
- There are
- Space Complexity:
O( n * 2^n)
- Recursion Stack: depends on the recursion depth which is at most
n
: O(n) - Space for Storing Results: (usually don’t count the result space) O(n * 2^n)
- store 2^n subset, every subset length is n
- Auxiliary Space:
- a new array is created in every recursive call
- Recursion Stack: depends on the recursion depth which is at most
1 | var subsets = function (nums) { |
90. Subsets II
Node:
Time Complexity && Space Complexity
- Time Complexity:
O(n*logn)+O(n*2^n) = O(n*2^n)
- Space Complexity:
O(n*2^n)
1 | var subsetsWithDup = function (nums) { |
- Title: Day24-BackTracking03
- Author: Guoyi
- Created at : 2024-10-15 22:03:18
- Updated at : 2024-12-07 03:58:41
- Link: https://guoyiwang.github.io/Algorithm/Day24-BackTracking03/
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments