CC-Q3.2

Stack Min:

How would you design a stack which, in addition to push and pop, has a function min which returns the minimum element? Push, pop and min should all operate in 0(1) time.

If we kept track of the minimum at each state, we would be able to easily know the minimum. We can do this by having each node record what the minimum beneath itself is. Then, to find the min, you just look at what the top element thinks is the min.
When you push an element onto the stack, the element is given the current minimum. It sets its "local min"to be the min.

public class StackWithMin extends Stack<NodeWithMin> {
  public void push(int value) {
    int newMin = Math.min(value, min());
    super.push(new NodeWithMin(value, newMin) );
  }
  public int min() {
    if (this . isEmpty()) {
      return Integer.MAX_VALUE // Error value
    } else {
      return peek().min;
    }
  }
}
class NodeWithMin {
  public int value;
  public int min;
  public NodeWithMin(int v, int min) {
    value = v;
    this.min = min;
  }  
}

There's just one issue with this: if we have a Iarge stack, we waste a lot of space by keeping track of the min for every single element. Can we do better?
We can (maybe) do a bit better than this by using an additional stack which keeps track of the mins.

public class StackWithMin2 extends Stack<Integer> {
  Stack<Integer> s2;
  Stack<Integer> s2(){
    s2 = new Stack<Integer>();
  }
  public void push(int value){
    if (value <= min) {
      s2.push(value);
    }
    super.push(value);
  }
  public Integer pop() {
    int value = super.pop();
    if (value == min) {
      s2.pop();
    }  
    return value;
  }
  public int min() {
   if(s2.isEmpty()) {
      return Integer.MAX_VALUE;
    } else {
      return s2.peek();
    }
   }
}

Why might this be more space efficient? Suppose we had a very large stack and the first element inserted happened to be the minimum. In the first solution, we would be keeping n integers, where n is the size of the stack. In the second solution though, we store just a few pieces of data: a second stack with one element and the members within this stack.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末沛鸵,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子缆八,更是在濱河造成了極大的恐慌曲掰,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件奈辰,死亡現(xiàn)場離奇詭異栏妖,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)奖恰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進(jìn)店門吊趾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人瑟啃,你說我怎么就攤上這事论泛。” “怎么了蛹屿?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵屁奏,是天一觀的道長。 經(jīng)常有香客問我错负,道長了袁,這世上最難降的妖魔是什么朗恳? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮载绿,結(jié)果婚禮上粥诫,老公的妹妹穿的比我還像新娘。我一直安慰自己崭庸,他們只是感情好怀浆,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著怕享,像睡著了一般执赡。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上函筋,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天沙合,我揣著相機(jī)與錄音,去河邊找鬼跌帐。 笑死首懈,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的谨敛。 我是一名探鬼主播究履,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼脸狸!你這毒婦竟也來了最仑?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤炊甲,失蹤者是張志新(化名)和其女友劉穎泥彤,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體卿啡,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡全景,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了牵囤。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片爸黄。...
    茶點(diǎn)故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖揭鳞,靈堂內(nèi)的尸體忽然破棺而出炕贵,到底是詐尸還是另有隱情,我是刑警寧澤野崇,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布称开,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏鳖轰。R本人自食惡果不足惜清酥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蕴侣。 院中可真熱鬧,春花似錦昆雀、人聲如沸辱志。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽揩懒。三九已至,卻和暖如春挽封,著一層夾襖步出監(jiān)牢的瞬間已球,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工辅愿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留智亮,地道東北人。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓渠缕,卻偏偏與公主長得像,于是被迫代替她去往敵國和親褒繁。 傳聞我的和親對象是個殘疾皇子亦鳞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評論 2 359

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