O(n) solution with detailed explanation
\n\n\n> image link: https://raw.githubusercontent.com/neal2018/blog/master/articles/LC2945_good.png\n\nAny question is welcomed.\n\nProvide code for reference.\
\n![Alt text](https://raw.githubusercontent.com/neal2018/blog/master/articles/LC2945_good.png?sanitize=true)\n\n> image link: https://raw.githubusercontent.com/neal2018/blog/master/articles/LC2945_good.png\n\nAny question is welcomed.\n\nProvide code for reference.\n\n```\nclass Solution {\npublic:\n    int findMaximumLength(vector<int>& nums) {\n        using ll = long long;\n        const auto n = nums.size();\n        vector<array<ll, 3>> stk{{0, 0, 0}};  // last + pre, pre, dp\n        // dp[i] = dp[j] + 1\n        // a[j+1] + ... + a[i] >= last[j]\n        //   pre[i] - pre[j] >= last[j]\n        //   pre[i] >= last[j] + pre[j]\n        ll pre = 0;\n        ll res = 0;\n        for (size_t i = 0, j = 0; i < n; i++) {\n            pre += nums[i];\n            j = min(j, stk.size() - 1);\n            while (j + 1 < stk.size() && pre >= stk[j+1][0]) j++;\n            const auto [val, pre_pre, pre_dp] = stk[j];\n            const auto cur_dp = pre_dp + 1;\n            res = cur_dp;\n\n            const auto last = pre - pre_pre;\n            while (stk.size() && stk.back()[0] >= last + pre) stk.pop_back();\n            stk.push_back({last + pre, pre, cur_dp});\n        }\n        return res;\n    }\n};\n```
['Dynamic Programming']