time O(n^2) space O(2^n)