DSA GRIND #12
DSA GRIND #12
by Ashur Baroutta
This weeks work involved climbing into the world of dynamic programming. Its something sometimes done without realization but for those unfamiliar dynamic programming is an algorithmic method of solving optimization problems by breaking the problem down into simpler subproblems. The idea is if we have the optimal solution to all of the subproblems we will have achieved the optimal solution for the overarching problem.
So the problem is going to be a 1-dimensional Dynamic programming problem. It states "You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top".
I started by initializing 3 variables (a,b,c). I set a,b equal to 1. I then setup a for loop with the exit condition of n-1. With each iteration of our loop, in order to make sure i had the accurate and most optimal answer for each distinct way to get to the top, I set c = a + b, a = b, and b = c. We are then tracking specific step on the stairs and the amount of ways to get to the top.
After working through the problem, I think the biggest takeaway for me is that dynamic programming has a value in breaking problems down into multiple steps that I can apply to future problems. Also and most importantly, this is a skill I haven't consciously practiced too much so if the intro problems are giving me a headache, I definitely need to work at this more. The submission is as follows :
Comments
Post a Comment