1. Brute Force

class Solution:
    def findMaximums(self, nums: List[int]) -> List[int]:
        n = len(nums)
        res = [0] * n
        for i in range(n):
            curMin = nums[i]
            for j in range(i, n):
                curMin = min(curMin, nums[j])
                res[j - i] = max(res[j - i], curMin)
        return res

Time & Space Complexity

  • Time complexity: O(n2)O(n ^ 2)
  • Space complexity: O(1)O(1) extra space.

Where nn is the length of the array numsnums.


2. Sliding Window (Monotonic Deque)

class Solution:
    def findMaximums(self, nums: List[int]) -> List[int]:
        n = len(nums)
        res = [0] * n
        for k in range(1, n + 1):
            q = deque()
            for r in range(n):
                while q and nums[q[-1]] >= nums[r]:
                    q.pop()
                q.append(r)
                if q[0] <= r - k:
                    q.popleft()
                if r >= k - 1:
                    res[k - 1] = max(res[k - 1], nums[q[0]])
        return res

Time & Space Complexity

  • Time complexity: O(n2)O(n ^ 2)
  • Space complexity: O(n)O(n)

Where nn is the length of the array numsnums.


3. Disjoint Set Union

class DSU:
    def __init__(self, n):
        self.parent = list(range(n))
        self.size = [1] * n

    def find(self, node):
        cur = node
        while cur != self.parent[cur]:
            self.parent[cur] = self.parent[self.parent[cur]]
            cur = self.parent[cur]
        return cur

    def union(self, u, v):
        pu = self.find(u)
        pv = self.find(v)
        if pu == pv:
            return False
        if self.size[pv] > self.size[pu]:
            pu, pv = pv, pu
        self.parent[pv] = pu
        self.size[pu] += self.size[pv]
        return True

    def getSize(self, node):
        return self.size[self.find(node)]


class Solution:
    def findMaximums(self, nums: List[int]) -> List[int]:
        n = len(nums)
        order = sorted(range(n), key=lambda i: -nums[i])
        dsu = DSU(n)
        active = [False] * n
        res = [0] * n
        filled = 0
        for i in order:
            active[i] = True
            if i > 0 and active[i - 1]:
                dsu.union(i, i - 1)
            if i < n - 1 and active[i + 1]:
                dsu.union(i, i + 1)
            size = dsu.getSize(i)
            while filled < size:
                res[filled] = nums[i]
                filled += 1
        return res

Time & Space Complexity

  • Time complexity: O(nlog⁡n)O(n \log n)
  • Space complexity: O(n)O(n)

Where nn is the length of the array numsnums.


4. Monotonic Stack

class Solution:
    def findMaximums(self, nums: List[int]) -> List[int]:
        n = len(nums)
        left, right = [-1] * n, [n] * n
        stack = []
        for i in range(n):
            while stack and nums[stack[-1]] >= nums[i]:
                stack.pop()
            if stack:
                left[i] = stack[-1]
            stack.append(i)

        stack = []
        for i in range(n - 1, -1, -1):
            while stack and nums[stack[-1]] >= nums[i]:
                stack.pop()
            if stack:
                right[i] = stack[-1]
            stack.append(i)

        res = [0] * n
        for i in range(n):
            length = right[i] - left[i] - 1
            res[length - 1] = max(res[length - 1], nums[i])

        for i in range(n - 2, -1, -1):
            res[i] = max(res[i], res[i + 1])
        return res

Time & Space Complexity

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

Where nn is the length of the array numsnums.


5. Monotonic Stack (One Pass)

class Solution:
    def findMaximums(self, nums: List[int]) -> List[int]:
        n = len(nums)
        res = [0] * n
        stack = []
        for i in range(n + 1):
            while stack and (i == n or nums[stack[-1]] >= nums[i]):
                j = stack.pop()
                l = stack[-1] if stack else -1
                length = i - l - 1
                res[length - 1] = max(res[length - 1], nums[j])
            stack.append(i)

        for i in range(n - 2, -1, -1):
            res[i] = max(res[i], res[i + 1])
        return res

Time & Space Complexity

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

Where nn is the length of the array numsnums.