2019-05-08 Final Review

Problem 1

Input

Binary tree T

Out

True if the tree is BST, False otherwise

Incorrect Solution

function Check(T):
    if T = null:
        return True
    return Check(T.left)
        and Check(T.right)
        and (T.key < T.right.key)
        and (T.key > T.left.key)

This is wrong because it only compares node with its direct children, but it doesn't check the grandchildren.

Idea

Check T.key > max(T.left) and T.key < min(T.right)

Solution

function Check2(T):
    if T = null:
        # return (True, -inf, inf)  # WRONG!!
        return (True, inf, -inf)
    res1, minl, maxl = Check2(T.left)
    res2, minr, maxr = Check2(T.right)
    res = resl and res r
        and T.key > maxl and T.key < minr
    _min = min(minl, minr, T.key)
    _max = max(maxl, maxr, T.key)
    return res, _min, _max

Complexity

Single Call: O(1)

number of calls = n + #Nulls \leq 3n

Total: 3nO(1)=O(n)

Exercise

CHECK IF RB-TREE

Stock Price

Input

Given stock prices P[1..n]

Output

Days to buy and sell to maximize profit given $1 fee for each day of owning the seock. Complexity O(n\log n)

Example

P = [1,3,3,4]

buy = 1
sell = 1
profit = 3 - 1 = 2

Divide & Conquer

From recursive call

  • buy&sell in 1st half
  • buy&sell in 2nd half

To consider

  • buy in 1st and sell in 2nd

if there is not $1 fee, the profit is max(P[n/2:]) - min(P[n/2])

max(P[k]-(k-n/2) | k from n/2+1 to n)
                +
max(-P[l]-(n/2-l) | l from 1 to n/2)

Solution

function SellBuy(P):
    if n = 1:
        return 0
    m = int(n/2)
    left = SellBuy(P[1..m])
    right = SellBut(P[m+1..n])
    middle = ...
    return max(left, right, middle)x
微信圖片_20190508115656.jpg

Problem 3

Input

Array A[1..n] of numers and number C

Output

  • True if there are i and j such that A[i] + A[j] = C
  • False otherwise

Complexity: O(n\log n)

Steps

  1. Sort A
  2. For every i, search for (C-A[i])/2. If found return True
  3. return False

?? :triangular_flag_on_post:: do 2 in O(n)

Dream

For every i, search for (C-A[i])/2 in A[i+1..n]

  1. RB-tree T empty
  2. For every i from n to 1
    • search (C-A[i])/2 in T, return True if found
    • insert A[i] ino T
  3. return False

If we have no duplicates,
sort array of paris (A[i],i) by i

?? :triangular_flag_on_post:: What if we want to count # of such (i,j)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市静汤,隨后出現(xiàn)的幾起案子裁僧,更是在濱河造成了極大的恐慌廉嚼,老刑警劉巖撮胧,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)待逞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)腰埂,“玉大人飒焦,你說(shuō)我怎么就攤上這事蜈膨∮炝” “怎么了?”我有些...
    開封第一講書人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵翁巍,是天一觀的道長(zhǎng)驴一。 經(jīng)常有香客問我,道長(zhǎng)灶壶,這世上最難降的妖魔是什么肝断? 我笑而不...
    開封第一講書人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮驰凛,結(jié)果婚禮上胸懈,老公的妹妹穿的比我還像新娘。我一直安慰自己恰响,他們只是感情好趣钱,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著胚宦,像睡著了一般首有。 火紅的嫁衣襯著肌膚如雪燕垃。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,190評(píng)論 1 299
  • 那天井联,我揣著相機(jī)與錄音卜壕,去河邊找鬼。 笑死烙常,一個(gè)胖子當(dāng)著我的面吹牛轴捎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播军掂,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼轮蜕,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了蝗锥?” 一聲冷哼從身側(cè)響起跃洛,我...
    開封第一講書人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎终议,沒想到半個(gè)月后汇竭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡穴张,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年细燎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片皂甘。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡玻驻,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出偿枕,到底是詐尸還是另有隱情璧瞬,我是刑警寧澤,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布渐夸,位于F島的核電站嗤锉,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏墓塌。R本人自食惡果不足惜瘟忱,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望苫幢。 院中可真熱鬧访诱,春花似錦、人聲如沸韩肝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)伞梯。三九已至玫氢,卻和暖如春帚屉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背漾峡。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工攻旦, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人生逸。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓牢屋,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親槽袄。 傳聞我的和親對(duì)象是個(gè)殘疾皇子烙无,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

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