3113. Find the Number of Subarrays Where Boundary Elements Are Maximum LeetCode Solution
In this guide, you will get 3113. Find the Number of Subarrays Where Boundary Elements Are Maximum LeetCode Solution with the best time and space complexity. The solution to Find the Number of Subarrays Where Boundary Elements Are Maximum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Number of Subarrays Where Boundary Elements Are Maximum solution in C++
- Find the Number of Subarrays Where Boundary Elements Are Maximum solution in Java
- Find the Number of Subarrays Where Boundary Elements Are Maximum solution in Python
- Additional Resources
Problem Statement of Find the Number of Subarrays Where Boundary Elements Are Maximum
You are given an array of positive integers nums.
Return the number of subarrays of nums, where the first and the last elements of the subarray are equal to the largest element in the subarray.
Example 1:
Input: nums = [1,4,3,3,2]
Output: 6
Explanation:
There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:
subarray [1,4,3,3,2], with its largest element 1. The first element is 1 and the last element is also 1.
subarray [1,4,3,3,2], with its largest element 4. The first element is 4 and the last element is also 4.
subarray [1,4,3,3,2], with its largest element 3. The first element is 3 and the last element is also 3.
subarray [1,4,3,3,2], with its largest element 3. The first element is 3 and the last element is also 3.
subarray [1,4,3,3,2], with its largest element 2. The first element is 2 and the last element is also 2.
subarray [1,4,3,3,2], with its largest element 3. The first element is 3 and the last element is also 3.
Hence, we return 6.
Example 2:
Input: nums = [3,3,3]
Output: 6
Explanation:
There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:
subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
Hence, we return 6.
Example 3:
Input: nums = [1]
Output: 1
Explanation:
There is a single subarray of nums which is [1], with its largest element 1. The first element is 1 and the last element is also 1.
Hence, we return 1.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
3113. Find the Number of Subarrays Where Boundary Elements Are Maximum LeetCode Solution in C++
class Solution {
public:
long long numberOfSubarrays(vector<int>& nums) {
long ans = 0;
vector<pair<int, int>> stack;
for (const int num : nums) {
while (!stack.empty() && stack.back().first < num)
stack.pop_back();
if (stack.empty() || stack.back().first != num)
stack.emplace_back(num, 0);
ans += ++stack.back().second;
}
return ans;
}
};
/* code provided by PROGIEZ */
3113. Find the Number of Subarrays Where Boundary Elements Are Maximum LeetCode Solution in Java
class Solution {
public long numberOfSubarrays(int[] nums) {
Deque<int[]> stack = new ArrayDeque<>();
long ans = 0;
int top = -1;
for (final int num : nums) {
while (!stack.isEmpty() && stack.peek()[0] < num)
stack.pop();
if (stack.isEmpty() || stack.peek()[0] != num)
stack.push(new int[] {num, 0});
ans += ++stack.peek()[1];
}
return ans;
}
}
// code provided by PROGIEZ
3113. Find the Number of Subarrays Where Boundary Elements Are Maximum LeetCode Solution in Python
class Solution:
def numberOfSubarrays(self, nums: list[int]) -> int:
ans = 0
stack = []
for num in nums:
while stack and stack[-1][0] < num:
stack.pop()
if not stack or stack[-1][0] != num:
stack.append([num, 0])
stack[-1][1] += 1
ans += stack[-1][1]
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.