public int GetMaxWaterTrapped()
        {
            int i     = 0;
            int total = 0;

            while (i < items.Length)
            {
                if (st.IsEmpty() || items[i] < items[st.Peek()])
                {
                    st.Push(i);
                }
                else
                {
                    while (!st.IsEmpty() && items[i] >= items[st.Peek()])
                    {
                        int top    = st.Pop();
                        int height = Math.Min(items[i], items[st.Peek()]) - items[top];
                        int width  = st.IsEmpty() ? 0 : i - 1 - st.Peek();
                        total = total + (height * width);
                    }
                }
                i++;
            }
            return(total);
        }
        public int GetMaxWithStack()
        {
            int           i  = 0;
            LBStack <int> st = new LBStack <int>();

            while (i < items.Length)
            {
                if (items[i] > items[st.Peek()] || st.IsEmpty())
                {
                    st.Push(i);
                }
                else
                {
                    while (!st.IsEmpty() && items[i] <= items[st.Peek()])
                    {
                        calculateArea(items, st, i);
                    }
                    st.Push(i);
                }
                i++;
            }

            while (!st.IsEmpty())
            {
                calculateArea(items, st, i);
            }

            return(max);
        }
Exemple #3
0
 public void Enqueue(int ele)
 {
     while (!st1.IsEmpty())
     {
         st2.Push(st1.Pop());
     }
     st1.Push(ele);
     while (!st2.IsEmpty())
     {
         st1.Push(st2.Pop());
     }
 }
        public IDictionary <int, int> GetNextGreaterElement()
        {
            LBStack <int> st = new LBStack <int>();

            foreach (int i in items)
            {
                st.Push(i);
            }
            while (!st.IsEmpty())
            {
                int item = st.Peek();
                if (st.top == 0 || st.top == items.Length - 1)
                {
                    st.Pop();
                    data.Add(item, -1);
                }
                else
                {
                    st.Pop();
                    data.Add(item, max);
                }
                max = item < max ? max : item;
            }

            return(data);
        }
Exemple #5
0
        public static bool Validate(string str)
        {
            LBStack <char> st = new LBStack <char>();

            map.Add('}', '{');
            map.Add(')', '(');
            map.Add(']', '[');

            char[] ch = str.ToCharArray();
            foreach (char c in ch)
            {
                string s = c.ToString();
                switch (s)
                {
                case "{":
                case "(":
                case "[": st.Push(c); break;

                case "}":
                case ")":
                case "]":
                    string popped2 = st.Pop().ToString();
                    if (map[c].ToString() != popped2)
                    {
                        return(false);
                    }
                    break;

                default: break;
                }
            }
            return(st.IsEmpty());
        }
        private void calculateArea(int[] items, LBStack <int> st, int index)
        {
            int pop_index   = st.Pop();
            int lower_bound = st.IsEmpty() ? -1 : st.Peek();
            int area        = items[pop_index] * ((index - 1) - lower_bound);

            UpdateArea(area);
        }