Skip to content

1793. Maximum Score of a Good Subarray 👍

Approach 1: Stack

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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

Approach 2: Heuristic

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
 public:
  int maximumScore(vector<int>& nums, int k) {
    const int n = nums.size();
    int ans = nums[k];
    int mn = nums[k];
    int i = k;
    int j = k;

    // Greedily expand the window and decrease the minimum as slow as possible.
    while (i > 0 || j < n - 1) {
      if (i == 0)
        ++j;
      else if (j == n - 1)
        --i;
      else if (nums[i - 1] < nums[j + 1])
        ++j;
      else  // nums[i - 1] >= nums[j + 1]
        --i;
      mn = min({mn, nums[i], nums[j]});
      ans = max(ans, mn * (j - i + 1));
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
  public int maximumScore(int[] nums, int k) {
    final int n = nums.length;
    int ans = nums[k];
    int mn = nums[k];
    int i = k;
    int j = k;

    // Greedily expand the window and decrease the minimum as slow as possible.
    while (i > 0 || j < n - 1) {
      if (i == 0)
        ++j;
      else if (j == n - 1)
        --i;
      else if (nums[i - 1] < nums[j + 1])
        ++j;
      else
        --i;
      mn = Math.min(mn, Math.min(nums[i], nums[j]));
      ans = Math.max(ans, mn * (j - i + 1));
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
  def maximumScore(self, nums: list[int], k: int) -> int:
    n = len(nums)
    ans = nums[k]
    mn = nums[k]
    i = k
    j = k

    # Greedily expand the window and decrease the minimum as slow as possible.
    while i > 0 or j < n - 1:
      if i == 0:
        j += 1
      elif j == n - 1:
        i -= 1
      elif nums[i - 1] < nums[j + 1]:
        j += 1
      else:
        i -= 1
      mn = min(mn, nums[i], nums[j])
      ans = max(ans, mn * (j - i + 1))

    return ans