Update solution.py

Fixed the bugs and updated recursive algorithm.