public static void fnTopKFrequentNumbers() { int[] nums = new int[] { 6, 0, 1, 4, 9, 7, -3, 1, -4, -8, 4, -7, -3, 3, 2, -3, 9, 5, -4, 0 }; int k = 6; IList <int> list = new List <int>(); if (nums == null || nums.Length == 0) { return; } Dictionary <int, int> dict = new Dictionary <int, int>(); for (int i = 0; i < nums.Length; i++) { if (!dict.ContainsKey(nums[i])) { dict.Add(nums[i], 0); } dict[nums[i]]++; } MinHeapRecurse <NumToCount> minHeap = new MinHeapRecurse <NumToCount>(k, new NumToCountComparer()); foreach (var item in dict) { NumToCount nc = new NumToCount(item.Key, item.Value); if (minHeap.Size() == k) { if (minHeap.Peek().count < nc.count) { minHeap.DeleteMin(); } } minHeap.Insert(nc); } int l = minHeap.Size(); while (l-- > 0) { list.Add(minHeap.DeleteMin().number); } list.Reverse(); for (int i = 0; i < list.Count; i++) { Console.Write(list[i] + " "); } Console.WriteLine(); }
public static void fnTopKFrequentWords() { //string[] arr = new string[] { "geek", "i", "love", "leetcode", "i", "love", "coding", "geek" }; //string[] arr = new string[] { "the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is", }; string[] arr = new string[] { "i", "love", "leetcode", "i", "love", "coding" }; Dictionary <string, int> dict = new Dictionary <string, int>(); int k = 3; for (int i = 0; i < arr.Length; i++) { if (!dict.ContainsKey(arr[i])) { dict.Add(arr[i], 0); } dict[arr[i]]++; } MinHeapRecurse <StringToCount> minHeap = new MinHeapRecurse <StringToCount>(k, new StringToCountComparer()); foreach (var item in dict) { StringToCount sc = new StringToCount(item.Key, item.Value); if (minHeap.Size() == k) { if (minHeap.Peek().count < sc.count || (minHeap.Peek().count < sc.count && sc.str.CompareTo(minHeap.Peek().str) < 0)) { minHeap.DeleteMin(); } } minHeap.Insert(sc); } List <string> strList = new List <string>(); int l = minHeap.Size(); while (l-- > 0) { strList.Add(minHeap.DeleteMin().str); } strList.Reverse(); for (int i = 0; i < strList.Count; i++) { Console.Write(strList[i] + " "); } Console.WriteLine(); }
public static string fnRemoveKdigits(string num, int k) { if (string.IsNullOrEmpty(num) || num.Length < k) { return(string.Empty); } MinHeapRecurse <int> mh = new MinHeapRecurse <int>(k, new IntComparer()); for (int i = 0; i < num.Length; i++) { if (k == mh.Size()) { if ((num[i] - '0') > mh.Peek()) { mh.DeleteMin(); } } mh.Insert(num[i] - '0'); } HashSet <int> hs = new HashSet <int>(); while (mh.Size() > 0) { var del = mh.DeleteMin(); hs.Add(del); } StringBuilder sb = new StringBuilder(); for (int j = num.Length - 1; j >= 0; j--) { if (!hs.Contains(num[j] - '0')) { sb.Append(num[j]); } else { hs.Remove(num[j] - '0'); } } var list = sb.ToString().Reverse().ToArray(); list.Reverse(); return(new string(list)); }
public static void kLargest() { int t = 1; while (t-- > 0) { int[] sizeAndLength = new int[] { 8, 3 }; int[] array = Array.ConvertAll("1 23 12 90 100 30 2 50".Split(' '), int.Parse); MinHeapRecurse <int> minHeap = new MinHeapRecurse <int>(sizeAndLength[1], new IntComparer()); for (int i = 0; i < sizeAndLength[1]; i++) { minHeap.Insert(array[i]); } for (int i = sizeAndLength[1]; i < sizeAndLength[0]; i++) { if (minHeap.Peek() < array[i]) { minHeap.DeleteMin(); minHeap.Insert(array[i]); } } List <int> list = new List <int>(); for (int i = 0; i < sizeAndLength[1]; i++) { list.Add(minHeap.DeleteMin()); } list.Reverse(); for (int i = 0; i < list.Count; i++) { Console.Write(list[i] + " "); } Console.WriteLine(); } }