Passed all tests, an n^2 solution