aaronlee27's blog

By aaronlee27, history, 3 years ago, In English

Given an array $$$a$$$, you have to divide this array into maximum numbers of continous subarrays that the sum of a subarray is greater or equal to every subarrays that come after it. $$$1 \le n \le 5 \times 10^5, 1 \le a_i \le 10^9$$$.

Example:

Input

4
6 5 2 3

Output

3

A possible solutions:

6 | 5 | 2 3

Can anyone help me with this problems? I only came up with $$$O(n^2)$$$ solution. Thanks in advance!

  • Vote: I like it
  • +17
  • Vote: I do not like it

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can you share problem link

»
3 years ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

nvm

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I have a weird idea. Unsure if it works. Let's add all elements to a set each element is currently a subarray. If there's an element $$$x$$$ with an element $$$y$$$ after it such that $$$x \lt y$$$ then we remove $$$y$$$ from the set and add it to $$$x$$$. Can someone provide a case where this fails?

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    If I understood your idea correctly, $$$[3, 3, 4]$$$ would fail. Your idea would merge the last two elements making the array $$$[3, 7]$$$ which would need to be merged again. The optimal solution is to merge the first two elements making the array $$$[6, 4]$$$.