works fine but exceed time limit
sol of q015 (meet performance criteria