BackmediumArraysUber

Consecutive Resource Allocation Solution

Problem Statement

Given a sequence of daily resource quantities represented as an array of integers resources and the number of consecutive days k a spaceship needs to dock, determine the maximum total resource quantity that can be allocated within any k-day window. A window is considered 'consecutive' if it contains k contiguous days, where the start and end days are inclusive.

Example 1
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output
40

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and k = 5, we first calculate the sum of the first 5 elements (1 + 2 + 3 + 4 + 5 = 15). Then, we slide the window to the right by one element and calculate the sum of the next 5 elements (2 + 3 + 4 + 5 + 6 = 20). We continue this process until we reach the end of the array. The maximum sum we get is 20, but we also need to consider the sum of the first 1 element (1) and the sum of the first 2 elements (1 + 2 = 3), which are also valid k-day windows. However, the maximum sum of any 5-day window is indeed 40, which is the sum of the last 5 elements (6 + 7 + 8 + 9 + 10 = 40).

Example 2
Input
[1, 2, 3, 4, 5]
Output
15

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5] and k = 3, we first calculate the sum of the first 3 elements (1 + 2 + 3 = 6). Then, we slide the window to the right by one element and calculate the sum of the next 3 elements (2 + 3 + 4 = 9). We continue this process until we reach the end of the array. The maximum sum we get is 9, but we also need to consider the sum of the first 1 element (1) and the sum of the first 2 elements (1 + 2 = 3), which are also valid k-day windows. However, the maximum sum of any 3-day window is indeed 9, which is the sum of the last 3 elements (3 + 4 + 5 = 12).

Constraints

  • 1 <= resources.length <= 10^5
  • -10^4 <= resources[i] <= 10^4
  • 1 <= k <= resources.length
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

Consecutive Resource Allocation — Problem Statement & Solution Guide

ArraysMediumSliding Window
TimeO(n)
|
SpaceO(1)

Problem Description

Given a sequence of daily resource quantities represented as an array of integers resources and the number of consecutive days k a spaceship needs to dock, determine the maximum total resource quantity that can be allocated within any k-day window. A window is considered 'consecutive' if it contains k contiguous days, where the start and end days are inclusive.

Examples

Example 1

Input

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Output

40

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and k = 5, we first calculate the sum of the first 5 elements (1 + 2 + 3 + 4 + 5 = 15). Then, we slide the window to the right by one element and calculate the sum of the next 5 elements (2 + 3 + 4 + 5 + 6 = 20). We continue this process until we reach the end of the array. The maximum sum we get is 20, but we also need to consider the sum of the first 1 element (1) and the sum of the first 2 elements (1 + 2 = 3), which are also valid k-day windows. However, the maximum sum of any 5-day window is indeed 40, which is the sum of the last 5 elements (6 + 7 + 8 + 9 + 10 = 40).

Example 2

Input

[1, 2, 3, 4, 5]

Output

15

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5] and k = 3, we first calculate the sum of the first 3 elements (1 + 2 + 3 = 6). Then, we slide the window to the right by one element and calculate the sum of the next 3 elements (2 + 3 + 4 = 9). We continue this process until we reach the end of the array. The maximum sum we get is 9, but we also need to consider the sum of the first 1 element (1) and the sum of the first 2 elements (1 + 2 = 3), which are also valid k-day windows. However, the maximum sum of any 3-day window is indeed 9, which is the sum of the last 3 elements (3 + 4 + 5 = 12).

Constraints

  • 1 <= resources.length <= 10^5
  • -10^4 <= resources[i] <= 10^4
  • 1 <= k <= resources.length

Optimal Approach & Strategy

The optimized approach uses the sliding window technique to achieve a time complexity of O(n). It involves maintaining a running sum of the current window and updating it as the window moves through the array. This is done by subtracting the element that is no longer in the window and adding the new element that enters the window.

Brute Force Approach

The brute-force approach involves checking every possible subarray of the given size and calculating its sum, resulting in a time complexity of O(n²). This can be achieved by using two nested loops to generate all possible subarrays. The outer loop iterates over the starting index of the subarray, and the inner loop iterates over the ending index.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function maxConsecutiveSum(resources, k) {
  if (k > resources.length || k <= 0) return 0;
  let maxSum = -Infinity;
  let currentSum = 0;
  for (let i = 0; i < resources.length; i++) {
    currentSum += resources[i];
    if (i >= k) {
      currentSum -= resources[i - k];
    }
    if (i >= k - 1) {
      maxSum = Math.max(maxSum, currentSum);
    }
  }
  return maxSum;
}

Asked in Top Tech Interviews

Uber

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.