Beispiel #1
0
        private int GetMaxSliceWithSwap(SlicedArray sliceResults, int[] A)
        {
            var maxSwappedSum    = int.MinValue;
            var maxSwappedLength = -1;

            Slice twoSlicesAgo = null;
            Slice lastSlice    = null;

            foreach (var slice in sliceResults.Slices)
            {
                if (slice.Sum < 0)
                {
                    twoSlicesAgo = lastSlice;
                    lastSlice    = slice;
                    continue;
                }

                int sum;
                int length;

                var maxValueNotInSlices = GetMaxValueNotInSlice(sliceResults.SlicesWithTopThreeMaxValues, slice, twoSlicesAgo);

                if (twoSlicesAgo != null &&
                    twoSlicesAgo.Sum >= 0 &&
                    slice.Sum >= 0 &&
                    lastSlice.MinIndex == lastSlice.MaxIndex)
                {
                    sum = slice.Sum + twoSlicesAgo.Sum +
                          ((maxValueNotInSlices > 0) ? maxValueNotInSlices : 0);
                    length = slice.MaxIndex - twoSlicesAgo.MinIndex;
                }
                else
                {
                    sum    = slice.Sum + ((maxValueNotInSlices > 0) ? maxValueNotInSlices : 0);
                    length = slice.MaxIndex - slice.MinIndex + 1;
                }

                if (sum == maxSwappedLength && length > maxSwappedLength)
                {
                    maxSwappedLength = length;
                }
                else if (sum > maxSwappedSum)
                {
                    maxSwappedLength = length;
                    maxSwappedSum    = sum;
                }

                twoSlicesAgo = lastSlice;
                lastSlice    = slice;
            }

            return(maxSwappedSum);
        }
Beispiel #2
0
        private int GetMaxSliceWithSwap(SlicedArray sliceResults, int[] A)
        {
            var maxSwappedSum = int.MinValue;
            var maxSwappedLength = -1;

            Slice twoSlicesAgo = null;
            Slice lastSlice = null;
            foreach (var slice in sliceResults.Slices)
            {
                if (slice.Sum < 0)
                {
                    twoSlicesAgo = lastSlice;
                    lastSlice = slice;
                    continue;
                }

                int sum;
                int length;

                var maxValueNotInSlices = GetMaxValueNotInSlice(sliceResults.SlicesWithTopThreeMaxValues, slice, twoSlicesAgo);

                if (twoSlicesAgo != null
                    && twoSlicesAgo.Sum >= 0
                    && slice.Sum >= 0
                    && lastSlice.MinIndex == lastSlice.MaxIndex)
                {
                    sum = slice.Sum + twoSlicesAgo.Sum +
                        ((maxValueNotInSlices > 0) ? maxValueNotInSlices : 0);
                    length = slice.MaxIndex - twoSlicesAgo.MinIndex;
                }
                else
                {
                    sum = slice.Sum + ((maxValueNotInSlices > 0) ? maxValueNotInSlices : 0);
                    length = slice.MaxIndex - slice.MinIndex + 1;
                }

                if (sum == maxSwappedLength && length > maxSwappedLength)
                {
                    maxSwappedLength = length;
                }
                else if (sum > maxSwappedSum)
                {
                    maxSwappedLength = length;
                    maxSwappedSum = sum;
                }

                twoSlicesAgo = lastSlice;
                lastSlice = slice;
            }

            return maxSwappedSum;
        }