Finding the longest non-negative sub array

前端 未结 3 875
粉色の甜心
粉色の甜心 2020-12-19 11:37

I\'m looking for an more efficient alternative to brute force for finding the longest sub array of an array with a non-negative sum. The numbers range from -5 to 5 in this a

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 12:12

    you can keep track of 11 things for each element while using dynamic programming, moving from left to right. Those 11 things correspond to the following possible sum :
    -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5
    For each of these you need to store the left most index, such that sum of the subarray starting at that index and ending in the current index gives that much sum.
    This is not a complete solution, but I believe this may serve the purpose of hint.

提交回复
热议问题