We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

generally, in dp solution..we start with 1 and build up our solution till n..but i think here (min,min-1,min-2) will do. how did you conclude that we dont require min-3,min-4. Can you elaborate

6 9 9 => reduce 9 to 6 in Two Steps + reduce next 9 to 6 in Two Steps => Total 4. OR 6-2=9-5=9-5 => Total 3 Steps.

6 8 8 => 6-2=8-4=8-4 => Total 3 Steps. OR 6=8-2=8-2 Total 2 Steps. i.e. when the difference is '3' or '4' then we reduce them by '5' which is One Step (better than '2'+'1' OR '2'+'2') .

There is no need to go till min-3 or min-4. Solution passes all test case with just min, min-1, min-2. Makes sense too, logically. Taking min-1 as baseline is helpful for numbers that are min+4 as they can reach min-1 in 1 step (-5). They couldn't reach min or min-2 in 1 one step.

Similarily, Taking min-2 as baseline is helpful for numbers that are min+3 as they can reach min-2 in 1 step (-5). They would have to take 2 steps in reaching min or min-1.

Taking min-3 as baseline is not helpful for any numbers. min+1 number take 2 steps in reaching there. min+2 number takes one step in reaching (-5) but they could have easily reached min in one step too (-2) . min+3 number take 2 steps in reaching min-3 and 2 steps in reaching min again so we don't really need min-3.

## Equal

You are viewing a single comment's thread. Return to all comments →

generally, in dp solution..we start with 1 and build up our solution till n..but i think here (min,min-1,min-2) will do. how did you conclude that we dont require min-3,min-4. Can you elaborate

There is no need to go till min-3 or min-4. Solution passes all test case with just min, min-1, min-2. Makes sense too, logically. Taking min-1 as baseline is helpful for numbers that are min+4 as they can reach min-1 in 1 step (-5). They couldn't reach min or min-2 in 1 one step.

Similarily, Taking min-2 as baseline is helpful for numbers that are min+3 as they can reach min-2 in 1 step (-5). They would have to take 2 steps in reaching min or min-1.

Taking min-3 as baseline is not helpful for any numbers. min+1 number take 2 steps in reaching there. min+2 number takes one step in reaching (-5) but they could have easily reached min in one step too (-2) . min+3 number take 2 steps in reaching min-3 and 2 steps in reaching min again so we don't really need min-3.

Same logic can be applied for min-4 baseline too.