Python3 solution with edge cases, all Ok Test cases


#1

Solution in python with all test cases passed:

# @param A : list of integers
# @return a list of integers
def maxset(self, A):
    res = list()
    sub_array = list()
    final = 0
    current = 0
    final_len = -1
    for index in range(len(A)):
        if A[index]>=0:
            sub_array.append(A[index])
            current, maxlen = sum(sub_array), len(sub_array)
            if current > final:
                final = current
                res = sub_array
            elif current==final:
                if maxlen > final_len:
                    final_len = maxlen
                    res = sub_array
        else:
            sub_array=[]
        #print("Loop-->{}, val-->{}, Result-->{}, old_max-->{}, current-->{}".format(index,A[index],res,final,current))
    return res

`
You can check the debug output to check all variable stack. The trick was to handle the edge cases in case of Tie.