Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2818: Apply Operations to Maximize Score

In this guide, we solve Leetcode #2818 Apply Operations to Maximize Score in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

You are given an array nums of n positive integers and an integer k. Initially, you start with a score of 1.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Stack, Greedy, Array, Math, Number Theory, Sorting, Monotonic Stack

Intuition

A locally optimal choice leads to a globally optimal result for this structure.

That means we can commit to decisions as we scan without backtracking.

Approach

Sort or preprocess if needed, then repeatedly take the best available local choice.

Maintain the minimal state necessary to validate the greedy decision.

Steps:

  • Sort or preprocess as needed.
  • Iterate and pick the best local option.
  • Track the current solution.

Example

Input: nums = [8,3,9,3,8], k = 2 Output: 81 Explanation: To get a score of 81, we can apply the following operations: - Choose subarray nums[2, ..., 2]. nums[2] is the only element in this subarray. Hence, we multiply the score by nums[2]. The score becomes 1 * 9 = 9. - Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 1, but nums[2] has the smaller index. Hence, we multiply the score by nums[2]. The score becomes 9 * 9 = 81. It can be proven that 81 is the highest score one can obtain.

Python Solution

def primeFactors(n): i = 2 ans = set() while i * i <= n: while n % i == 0: ans.add(i) n //= i i += 1 if n > 1: ans.add(n) return len(ans) class Solution: def maximumScore(self, nums: List[int], k: int) -> int: mod = 10**9 + 7 arr = [(i, primeFactors(x), x) for i, x in enumerate(nums)] n = len(nums) left = [-1] * n right = [n] * n stk = [] for i, f, x in arr: while stk and stk[-1][0] < f: stk.pop() if stk: left[i] = stk[-1][1] stk.append((f, i)) stk = [] for i, f, x in arr[::-1]: while stk and stk[-1][0] <= f: stk.pop() if stk: right[i] = stk[-1][1] stk.append((f, i)) arr.sort(key=lambda x: -x[2]) ans = 1 for i, f, x in arr: l, r = left[i], right[i] cnt = (i - l) * (r - i) if cnt <= k: ans = ans * pow(x, cnt, mod) % mod k -= cnt else: ans = ans * pow(x, k, mod) % mod break return ans

Complexity

The time complexity is O(n×log⁡n)O(n \times \log n)O(n×logn), and the space complexity is O(n)O(n)O(n). The space complexity is O(n)O(n)O(n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy