Example #1
0
        /*
         * Complete the 'angryAnimals' function below.
         *
         * The function is expected to return a LONG_INTEGER.
         * The function accepts following parameters:
         *  1. INTEGER n
         *  2. INTEGER_ARRAY a
         *  3. INTEGER_ARRAY b
         */

        public static long angryAnimals(int n, List <int> a, List <int> b)
        {
            //List<List<string>> lstSafeCombinations = new List<List<string>>();
            AngryComboDict dict            = new AngryComboDict(a, b);
            long           count           = n;
            List <long>    lstResultCounts = new List <long>();



            for (int i = 1; i <= n; i++)
            {
                //  lstSafeCombinations.Add(new List<string>());
                bool skipAhead = false;
//                string items = "";
                for (int j = i + 1; j <= n; j++)
                {
                    //for(int k=i; k < j; k++)
                    //{
                    //if (!dict.KeyExists(k))
                    //{
                    //    break;
                    //}

                    //find limit between i and j so that it can be verified and loop is skipped

                    if (dict.KeyValuePairExistsMinMax(j, i))
                    {
                        skipAhead = true;
                        break;
                    }
                    //}

                    if (!skipAhead)
                    {
                        count = count + 1;
                        //       lstSafeCombinations[i - 1].Add("--"+j.ToString());
                    }
                    else
                    {
                        break;
                    }
                }
            }


            //int count = n;
            //foreach(List<string> lst in lstSafeCombinations)
            //{
            //    count = count + lst.Count;
            //}

            return(count);
        }
        public static void CalculatePartitionResult(object stateParams)
        {
            ThreadParams thrParams = (ThreadParams)stateParams;

            int            start = thrParams.start;
            int            end   = thrParams.end;
            int            n     = thrParams.nCount;
            AngryComboDict dict  = thrParams.dict;
            long           count = 0;//thrParams.Count;

            for (int i = start; i <= end; i++)
            {
                //  lstSafeCombinations.Add(new List<string>());
                bool skipAhead = false;
                //                string items = "";
                for (int j = i + 1; j <= n; j++)
                {
                    //for(int k=i; k < j; k++)
                    //{
                    //if (!dict.KeyExists(k))
                    //{
                    //    break;
                    //}

                    //find limit between i and j so that it can be verified and loop is skipped

                    if (dict.KeyValuePairExistsMinMax(j, i))
                    {
                        skipAhead = true;
                        break;
                    }
                    //}

                    if (!skipAhead)
                    {
                        count = count + 1;
                        //       lstSafeCombinations[i - 1].Add("--"+j.ToString());
                    }
                    else
                    {
                        break;
                    }
                }
            }

            thrParams.Count = count;
        }
        /*
         * Complete the 'angryAnimals' function below.
         *
         * The function is expected to return a LONG_INTEGER.
         * The function accepts following parameters:
         *  1. INTEGER n
         *  2. INTEGER_ARRAY a
         *  3. INTEGER_ARRAY b
         */

        public static long angryAnimals(int n, List <int> a, List <int> b)
        {
            // List<List<string>> lstSafeCombinations = new List<List<string>>();
            AngryComboDict dict  = new AngryComboDict(a, b);
            long           count = n;


            Parallel.For(1, n, new ParallelOptions {
                MaxDegreeOfParallelism = 1
            }, (i, state) => {
                //lock (dict)
                //{
                //    lstSafeCombinations.Add(new List<string>());
                //}
                long localCount = 0;
                for (int j = i + 1; j <= n; j++)
                {
                    Console.WriteLine("(i:{0},j:{1})->>", i, j);
                    if (dict.KeyExists(j))
                    {
                        Console.WriteLine("(i:{0},j:{1})   KeyExists for {0} -> with Min Value {2}", i, j, dict.GetMinValueAgainstKey(j));
                    }
                    //find limit between i and j so that it can be verified and loop is skipped
                    if (dict.KeyValuePairExistsMinMax(j, i))
                    {
                        Console.WriteLine("(i:{0},j:{1})::Break", i, j);
                        break;
                    }

                    localCount = localCount + 1;
                    //lock (dict)
                    //{ lstSafeCombinations[i - 1].Add("--" + j.ToString()); }
                }
                lock (dict)
                {
                    count = count + localCount;
                }
            });
            return(count);
        }
        /*
         * Complete the 'angryAnimals' function below.
         *
         * The function is expected to return a LONG_INTEGER.
         * The function accepts following parameters:
         *  1. INTEGER n
         *  2. INTEGER_ARRAY a
         *  3. INTEGER_ARRAY b
         */

        public static long angryAnimals(int n, List <int> a, List <int> b)
        {
            //List<List<string>> lstSafeCombinations = new List<List<string>>();
            AngryComboDict dict  = new AngryComboDict(a, b);
            long           count = n;

            //{
            //    List<ThreadParams> lstResultCounts = new List<ThreadParams>();
            //    List<Thread> lstThread = new List<Thread>();

            //    int thrCount = 8;
            //    int partitions = n / thrCount;

            //    for (int i = 0; i < partitions; i++)
            //    {
            //        int upperLimit = ((i * thrCount) + 1);
            //        ThreadParams tp = new ThreadParams()
            //        {
            //            start = upperLimit,
            //            end = n,
            //            nCount = n,
            //            dict = dictionary
            //        };

            //        Thread t = new Thread(new ParameterizedThreadStart(CalculatePartitionResult));

            //        lstThread.Add(t);
            //        lstResultCounts.Add(tp);

            //        t.Start(tp);
            //    }
            //}
            //for (int i = 1; i <= n; i++)
            //{
            //    //  lstSafeCombinations.Add(new List<string>());
            //    bool skipAhead = false;
            //    //                string items = "";
            //    for (int j = i + 1; j <= n; j++)
            //    {

            //        //for(int k=i; k < j; k++)
            //        //{
            //        //if (!dict.KeyExists(k))
            //        //{
            //        //    break;
            //        //}

            //        //find limit between i and j so that it can be verified and loop is skipped

            //        if (dictionary.KeyValuePairExistsMinMax(j, i))
            //        {
            //            skipAhead = true;
            //            break;
            //        }
            //        //}

            //        if (!skipAhead)
            //        {
            //            count = count + 1;
            //            //       lstSafeCombinations[i - 1].Add("--"+j.ToString());
            //        }
            //        else
            //        {
            //            break;
            //        }
            //    }
            //}

            //            t1.Start(tp);
            //          t1.Join();



            //            count = count + tp.Count;


            Parallel.For(1, n, new ParallelOptions {
                MaxDegreeOfParallelism = 10
            }, (i, state) => {
                //   lstSafeCombinations.Add(new List<string>());
                long localCount = 0;
                bool skipAhead  = false;
                //                string items = "";
                for (int j = i + 1; j <= n; j++)
                {
                    //for(int k=i; k < j; k++)
                    //{
                    //if (!dict.KeyExists(k))
                    //{
                    //    break;
                    //}

                    //find limit between i and j so that it can be verified and loop is skipped


                    if (dict.KeyValuePairExistsMinMax(j, i))
                    {
                        skipAhead = true;
                        break;
                    }
                    //}

                    if (!skipAhead)
                    {
                        localCount = localCount + 1;
                        //       lstSafeCombinations[i - 1].Add("--"+j.ToString());
                    }
                    else
                    {
                        break;
                    }
                }
                lock (dict)
                {
                    count = count + localCount;
                }
            });

            return(count);
        }