//logic:
        //The matrix is sorted in both rows and column
        //so the first element will be the smallest.
        //build min heap from the first row..
        //heap node should have data, rowno, colno
        //get the min element in heap..
        //get the col no and get the nextrow on the same and update into the heap (we can do insert if we pop on previou step ) but update and heapify is better for this
        //get the next smallest
        //http://www.geeksforgeeks.org/kth-smallest-element-in-a-row-wise-and-column-wise-sorted-2d-array-set-1/

        public int FindthekthSmalleseElementInMatrix(int[,] matrix, int k)
        {
            MinHeap <MatrixElement> myheap = new MinHeap <MatrixElement>(new MinMatrixElementComparer());

            //add first row to minheap//all col in first row
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                myheap.Insert(new MatrixElement(matrix[0, col], 0, col));
            }

            // to find kth smallest..iterate k times
            for (int i = 1; i < k; i++)
            {
                //get the min
                MatrixElement minelement = myheap.Peek();
                //Find the next element in the next row of the same colmumn
                //check the next row is not ourside bounds
                if (minelement.Row + 1 <= matrix.GetLength(1) - 1)
                {
                    MatrixElement nextelement = new MatrixElement(matrix[minelement.Row + 1, minelement.Col], minelement.Row + 1, minelement.Col);

                    //we can update the heap root element and then call minheapify of the root
                    //my heap fon't prov that so
                    myheap.PopRoot();
                    myheap.Insert(nextelement); //this will make sure to have the smallest
                }
                else
                {
                    myheap.PopRoot();
                }
            }

            return(myheap.Peek().Data);
        }
        private Range FindShortestRange(List <Queue <int> > queuelist)
        {
            MinHeap <HeapNodeCustom> heap = new MinHeap <HeapNodeCustom>(new KarthicMinHeapComparer4());

            int bestRangemin = Int32.MaxValue;
            int bestRangemax = Int32.MinValue;

            //insert head of  list into heap
            for (int i = 0; i < queuelist.Count; i++)
            {
                Queue <int> queue = queuelist[i];
                heap.Insert(new HeapNodeCustom(queue.Peek(), i));
                if (queue.Peek() > bestRangemax)
                {
                    bestRangemax = queue.Peek();
                }
            }

            bestRangemin = heap.Peek().Value;
            Range shortestrange = new Range(bestRangemin, bestRangemax);  //initial best to first



            //we have max value...get the min value from the heap
            while (true)
            {
                HeapNodeCustom minrecord = heap.PopRoot(); //pop the min value
                //get the list of poped element
                Queue <int> queue = queuelist[minrecord.IndexofList];
                //remove the first element
                queue.Dequeue();

                if (queue.Count == 0)
                {
                    break;
                }

                //insert next element in this queue to the heap
                heap.Insert(new HeapNodeCustom(queue.Peek(), minrecord.IndexofList));
                //we have running value of max
                if (queue.Peek() > bestRangemax)
                {
                    bestRangemax = queue.Peek();
                }
                bestRangemin = heap.Peek().Value;
                Range range = new Range(bestRangemin, bestRangemax);
                if (range.GetLength() < shortestrange.GetLength())
                {
                    shortestrange = range;
                }
            }

            return(shortestrange);
        }
예제 #3
0
 public void InsertRandomElement(int randomvalue, MaxHeap <int> maxheap, MinHeap <int> minheap)
 {
     if (maxheap.Size() == minheap.Size())
     {
         if (minheap.Size() > 0 && randomvalue > minheap.Peek())
         {
             //if the coming random value is greater than the minheap peek
             maxheap.Insert(minheap.PopRoot());
             minheap.Insert(randomvalue);
         }
         else
         {
             maxheap.Insert(randomvalue);
         }
     }
     else
     {
         //if the Size() is not equal means the max heap will be bigger
         if (randomvalue < maxheap.Peek())
         {
             minheap.Insert(maxheap.PopRoot());
             maxheap.Insert(randomvalue);
         }
         else
         {
             minheap.Insert(randomvalue);
         }
     }
 }
예제 #4
0
 public double  CalculateMedian(MaxHeap <int> maxheap, MinHeap <int> minheap)
 {
     //check for empty..if maxheap is empty then minheap will also be empty
     if (maxheap.Size() == 0)
     {
         return(0);
     }
     if (maxheap.Size() == minheap.Size())
     {
         //mean there are even number of item..median = (sum of middle two items)/2
         return(Convert.ToDouble((maxheap.Peek() + minheap.Peek()) / 2));
     }
     else
     {
         return(maxheap.Peek());
     }
 }