public KthLargestNumberInStream(int k, int[] ar) { arr = ar; K = k; pq = new PQ <int>(); for (int i = 0; i < arr.Length; i++) { Add(arr[i]); } }
//smallest or nearest=>Max Heap public static int[][] KClosest(int[][] points, int K) { PQ <Point> pq = new PQ <Point>(false); // PriorityQueue<Tuple<int,int>> pq1 = new PriorityQueue<Tuple<int,int>>(false); int rows = points.Length; for (int i = 0; i < rows; i++) { // distance[i] = (points[i][0] * points[i][0]) + (points[i][1] * points[i][1]); var p = new Point(points[i][0], points[i][1], i); if (pq.Count() < K) { // pq.Enqueue(distance[i], new Tuple<int,int>(distance[i],i)); pq.Enqueue(p); } else { if (p.CompareTo(pq.Peek()) < 0) //if (distance[i] < pq.Peek().Item1) { pq.Dequeue(); pq.Enqueue(p); } } } var result = new int[K][]; int index = 0; for (int j = 0; j < K; j++) { result[index++] = points[pq.Dequeue().index]; } return(result); }
public static void Main(string[] args) { List <int> lst1 = new List <int> { 1, 2, 3, 4 }; List <int> lst2 = new List <int> { 2, 5, 6 }; List <int> lst3 = new List <int> { 2, 5, 7 }; MergeKSortedStreams mg = new MergeKSortedStreams(new List <List <int> > { lst1, lst2, lst3 }); var mergeRes = mg.MergeResult(); CountNumbersSmallerThanSelf.CountSmaller(new int[] { 5, 2, 6, 1 }); var repeatCount = NumberOfOccurancesInSortedArray.GetNumberOfOccurancesInSortedArray(new int[] { 1, 2, 3, 3, 3, 3, 4, 5, 6, 7 }, 3); // BSTCount.PrintBST(4); int[] list1 = new int[2] { 3, 3 }; int[] list2 = new int[2] { 5, -1 }; int[] list3 = new int[2] { -2, 4 }; var closetPoints = KClosestPoints.KClosest(new int[][] { list1, list2, list3 }, 2); //Ans: key1: 3,hello key2:1,zzz key3;1,world string[] output = LexicographicalOrder.Solve(new string[] { "key1 abcd", "key2 zzz", "key1 hello", "key3 world", "key1 hello" }); int[] res = KLargestElementsInStream.TopK(new int[] { 4, 8, 9, 6, 6, 2, 10, 2, 8, 1, 2 }, 9); MergeKSortedArrays mergeArrays = new MergeKSortedArrays(); var mergeResult = mergeArrays.MergeArrays(new int[, ] { { 1, 3, 5, 7 }, { 2, 4, 6, 8 }, { 0, 9, 10, 11 } }); KthLargestNumberInStream kstream = new KthLargestNumberInStream(3, new int[] { 4, 5, 8, 2 }); var val = kstream.Add(3); // returns 4 val = kstream.Add(5); // returns 5 val = kstream.Add(10); // returns 5 val = kstream.Add(9); // returns 8 val = kstream.Add(4); // returns 8 PriorityQueue <int> heap = new PriorityQueue <int>(false); heap.Enqueue(4, 4); heap.Enqueue(5, 5); heap.Enqueue(8, 8); heap.Enqueue(2, 2); Console.Write("PQ:"); int cnt = heap.Count(); for (int i = 0; i < cnt; i++) { Console.Write($"{ heap.Dequeue()} "); } Console.WriteLine(); PQ <int> h = new PQ <int>(Comparer <int> .Create((a, b) => b.CompareTo(a))); h.Enqueue(5); h.Enqueue(2); h.Enqueue(8); h.Enqueue(9); Console.Write("PQ-New:"); cnt = h.Count(); for (int i = 0; i < cnt; i++) { Console.Write($"{ h.Dequeue()} "); } string s = "ab cd e j asd ljffg df"; var result = SortBasedOnsize.Sort(s); Console.WriteLine(); //Quick Sort int[] arr = new int[10] { 1, 5, 4, 11, 20, 8, 2, 98, 90, 16 }; SelectionSort.Sort(new int[] { 1, 7, 2, 4, 5 }); BubbleSort.Sort1(new int[] { 1, 7, 2, 4, 5 }); QuickSort obj = new QuickSort(); obj.Sort(arr, 0, arr.Length - 1); Console.WriteLine("Quick Sorted Values:"); for (int i = 0; i < arr.Length; i++) { Console.Write(arr[i] + " "); } Console.WriteLine(); int k = 5; var largest = KthLargestUnSortedArray.FindKthLargest(arr, 0, arr.Length - 1, arr.Length - k); Console.WriteLine($"{k}th largest element is: {largest}"); var smallest = KthLargestUnSortedArray.FindKthLargest(arr, 0, arr.Length - 1, k - 1); Console.WriteLine($"{k}th Smallest element is: {smallest}"); Console.WriteLine(); //Merge Sort arr = new int[5] { //7,4,5,31,8,2,6 7, 1, 2, 5, 4 }; MergeSort.Sort(arr, 0, arr.Length - 1); Console.WriteLine("Merge Sorted Values:"); for (int i = 0; i < arr.Length; i++) { Console.Write(arr[i] + " "); } Console.WriteLine(); //Heap Sort arr = new int[7] { 7, 4, 5, 31, 8, 2, 6 }; HeapSort.Sort(arr); Console.WriteLine("Heap Sorted Values:"); for (int i = 0; i < arr.Length; i++) { Console.Write(arr[i] + " "); } Console.WriteLine(); InsertionSort.Sort(new int[] { 1, 6, 2, 4, 3 }); Console.ReadLine(); }