Exemple #1
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());
        }
        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);
        }
        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);
        }