Max double slice sum

前端 未结 14 1159
后悔当初
后悔当初 2020-12-13 20:53

Recently, I tried to solve the Max Double Slice Sum problem in codility which is a variant of max slice problem. My Solution was to look for a slice that has maximum value w

14条回答
  •  离开以前
    2020-12-13 20:58

    Vb.net version of the above solution is as below:

    Private Function solution(A As Integer()) As Integer
        ' write your code in VB.NET 4.0
        Dim Slice1() As Integer = Ending(A)
            Dim slice2() As Integer = Starting(A)
            Dim maxSUM As Integer = 0
            For i As Integer = 1 To A.Length - 2
                maxSUM = Math.Max(maxSUM, Slice1(i - 1) + slice2(i + 1))
            Next
            Return maxSUM
    End Function
        Public Shared Function Ending(input() As Integer) As Integer()
            Dim result As Integer() = New Integer(input.Length - 1) {}
            result(0) = InlineAssignHelper(result(input.Length - 1), 0)
            For i As Integer = 1 To input.Length - 2
                result(i) = Math.Max(0, result(i - 1) + input(i))
            Next
            Return result
        End Function
        Public Shared Function Starting(input() As Integer) As Integer()
            Dim result As Integer() = New Integer(input.Length - 1) {}
            result(0) = InlineAssignHelper(result(input.Length - 1), 0)
            For i As Integer = input.Length - 2 To 1 Step -1
                result(i) = Math.Max(0, result(i + 1) + input(i))
            Next
            Return result
        End Function
            Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
                target = value
                Return value
            End Function
    

    View result on codility

提交回复
热议问题