Good solution from leetcode