How to implement three stacks using a single array

后端 未结 17 775
情书的邮戳
情书的邮戳 2020-12-23 15:10

I came across this problem in an interview website. The problem asks for efficiently implement three stacks in a single array, such that no stack overflows until there is no

17条回答
  •  死守一世寂寞
    2020-12-23 15:50

    1. Make a HashMap with keys to the begin and end positions e.g. < "B1" , 0 >, <"E1" , n/3 >

    2. for each Push(value) add a condition to check if position of Bx is previous to Ex or there is some other "By" in between. -- lets call it condition (2)

    3. with above condition in mind, if above (2) is true // if B1 and E1 are in order { if ( S1.Push()), then E1 ++ ; else // condition of overflow , { start pushing at end of E2 or E3 (whichever has a space) and update E1 to be E2-- or E3-- ; } }

      if above (2) is false { if ( S1.Push()), then E1 -- ; else // condition of overflow , { start pushing at end of E2 or E3 (whichever has a space) and update E1 to be E2-- or E3-- ; } }

提交回复
热议问题