1793. Maximum Score of a Good Subarray LeetCode Solution

In this guide, you will get 1793. Maximum Score of a Good Subarray LeetCode Solution with the best time and space complexity. The solution to Maximum Score of a Good Subarray 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

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Score of a Good Subarray solution in C++
  4. Maximum Score of a Good Subarray solution in Java
  5. Maximum Score of a Good Subarray solution in Python
  6. Additional Resources
1793. Maximum Score of a Good Subarray LeetCode Solution image

Problem Statement of Maximum Score of a Good Subarray

You are given an array of integers nums (0-indexed) and an integer k.
The score of a subarray (i, j) is defined as min(nums[i], nums[i+1], …, nums[j]) * (j – i + 1). A good subarray is a subarray where i <= k <= j.
Return the maximum possible score of a good subarray.

Example 1:

Input: nums = [1,4,3,7,4,5], k = 3
Output: 15
Explanation: The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.

Example 2:

Input: nums = [5,5,4,5,4,1,1,1], k = 0
Output: 20
Explanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.

Constraints:

1 <= nums.length <= 105
1 <= nums[i] <= 2 * 104
0 <= k < nums.length

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1793. Maximum Score of a Good Subarray LeetCode Solution in C++

class Solution {
 public:
  // Similar to 84. Largest Rectangle in Histogram
  int maximumScore(vector<int>& nums, int k) {
    int ans = 0;
    stack<int> stack;

    for (int i = 0; i <= nums.size(); ++i) {
      while (!stack.empty() &&
             (i == nums.size() || nums[stack.top()] > nums[i])) {
        const int h = nums[stack.top()];
        stack.pop();
        const int w = stack.empty() ? i : i - stack.top() - 1;
        if ((stack.empty() || stack.top() + 1 <= k) && i - 1 >= k)
          ans = max(ans, h * w);
      }
      stack.push(i);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1793. Maximum Score of a Good Subarray LeetCode Solution in Java

class Solution {
  // Similar to 84. Largest Rectangle in Histogram
  public int maximumScore(int[] nums, int k) {
    int ans = 0;
    Deque<Integer> stack = new ArrayDeque<>();

    for (int i = 0; i <= nums.length; ++i) {
      while (!stack.isEmpty() && (i == nums.length || nums[stack.peek()] > nums[i])) {
        final int h = nums[stack.pop()];
        final int w = stack.isEmpty() ? i : i - stack.peek() - 1;
        if ((stack.isEmpty() || stack.peek() + 1 <= k) && i - 1 >= k)
          ans = Math.max(ans, h * w);
      }
      stack.push(i);
    }

    return ans;
  }
}
// code provided by PROGIEZ

1793. Maximum Score of a Good Subarray LeetCode Solution in Python

class Solution:
  # Similar to 84. Largest Rectangle in Histogram
  def maximumScore(self, nums: list[int], k: int) -> int:
    ans = 0
    stack = []

    for i in range(len(nums) + 1):
      while stack and (i == len(nums) or nums[stack[-1]] > nums[i]):
        h = nums[stack.pop()]
        w = i - stack[-1] - 1 if stack else i
        if (not stack or stack[-1] + 1 <= k) and i - 1 >= k:
          ans = max(ans, h * w)
      stack.append(i)

    return ans
# code by PROGIEZ

Additional Resources

See also  478. Generate Random Point in a Circle LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.