▁the ▁way ▁that ▁yields ▁the ▁lowest ▁total ▁cost , ▁and ▁do ▁the ▁same ▁for ▁each ▁factor . ▁ ▁However , ▁if ▁we ▁implement ▁this ▁algorithm ▁we ▁discover ▁that ▁it ▁is ▁just ▁as ▁slow ▁as ▁the ▁na ive ▁way ▁of ▁trying ▁all ▁permut ations ! ▁What ▁went ▁wrong ? ▁The ▁answer ▁is ▁that ▁we ' re ▁doing ▁a ▁lot ▁of ▁redundant ▁work . ▁For ▁example , ▁above ▁we ▁made ▁a ▁recursive ▁call ▁to ▁find ▁the ▁best ▁cost ▁for ▁computing ▁both ▁ABC ▁and ▁AB . ▁But ▁finding ▁the ▁best ▁cost ▁for ▁computing ▁ABC ▁also ▁requires ▁finding ▁the ▁best ▁cost ▁for ▁AB . ▁As ▁the ▁recursion ▁grows ▁deeper , ▁more ▁and ▁more ▁of ▁this ▁type ▁of ▁unnecessary ▁repet ition ▁occurs . ▁ ▁One ▁simple ▁solution ▁is ▁called ▁mem o ization : ▁each ▁time ▁we ▁compute ▁the ▁minimum ▁cost ▁needed ▁to ▁multiply ▁out ▁a ▁specific ▁sub sequence , ▁we ▁save ▁it . ▁If ▁we ▁are ▁ever ▁asked ▁to ▁compute ▁it ▁again , ▁we ▁simply ▁give ▁the ▁saved ▁answer , ▁and ▁do ▁not ▁re compute ▁it . ▁Since ▁there ▁are ▁about ▁n 2 / 2 ▁different ▁subsequ ences , ▁where ▁n ▁is ▁the ▁number ▁of ▁matrices , ▁the ▁space ▁required ▁to ▁do ▁this ▁is ▁reasonable . ▁It ▁can ▁be ▁shown ▁that ▁this ▁simple ▁trick ▁brings ▁the ▁runtime ▁down ▁to ▁O ( n 3 ) ▁from ▁O ( 2 n ), ▁which ▁is ▁more ▁than ▁efficient ▁enough ▁for ▁real ▁applications . ▁This ▁is ▁top - down ▁dynamic ▁programming . ▁ ▁The ▁following ▁bottom - up ▁approach ▁ ▁comput es