Example #1
0
        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();
        }
        protected override void Execute(CodeActivityContext executionContext)
        {
            ITracingService tracer = executionContext.GetExtension <ITracingService>();

            try
            {
                string stringToCount = StringToCount.Get(executionContext);

                string[] words = stringToCount.Trim().Split(' ');

                Count.Set(executionContext, words.Length);
            }
            catch (Exception ex)
            {
                tracer.Trace("Exception: {0}", ex.ToString());
            }
        }
        protected override void ExecuteCrmWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext localContext)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (localContext == null)
            {
                throw new ArgumentNullException(nameof(localContext));
            }

            string stringToCount = StringToCount.Get(context);

            string[] words = stringToCount.Trim().Split(' ');

            Count.Set(context, words.Length);
        }