Leetcode Problem 1793. Maximum Score of a Good Subarray

1793. Maximum Score of a Good Subarray

Leetcode Solutions

Approach: Greedy

  1. Initialize n = nums.length, left = k, right = k, ans = nums[k], and currMin = nums[k].
  2. While left > 0 or right < n - 1:
    • Compare nums[left - 1] with nums[right + 1].
    • If nums[right + 1] is greater, increment right and update currMin with nums[right] if it is lower.
    • Otherwise, decrement left and update currMin with nums[left] if it is lower.
    • Update ans with currMin * (right - left + 1) if it is greater.
  3. Return ans.
UML Thumbnail

Approach: Binary Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...