AC

time O(n) solution