Rewrite solution #1 in a more efficiency way