Apply Operations to Maximize Score - Problem
Apply Operations to Maximize Score

Imagine you're a treasure hunter with a special scoring system! You have an array of positive integers and can perform at most k operations to maximize your score, starting from 1.

๐ŸŽฏ Your Mission:
โ€ข Choose any subarray that you haven't used before
โ€ข Find the element with the highest prime score (most distinct prime factors)
โ€ข If there's a tie, pick the one with the smallest index
โ€ข Multiply your current score by this chosen element

๐Ÿ“Š Prime Score Explained:
The prime score of a number equals its count of distinct prime factors:
โ€ข 300 = 2ยฒ ร— 3 ร— 5ยฒ โ†’ Prime score = 3 (factors: 2, 3, 5)
โ€ข 12 = 2ยฒ ร— 3 โ†’ Prime score = 2 (factors: 2, 3)

Return the maximum possible score modulo 10โน + 7.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2, 3, 5], k = 2
โ€บ Output: 15
๐Ÿ’ก Note: Prime scores: 2โ†’1, 3โ†’1, 5โ†’1. All elements have equal prime scores, so we pick by smallest index. We can choose subarrays [5] and [3] to get score 1 ร— 5 ร— 3 = 15.
example_2.py โ€” Different Prime Scores
$ Input: nums = [6, 10, 15], k = 2
โ€บ Output: 150
๐Ÿ’ก Note: Prime scores: 6โ†’2 (2ร—3), 10โ†’2 (2ร—5), 15โ†’2 (3ร—5). All have prime score 2, pick by index. Choose [6] and [10]: 1 ร— 6 ร— 10 = 60... Actually, optimal is [15] and [10]: 1 ร— 15 ร— 10 = 150.
example_3.py โ€” Single Element
$ Input: nums = [12], k = 1
โ€บ Output: 12
๐Ÿ’ก Note: Only one element available. Prime score of 12 = 2 (factors: 2, 3). Choose subarray [12], result = 1 ร— 12 = 12.

Visualization

Tap to expand
Championship Team Selection Strategy2Skill: 13Skill: 15Skill: 1๐ŸŽฏ Formation Analysis:Player 2 dominates: 3 formations | Player 3 dominates: 4 formations | Player 5 dominates: 3 formationsโšก Greedy Draft Strategy:1st PickPlayer 52nd PickPlayer 3AvailablePlayer 2๐Ÿ† Final Team Score:1 ร— 5 ร— 3 = 15โœจ Key Insight: Use monotonic stacks to efficiently find each player'sdominance range, then greedily select the highest-value players!Time Complexity: O(n log n) | Space Complexity: O(n)
Understanding the Visualization
1
Scout Players
Calculate each player's skill rating (prime score)
2
Find Territories
Determine which formations each player can dominate
3
Calculate Impact
Count total formations where each player would be chosen
4
Draft Strategy
Greedily select highest-value players first
Key Takeaway
๐ŸŽฏ Key Insight: By precomputing each element's contribution range using monotonic stacks, we can transform this complex problem into a simple greedy selection of the highest-value elements!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

O(n) for monotonic stack computation + O(n log n) for sorting the candidates

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for stacks, prime scores, and candidate list

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 105
  • 1 โ‰ค k โ‰ค min(nums.length * (nums.length + 1) / 2, 109)
  • All elements are positive integers
  • Answer fits in 32-bit integer after modulo
Asked in
Google 42 Meta 38 Amazon 31 Microsoft 24
28.6K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen