The Brute Force Approach involves iterating over all possible subarrays of the given array. For each subarray, find the maximum and minimum elements, and calculate the difference. Sum all these differences to get the final answer. While this approach is simple, it is not efficient for larger arrays due to its quadratic time complexity.
Time Complexity: O(n^2), where n is the length of the array, due to the nested loop structure.
Space Complexity: O(1), no extra data structures are used apart from loop variables.
1#include <iostream>
2#include <vector>
3#include <limits.h>
4
5using namespace std;
6
7int subArrayRanges(vector<int>& nums) {
8 int sum = 0;
9 int n = nums.size();
10 for (int i = 0; i < n; i++) {
11 int minVal = INT_MAX;
12 int maxVal = INT_MIN;
13 for (int j = i; j < n; j++) {
14 maxVal = max(maxVal, nums[j]);
15 minVal = min(minVal, nums[j]);
16 sum += (maxVal - minVal);
17 }
18 }
19 return sum;
20}
21
22int main() {
23 vector<int> nums = {1, 2, 3};
24 cout << subArrayRanges(nums) << endl;
25 return 0;
26}
Similar to the C solution, this C++ implementation uses nested loops to determine the maximum and minimum of each subarray, using STL functions to simplify the calculation. The subarray range is computed for every possible subarray and accumulated into the sum.
The Monotonic Stack Approach optimizes the calculation by focusing on the contribution of each element as a maximum or minimum in various subarrays. By using two stacks, the solution efficiently determines the range extend of each number within the subarray, deriving the number of subarrays it could be the maximum or minimum of. This optimized approach emphasizes reducing redundant calculations commonly inherent in brute force methods.
Time Complexity: O(n), regarded as linear due to the stack operations limiting the number of necessary operations to a reduced count.
Space Complexity: O(n), for storing the elements in the stack structures.
1function subArrayRanges(nums) {
2 let maxSum = 0, minSum = 0;
3 const n = nums.length;
4 const prev = new Array(n).fill(0);
5 const next = new Array(n).fill(0);
6 const stack = [];
7
8 for (let i = 0; i < n; ++i) {
9 while (stack.length && nums[stack[stack.length - 1]] > nums[i]) {
10 next[stack.pop()] = i;
11 }
12 stack.push(i);
13 }
14 while (stack.length) next[stack.pop()] = n;
15
16 for (let i = n - 1; i >= 0; --i) {
17 while (stack.length && nums[stack[stack.length - 1]] >= nums[i]) {
18 prev[stack.pop()] = i;
19 }
20 stack.push(i);
21 }
22 while (stack.length) prev[stack.pop()] = -1;
23
24 for (let i = 0; i < n; i++) {
25 minSum += (i - prev[i]) * (next[i] - i) * nums[i];
26 }
27
28 for (let i = 0; i < n; ++i) {
29 while (stack.length && nums[stack[stack.length - 1]] < nums[i]) {
30 next[stack.pop()] = i;
31 }
32 stack.push(i);
33 }
34 while (stack.length) next[stack.pop()] = n;
35
36 for (let i = n - 1; i >= 0; --i) {
37 while (stack.length && nums[stack[stack.length - 1]] <= nums[i]) {
38 prev[stack.pop()] = i;
39 }
40 stack.push(i);
41 }
42 while (stack.length) prev[stack.pop()] = -1;
43
44 for (let i = 0; i < n; i++) {
45 maxSum += (i - prev[i]) * (next[i] - i) * nums[i];
46 }
47
48 return maxSum - minSum;
49}
50
51console.log(subArrayRanges([1, 2, 3]));
JavaScript's stack-oriented implementation similarly incurs necessary steps to holistically capture subarray interactivity by treating sources of empowerment as varying subarray infrastructural participants.