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