Exemple #1
0
        public static void Taxicab3(int n)
        {
            // O(n) time and O(n) space
            var pq = new MinPQ <SumOfCubes>();

            for (int i = 1; i <= n; i++)
            {
                pq.Push(new SumOfCubes(i, i));
            }

            // O(n^2*logn) time
            var sentinel = new SumOfCubes(0, 0);

            while (pq.Count > 0)
            {
                var current = pq.Pop();

                if (current.Result == sentinel.Result)
                {
                    Console.WriteLine($"{sentinel.A}^3+{sentinel.B}^3 = {current.A}^3+{current.B}^3 = {current.Result}");
                }

                if (current.B <= n)
                {
                    pq.Push(new SumOfCubes(current.A, current.B + 1));
                }

                sentinel = current;
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            var max = new MaxPQ <int>();
            var min = new MinPQ <int>();
            var mid = new MidPQ <int>();

            for (int i = 0; i < 10; i++)
            {
                max.Push(i);
                min.Push(i);
                mid.Push(i);
            }

            while (max.Count > 0 && min.Count > 0)
            {
                Console.WriteLine($"{max.Pop()} {min.Pop()} {mid.Pop()}");
            }

            Console.ReadKey();
        }
Exemple #3
0
 public MidPQ()
 {
     _min = new MinPQ <T>();
     _max = new MaxPQ <T>();
 }