Clear Python O(N) solution using dictionary


#1

Take a look on my solution:

    def max_set(self, array):
        len_of_arr = len(array)
        positive_arrays = []
        tmp_array = []
        result_dict = dict()
        for i in range(len_of_arr):
            if array[i] >= 0:
                tmp_array.append(array[i])
            elif len(tmp_array) > 0 or i == len_of_arr:
                positive_arrays.append(tmp_array)
                tmp_sum = sum(tmp_array)
                if result_dict.get(tmp_sum):
                    if len(tmp_array) > len(result_dict.get(tmp_sum)):
                        result_dict[sum(tmp_array)] = tmp_array
                else:
                    result_dict[sum(tmp_array)] = tmp_array
                tmp_array = []
        return result_dict[max(result_dict.keys())]