Example #1
0
        public void CountingCarsTest()
        {
            var prefixSums = new PrefixSums();
            var cars       = new int[] { 0, 1, 0, 1, 1 };

            Assert.Equal(5, prefixSums.CountPassingCars(cars));
        }
Example #2
0
        public void MinAvgTwoSliceTest1()
        {
            var prefixSums = new PrefixSums();
            var nums       = new int[] { 4, 2, 2, 2, 5, 1, 5 };

            Assert.Equal(1, prefixSums.MinAvgTwoSlice(nums));
        }
Example #3
0
        private static double[] CalculateMeanForEveryN(int[] prefixSums, int n)
        {
            var p = new double[prefixSums.Length - n];

            for (var i = 0; i < p.Length; i++)
            {
                p[i] = PrefixSums.CountTotal(prefixSums, i, i + n - 1) / (double)n;
            }
            return(p);
        }
Example #4
0
        public static int Solve(int[] mushrooms, int m, int k)
        {
            var n      = mushrooms.Length;
            var result = 0;
            var pref   = PrefixSums.CalculatePrefixSums(mushrooms);

            foreach (var p in Enumerable.Range(0, Math.Min(m, k) + 1))
            {
                var leftPos  = k - p;
                var rightPos = Math.Min(n - 1, Math.Max(k, k + m - 2 * p));
                result = Math.Max(result, PrefixSums.CountTotal(pref, leftPos, rightPos));
            }
            foreach (var p in Enumerable.Range(0, Math.Min(m + 1, n - k)))
            {
                var rightPos = k + p;
                var leftPos  = Math.Max(0, Math.Min(k, k - (m - 2 * p)));
                result = Math.Max(result, PrefixSums.CountTotal(pref, leftPos, rightPos));
            }
            return(result);
        }
Example #5
0
        public static int Solve(int[] a)
        {
            if (a.Length == 2)
            {
                return(0);
            }

            var p     = PrefixSums.CalculatePrefixSums(a);
            var mean2 = CalculateMeanForEveryPair(p);
            var mean3 = CalculateMeanForEvery3(p);

            double min      = p[p.Length - 1] < 0 ? 0 : p[p.Length - 1];
            var    minIndex = -1;

            for (var i = 0; i < mean2.Length; i++)
            {
                if (!(mean2[i] < min))
                {
                    continue;
                }

                minIndex = i;
                min      = mean2[i];
            }

            for (var i = 0; i < mean3.Length; i++)
            {
                if (!(mean3[i] < min))
                {
                    continue;
                }

                minIndex = i;
                min      = mean3[i];
            }

            return(minIndex);
        }