Time : O(n^2), Space: O(n^2)