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.
1def subArrayRanges(nums):
2 total = 0
3 n = len(nums)
4 for i in range(n):
5 min_val = float('inf')
6 max_val = float('-inf')
7 for j in range(i, n):
8 max_val = max(max_val, nums[j])
9 min_val = min(min_val, nums[j])
10 total += max_val - min_val
11 return total
12
13# Example Usage
14print(subArrayRanges([1, 2, 3]))
Utilizes a similar double loop structure. The Python code leverages built-in functions to manage and compute maximum and minimum values within the subarrays.
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.