v2 time O(r*n) space O(n) Accepted