Leetcode solution in Python