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.
1import java.util.*;
2
3public class SubarrayRanges {
4 public static int subArrayRanges(int[] nums) {
5 int sum = 0;
6 int n = nums.length;
7 for (int i = 0; i < n; i++) {
8 int minVal = Integer.MAX_VALUE;
9 int maxVal = Integer.MIN_VALUE;
10 for (int j = i; j < n; j++) {
11 maxVal = Math.max(maxVal, nums[j]);
12 minVal = Math.min(minVal, nums[j]);
13 sum += (maxVal - minVal);
14 }
15 }
16 return sum;
17 }
18
19 public static void main(String[] args) {
20 int[] nums = {1, 2, 3};
21 System.out.println(subArrayRanges(nums));
22 }
23}
This Java implementation applies the same logic, iterating through possible subarrays using nested loops. The inner loop updates the max and min values, contributing to the total sum all subarray ranges as required.
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.
1import java.util.*;
2
3public class SubarrayRanges {
4 public static int subArrayRanges(int[] nums) {
5 long maxSum = 0, minSum = 0;
6 int n = nums.length;
7 int[] prev = new int[n], next = new int[n];
8 Stack<Integer> stack = new Stack<>();
9
10 for (int i = 0; i < n; ++i) {
11 while (!stack.isEmpty() && nums[stack.peek()] > nums[i]) {
12 next[stack.pop()] = i;
13 }
14 stack.push(i);
15 }
16 while (!stack.isEmpty()) next[stack.pop()] = n;
17
18 for (int i = n - 1; i >= 0; --i) {
19 while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) {
20 prev[stack.pop()] = i;
21 }
22 stack.push(i);
23 }
24 while (!stack.isEmpty()) prev[stack.pop()] = -1;
25
26 for (int i = 0; i < n; i++) {
27 minSum += (long)(i - prev[i]) * (next[i] - i) * nums[i];
28 }
29
30 for (int i = 0; i < n; ++i) {
31 while (!stack.isEmpty() && nums[stack.peek()] < nums[i]) {
32 next[stack.pop()] = i;
33 }
34 stack.push(i);
35 }
36 while (!stack.isEmpty()) next[stack.pop()] = n;
37
38 for (int i = n - 1; i >= 0; --i) {
39 while (!stack.isEmpty() && nums[stack.peek()] <= nums[i]) {
40 prev[stack.pop()] = i;
41 }
42 stack.push(i);
43 }
44 while (!stack.isEmpty()) prev[stack.pop()] = -1;
45
46 for (int i = 0; i < n; i++) {
47 maxSum += (long)(i - prev[i]) * (next[i] - i) * nums[i];
48 }
49
50 return (int)(maxSum - minSum);
51 }
52
53 public static void main(String[] args) {
54 int[] nums = {1, 2, 3};
55 System.out.println(subArrayRanges(nums));
56 }
57}
Java offers stack usage for handling each intended role for elements by utilizing the next and previous relationships to determine extent to which each number influences subarrays as boundaries.