why i am getting time limit exceeded
Tilling problem 2
Please try with memoization in your code. It means you can store the ans of a subproblem the first time you solve it and then use the same ans when you encounter that subproblem instead of solving it again.
This will optimize time-complexity of your code.