Problem solution · C++

Find Maximum Non Decreasing Array Length

Find Maximum Non Decreasing Array Length: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Dynamic programming
Source
Kamyu LeetCode Solutions
Length
98 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Find Maximum Non Decreasing Array Length, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 98 lines of C++ from the credited upstream file find-maximum-non-decreasing-array-length.cpp.
  • The implementation visibly relies on sequence storage, work queue, cached states.
  • 10 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeFind Maximum Non Decreasing Array Length · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n)// Space: O(n) // dp, greedy, prefix sum, mono stack, two pointersclass Solution {public:    int findMaximumLength(vector<int>& nums) {        int dp = 0;        int64_t prefix = 0;        vector<vector<int64_t>> stk = {{0, 0, 0}};        for (int right = 0, left = 0; right < size(nums); ++right) {            prefix += nums[right];            for (; left + 1 < size(stk) && stk[left+1][0] <= prefix; ++left);            const int last = prefix - stk[left][1];            dp = stk[left][2] + 1;            while (!empty(stk) && stk.back()[0] >= last + prefix) {                stk.pop_back();            }            stk.push_back({last + prefix, prefix, dp});            left = min(left, static_cast<int>(size(stk) - 1));        }        return dp;    }}; // Time:  O(n)// Space: O(n)// dp, greedy, prefix sum, mono dequeclass Solution2 {public:    int findMaximumLength(vector<int>& nums) {        int dp = 0;        int64_t prefix = 0, prev_prefix = 0, prev_dp = 0;;        deque<vector<int64_t>> dq;        for (int right = 0; right < size(nums); ++right) {            prefix += nums[right];            for (; !empty(dq) && dq.front()[0] <= prefix; dq.pop_front()) {                prev_prefix = dq.front()[1];                prev_dp = dq.front()[2];            }            const int last = prefix - prev_prefix;            dp = prev_dp + 1;            while (!empty(dq) && dq.back()[0] >= last + prefix) {                dq.pop_back();            }            dq.push_back({last + prefix, prefix, dp});        }        return dp;    }}; // Time:  O(nlogn)// Space: O(n)// dp, greedy, prefix sum, mono stack, binary searchclass Solution3 {public:    int findMaximumLength(vector<int>& nums) {        int dp = 0;        int64_t prefix = 0;        vector<vector<int64_t>> stk = {{0, 0, 0}};        for (int right = 0; right < size(nums); ++right) {            prefix += nums[right];            const int left = distance(cbegin(stk), lower_bound(cbegin(stk), cend(stk), vector<int64_t>{prefix+1, 0, 0})) - 1;            const int last = prefix - stk[left][1];            dp = stk[left][2] + 1;            while (!empty(stk) && stk.back()[0] >= last + prefix) {                stk.pop_back();            }            stk.push_back({last + prefix, prefix, dp});        }        return dp;    }}; // Time:  O(nlogn)// Space: O(n)// dp, greedy, prefix sum, binary searchclass Solution4 {public:    int findMaximumLength(vector<int>& nums) {        vector<int64_t> prefix(size(nums) + 1);        for (int i = 0; i < size(nums); ++i) {            prefix[i + 1] = prefix[i] + nums[i];        }        vector<int64_t> dp(size(nums) + 1, numeric_limits<int>::max());        dp[0] = 0;        vector<int> prev(size(nums) + 1, -1);        for (int right = 0, left = -1; right < size(nums); ++right) {            left = max(left, prev[right]);            dp[right + 1] = dp[left + 1] + 1;            const int next_right = distance(cbegin(prefix), lower_bound(cbegin(prefix), cend(prefix), prefix[right + 1] + (prefix[right + 1] - prefix[left + 1]))) - 1;            prev[next_right] = right;        }        return dp.back();    }};  

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗