Esempio n. 1
0
        static int TwoStacks(int x, int[] a, int[] b)
        {
            var obj1     = new StackUsingLL <int>();
            var sumStack = new StackUsingLL <int>();

            for (int i = a.Length - 1; i >= 0; i--)
            {
                obj1.Push(a[i]);
            }

            var obj2 = new StackUsingLL <int>();

            for (int i = b.Length - 1; i >= 0; i--)
            {
                obj2.Push(b[i]);
            }

            while (sumStack.SumStack() + obj1.PeekTop() <= x)
            {
                sumStack.Push(obj1.Pop());
            }


            int countStack1 = sumStack.StackLength();
            int sum         = sumStack.SumStack();
            int max         = countStack1;
            int countStack2 = 0;

            while (obj2.StackLength() != 0 && sumStack.StackLength() > 0)
            {
                sum = sum + obj2.Pop();
                countStack2++;
                if (sum > x)
                {
                    sum = sum - sumStack.Pop();
                    countStack1--;
                }
                if (sum <= x && countStack1 + countStack2 > max)
                {
                    max = countStack1 + countStack2;
                }
            }
            return(max);
        }
Esempio n. 2
0
        public void StackLL_Test()
        {
            var obj = new StackUsingLL <int>();

            obj.Push(1);
            obj.Push(1);
            obj.Push(1);
            obj.Push(1);
            obj.Push(1);
            obj.Push(1);
            obj.Push(5);
            obj.Push(4);
            obj.Push(3);
            obj.Push(1);
            obj.EmptyStack();
            Assert.Equal(0, obj.StackLength());
            obj.Push(2);
            Assert.Equal(1, obj.StackLength());
        }
Esempio n. 3
0
        public string IsBalanced(string s)
        {
            StackUsingLL <int> obj = new StackUsingLL <int>();

            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '{' || s[i] == '[' || s[i] == '(')
                {
                    obj.Push(s[i]);
                }
                else
                {
                    if (obj.StackLength() > 0)
                    {
                        switch (s[i])
                        {
                        case '}': if (obj.PeekTop() == '{')
                            {
                                obj.Pop();
                            }
                            else
                            {
                                return("NO");
                            }
                            break;


                        case ']':  if (obj.PeekTop() == '[')
                            {
                                obj.Pop();
                            }
                            else
                            {
                                return("NO");
                            }
                            break;

                        case ')':   if (obj.PeekTop() == '(')
                            {
                                obj.Pop();
                            }
                            else
                            {
                                return("NO");
                            }
                            break;
                        }
                    }
                    else
                    {
                        return("NO");
                    }
                }
            }

            if (obj.StackLength() == 0)
            {
                return("YES");
            }
            else
            {
                return("NO");
            }
        }