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
๐ฏ 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.
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
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
โก Linearithmic
Space Complexity
O(n)
Space for stacks, prime scores, and candidate list
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code