Leetcode Problem 2818. Apply Operations to Maximize Score
2818. Apply Operations to Maximize Score
Leetcode Solutions
Greedy Approach with Prime Score Calculation and Subarray Count
Calculate the prime score for each element in the array.
Use a monotonic stack to find the next greater element with a prime score for each element.
Calculate the number of subarrays where each element would be the maximum prime score element.
Sort the elements based on their value and prime score contribution.
Initialize the result as 1.
Iterate over the sorted elements and for each element:
a. Determine the minimum of k and the element's subarray count.
b. Multiply the result by the element's value raised to the power of the minimum value calculated above, modulo 10^9 + 7.
c. Decrement k by the minimum value calculated above.