如何计算此解决方案的时间和 space 复杂度?
How to compute time and space complexity of this solution?
我正在解决关于 leetcode 的 this 问题。无法计算出我的解决方案的时间和 space 复杂性。
特别是,我不明白如何在此处应用 Master Theorem 以防我们有 FOR 循环。这里的 a 和 b 是什么?由于输入分为多次并且针对不同大小的子问题。另一个复杂问题是记忆。
class Solution {
private Map<String, List<Integer>> cache = new HashMap<>();
public List<Integer> diffWaysToCompute(String equation) {
if (cache.containsKey(equation)) return cache.get(equation);
if (!(equation.contains("+") || equation.contains("-") || equation.contains("*"))) return Collections.singletonList(Integer.valueOf(equation));
List<Integer> result = new ArrayList<>();
for (int i = 0; i < equation.length();i++) {
char ch = equation.charAt(i);
if (ch == '+' || ch == '-' || ch == '*') {
List<Integer> left = diffWaysToCompute(equation.substring(0, i));
List<Integer> right = diffWaysToCompute(equation.substring(i+1, equation.length()));
result.addAll(crossCalc(left, right, ch));
}
}
cache.put(equation, result);
return result;
}
private List<Integer> crossCalc(List<Integer> left, List<Integer> rigth, char sign) {
List<Integer> result = new ArrayList<>();
for (Integer l : left) {
for (Integer r : rigth) {
if (sign == '-') {
result.add(l - r);
} else if (sign == '+') {
result.add(l + r);
} else {
result.add(l*r);
}
}
}
return result;
}
}
我正在寻找如何计算时间复杂度的解释,而不仅仅是答案。如果你能解释有和没有记忆的复杂性,最好。谢谢!
T(n) = Sum{T(i) + T(N-i)} for some index i <= 2(T(1) + T(2) + ... + T(n - 1))
=> T(n + 1) - T(n) = 2T(n) => T(n) <= O(3^n) worst case
其中 n 是拆分数的计数
你的算法的时间复杂度等于正确匹配的包含 n 对括号的表达式的数量。
它被称为加泰罗尼亚数字,它等于 C(2 * n, n) / (n + 1) = (2 * n)! / ((n + 1)! * n!).
此外,还有一个计算加泰罗尼亚数的递归公式:
f(n+1) = f(0)f(n) + f(1)f(n-1) + f(2)f(n-2) + ... + f(n-2)f(2) + f(n-1)f(1) + f(n)f(0)
你知道的,这和你的算法时间复杂度方程是一样的!
T(n+1) = T(0)T(n) + T(1)T(n-1) + T(2)T(n-2) + ... + T(n-2)T(2) + T(n-1)T(1) + T(n)T(0)
由于result
ArrayList 的元素数量可能很大,因此该算法的内存复杂度可能与其时间复杂度一样大。因此,在最坏情况下,内存和时间复杂度将是第 n 个加泰罗尼亚数字。
我正在解决关于 leetcode 的 this 问题。无法计算出我的解决方案的时间和 space 复杂性。
特别是,我不明白如何在此处应用 Master Theorem 以防我们有 FOR 循环。这里的 a 和 b 是什么?由于输入分为多次并且针对不同大小的子问题。另一个复杂问题是记忆。
class Solution {
private Map<String, List<Integer>> cache = new HashMap<>();
public List<Integer> diffWaysToCompute(String equation) {
if (cache.containsKey(equation)) return cache.get(equation);
if (!(equation.contains("+") || equation.contains("-") || equation.contains("*"))) return Collections.singletonList(Integer.valueOf(equation));
List<Integer> result = new ArrayList<>();
for (int i = 0; i < equation.length();i++) {
char ch = equation.charAt(i);
if (ch == '+' || ch == '-' || ch == '*') {
List<Integer> left = diffWaysToCompute(equation.substring(0, i));
List<Integer> right = diffWaysToCompute(equation.substring(i+1, equation.length()));
result.addAll(crossCalc(left, right, ch));
}
}
cache.put(equation, result);
return result;
}
private List<Integer> crossCalc(List<Integer> left, List<Integer> rigth, char sign) {
List<Integer> result = new ArrayList<>();
for (Integer l : left) {
for (Integer r : rigth) {
if (sign == '-') {
result.add(l - r);
} else if (sign == '+') {
result.add(l + r);
} else {
result.add(l*r);
}
}
}
return result;
}
}
我正在寻找如何计算时间复杂度的解释,而不仅仅是答案。如果你能解释有和没有记忆的复杂性,最好。谢谢!
T(n) = Sum{T(i) + T(N-i)} for some index i <= 2(T(1) + T(2) + ... + T(n - 1))
=> T(n + 1) - T(n) = 2T(n) => T(n) <= O(3^n) worst case
其中 n 是拆分数的计数
你的算法的时间复杂度等于正确匹配的包含 n 对括号的表达式的数量。
它被称为加泰罗尼亚数字,它等于 C(2 * n, n) / (n + 1) = (2 * n)! / ((n + 1)! * n!).
此外,还有一个计算加泰罗尼亚数的递归公式:
f(n+1) = f(0)f(n) + f(1)f(n-1) + f(2)f(n-2) + ... + f(n-2)f(2) + f(n-1)f(1) + f(n)f(0)
你知道的,这和你的算法时间复杂度方程是一样的!
T(n+1) = T(0)T(n) + T(1)T(n-1) + T(2)T(n-2) + ... + T(n-2)T(2) + T(n-1)T(1) + T(n)T(0)
由于result
ArrayList 的元素数量可能很大,因此该算法的内存复杂度可能与其时间复杂度一样大。因此,在最坏情况下,内存和时间复杂度将是第 n 个加泰罗尼亚数字。