Say you have an array for which theithelement is the price of a given stock on dayi.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Example 1:Input: [7, 1, 5, 3, 6, 4] Output: 5
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:Input: [7, 6, 4, 3, 1] Output: 0
In this case, no transaction is done, i.e. max profit = 0.
121. Best Time to Buy and Sell Stock
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門蕾羊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來喧笔,“玉大人,你說我怎么就攤上這事龟再∈檎ⅲ” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵利凑,是天一觀的道長浆劲。 經(jīng)常有香客問我嫌术,道長,這世上最難降的妖魔是什么牌借? 我笑而不...
- 正文 為了忘掉前任度气,我火速辦了婚禮,結(jié)果婚禮上膨报,老公的妹妹穿的比我還像新娘磷籍。我一直安慰自己,他們只是感情好丙躏,可當(dāng)我...
- 文/花漫 我一把揭開白布择示。 她就那樣靜靜地躺著,像睡著了一般晒旅。 火紅的嫁衣襯著肌膚如雪栅盲。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼硕淑,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了嘉赎?” 一聲冷哼從身側(cè)響起置媳,我...
- 正文 年R本政府宣布英上,位于F島的核電站炭序,受9級特大地震影響啤覆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜惭聂,卻給世界環(huán)境...
- 文/蒙蒙 一窗声、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧辜纲,春花似錦笨觅、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至扫俺,卻和暖如春苍苞,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背狼纬。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 121. Best Time to Buy and Sell Stock https://leetcode.com...
- Q: Say you have an array for which the ith element is the...
- 問題: Say you have an array for which the ith element is th...
- My Submissions Total Accepted: 152541Total Submissions: 3...