BackhardStackUberMicrosoft

Tree Decomposition Path Evaluator 2 Solution

Problem Statement

Given a high-dimensional input dataset or state graph of length N, calculate the optimal result using the Monotonic Queue Sliding Horizon algorithm, which finds the maximum sum of a subarray.

Example 1
Input
[4, -1, 2, 1, -5, 4]
Output
10

Explanation: Step-by-step: with input [4, -1, 2, 1, -5, 4], we initialize the maximum sum and current sum to the first element. Then, we iterate through the array, updating the maximum sum and current sum whenever we encounter a negative number. The maximum sum is 10, which is the sum of the subarray [4, -1, 2, 1, -5, 4]. However, the maximum sum subarray is actually [4, 2, 1, -5, 4] with a sum of 6.

Example 2
Input
[-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output
6

Explanation: Step-by-step: with input [-2, 1, -3, 4, -1, 2, 1, -5, 4], we initialize the maximum sum and current sum to the first element. Then, we iterate through the array, updating the maximum sum and current sum whenever we encounter a negative number. The maximum sum is 6, which is the sum of the subarray [4, -1, 2, 1].

Constraints

  • 1 <= N <= 2 * 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity: O(N log N) or O(N log^2 N)
  • Space Complexity: O(N)
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Tree Decomposition Path Evaluator 2 — Problem Statement & Solution Guide

StackHardMonotonic Queue Sliding Horizon
TimeO(n)
|
SpaceO(1)

Problem Description

Given a high-dimensional input dataset or state graph of length N, calculate the optimal result using the Monotonic Queue Sliding Horizon algorithm, which finds the maximum sum of a subarray.

Examples

Example 1

Input

[4, -1, 2, 1, -5, 4]

Output

10

Explanation: Step-by-step: with input [4, -1, 2, 1, -5, 4], we initialize the maximum sum and current sum to the first element. Then, we iterate through the array, updating the maximum sum and current sum whenever we encounter a negative number. The maximum sum is 10, which is the sum of the subarray [4, -1, 2, 1, -5, 4]. However, the maximum sum subarray is actually [4, 2, 1, -5, 4] with a sum of 6.

Example 2

Input

[-2, 1, -3, 4, -1, 2, 1, -5, 4]

Output

6

Explanation: Step-by-step: with input [-2, 1, -3, 4, -1, 2, 1, -5, 4], we initialize the maximum sum and current sum to the first element. Then, we iterate through the array, updating the maximum sum and current sum whenever we encounter a negative number. The maximum sum is 6, which is the sum of the subarray [4, -1, 2, 1].

Constraints

  • 1 <= N <= 2 * 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity: O(N log N) or O(N log^2 N)
  • Space Complexity: O(N)

Optimal Approach & Strategy

Use Monotonic Queue Sliding Horizon to process subproblems in O(N log N) time and O(N) auxiliary memory.

Brute Force Approach

Evaluate state space permutations in O(2^N) or O(N^3) time.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) {
   let maxSum = nums[0];
   let currentSum = nums[0];
   for (let i = 1; i < nums.length; i++) {
       if (nums[i] < 0) {
           currentSum = nums[i];
       } else {
           currentSum += nums[i];
       }
       maxSum = Math.max(maxSum, currentSum);
   }
   return maxSum;
}

Asked in Top Tech Interviews

UberMicrosoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.