Leetcode solution written in python