Dynamic programming

Today, I'm going to talk something detailed about dynamic programming. Dynamic programming is a kind of complicated algorithm which involves a lot of algorithm-design thoughts姐呐,such as optimal sub-result, recursive, iterative, divide-conquer and so on. ?I will analyse my own thought by tearing apart the whole packed enigma. ?

First, the aim of using dynamic programming is reducing the time complexity and sometimes avoiding the unnecessary space-consuming. To solve a problem, the first method which is also the simplest comes to mind is brute-force method. However, brute-force always can't meet the online judge standard. Then divide-conquer algorithm is adopted. A big amount of problems can be solved by divide-conquer. We can understand this algorithm just by the literal meaning. We divide the problem into sub-problems which is small enough to solve directly first, then merge these sub-results into the final result.?

Brute-force and divide-conquer are the overview of applied algorithms. For writing code, there are two ways to implement these algorithms, iterative and recursive. The difference between iterative and recursive isn't that clear. In my opinion, iterative way is fast and memory-friendly, but hard to write by code. On the contrary, recursive way is slow and memory-consuming, but easy to implement.?

Dynamic programming is an algorithm which is only for the convenience of computer. It's hard for programmer to write, but efficient for computer to implement. It combines the features of divide-conquer and iterative. However, the concept of dynamic programming isn't just divide-conquer and iterative. It's more advanced and harder to understand. Even you understand it perfectly, without enough experience and patience, it's still hard to adopt it in solving a related problem.?

I'm going to explain my idea of dynamic programming. If it's flawed, even wrong, please point it out. The key point of dynamic programming is to find the same status for all the small-divided problems and switch the status to the final problem. I will coding out a classic dp problem to interpret the finding and switching process.?

Problem: What's the length of the longest incrementing subsequence (not consecutive) in array (1,7,2,3,8,4).

Solution:

int lis(int *arr, int len)

{

? ? ?//find the status for sub-problem

? ? ? int *mLen = (int *)malloc(sizeof(int)*len);

? ? ? mLen[0] = 1;

? ? ? for (int i=0; i<len; i++)

? ? ? {

? ? ? ? ? ? ?for (int j=0; j<i; j++)

? ? ? ? ? ? ?{

? ? ? ? ? ? ? ? ? //switching the status gradually

? ? ? ? ? ? ? ? ? if (mLen[j] < mLen[i])

? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ?mLen[i] = mLen[j] + 1;

? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ?mLen[i] = mLen[j];

? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ?}

? ? ? }

? ? ? return mLen[len-1];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末骏掀,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子笑陈,更是在濱河造成了極大的恐慌葵袭,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,110評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蓬网,死亡現(xiàn)場(chǎng)離奇詭異鹉勒,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)锯厢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門脯倒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人剪撬,你說我怎么就攤上這事郁岩∪焙荩” “怎么了萍摊?”我有些...
    開封第一講書人閱讀 165,474評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵冰木,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我踊沸,道長(zhǎng),這世上最難降的妖魔是什么评凝? 我笑而不...
    開封第一講書人閱讀 58,881評(píng)論 1 295
  • 正文 為了忘掉前任腺律,我火速辦了婚禮匀钧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘之斯。我一直安慰自己,他們只是感情好莉擒,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評(píng)論 6 392
  • 文/花漫 我一把揭開白布项乒。 她就那樣靜靜地躺著梁沧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪频鉴。 梳的紋絲不亂的頭發(fā)上恋拍,一...
    開封第一講書人閱讀 51,698評(píng)論 1 305
  • 那天施敢,我揣著相機(jī)與錄音周荐,去河邊找鬼。 笑死腋妙,一個(gè)胖子當(dāng)著我的面吹牛讯榕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播济竹,決...
    沈念sama閱讀 40,418評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼霎槐,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了罕袋?” 一聲冷哼從身側(cè)響起碍岔,我...
    開封第一講書人閱讀 39,332評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎榆纽,沒想到半個(gè)月后捏肢,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,796評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡衣屏,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評(píng)論 3 337
  • 正文 我和宋清朗相戀三年狼忱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了一睁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,110評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡窘俺,死狀恐怖复凳,靈堂內(nèi)的尸體忽然破棺而出灶泵,到底是詐尸還是另有隱情对途,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評(píng)論 5 346
  • 正文 年R本政府宣布深纲,位于F島的核電站劲妙,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏镣奋。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評(píng)論 3 331
  • 文/蒙蒙 一余赢、第九天 我趴在偏房一處隱蔽的房頂上張望妻柒。 院中可真熱鬧,春花似錦举塔、人聲如沸求泰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽卜朗。三九已至,卻和暖如春聊替,著一層夾襖步出監(jiān)牢的瞬間培廓,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工泣港, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人呛每。 一個(gè)月前我還...
    沈念sama閱讀 48,348評(píng)論 3 373
  • 正文 我出身青樓晨横,卻偏偏與公主長(zhǎng)得像箫柳,于是被迫代替她去往敵國(guó)和親手形。 傳聞我的和親對(duì)象是個(gè)殘疾皇子悯恍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容