|
| 1 | +/** |
| 2 | + * 2818. Apply Operations to Maximize Score |
| 3 | + * https://leetcode.com/problems/apply-operations-to-maximize-score/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You are given an array nums of n positive integers and an integer k. |
| 7 | + * |
| 8 | + * Initially, you start with a score of 1. You have to maximize your score by applying the following |
| 9 | + * operation at most k times: |
| 10 | + * - Choose any non-empty subarray nums[l, ..., r] that you haven't chosen previously. |
| 11 | + * - Choose an element x of nums[l, ..., r] with the highest prime score. If multiple such elements |
| 12 | + * exist, choose the one with the smallest index. |
| 13 | + * - Multiply your score by x. |
| 14 | + * |
| 15 | + * Here, nums[l, ..., r] denotes the subarray of nums starting at index l and ending at the index |
| 16 | + * r, both ends being inclusive. |
| 17 | + * |
| 18 | + * The prime score of an integer x is equal to the number of distinct prime factors of x. For |
| 19 | + * example, the prime score of 300 is 3 since 300 = 2 * 2 * 3 * 5 * 5. |
| 20 | + * |
| 21 | + * Return the maximum possible score after applying at most k operations. |
| 22 | + * |
| 23 | + * Since the answer may be large, return it modulo 109 + 7. |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {number[]} nums |
| 28 | + * @param {number} k |
| 29 | + * @return {number} |
| 30 | + */ |
| 31 | +var maximumScore = function(nums, k) { |
| 32 | + const MOD = 1000000007n; |
| 33 | + const MAX = 100000; |
| 34 | + const powerCache = {}; |
| 35 | + |
| 36 | + const primeScores = new Array(MAX + 1).fill(0); |
| 37 | + for (let prime = 2; prime <= MAX; prime++) { |
| 38 | + if (primeScores[prime] > 0) continue; |
| 39 | + for (let multiple = prime; multiple <= MAX; multiple += prime) { |
| 40 | + primeScores[multiple]++; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + const elements = nums.map(num => [num, primeScores[num], 1]); |
| 45 | + |
| 46 | + const stack = [-1]; |
| 47 | + const n = elements.length; |
| 48 | + |
| 49 | + for (let i = 0; i <= n; i++) { |
| 50 | + while ( |
| 51 | + stack.length > 1 && (i === n || elements[stack[stack.length - 1]][1] < elements[i][1]) |
| 52 | + ) { |
| 53 | + const current = stack.pop(); |
| 54 | + elements[current][2] = (i - current) * (current - stack[stack.length - 1]); |
| 55 | + } |
| 56 | + |
| 57 | + stack.push(i); |
| 58 | + } |
| 59 | + |
| 60 | + elements.sort((a, b) => b[0] - a[0]); |
| 61 | + |
| 62 | + let result = 1n; |
| 63 | + let remainingOps = k; |
| 64 | + |
| 65 | + for (let i = 0; remainingOps > 0; i++) { |
| 66 | + const usedOps = Math.min(remainingOps, elements[i][2]); |
| 67 | + remainingOps -= usedOps; |
| 68 | + |
| 69 | + const cacheKey = `${elements[i][0]},${usedOps}`; |
| 70 | + let power; |
| 71 | + |
| 72 | + if (cacheKey in powerCache) { |
| 73 | + power = powerCache[cacheKey]; |
| 74 | + } else { |
| 75 | + let base = BigInt(elements[i][0]); |
| 76 | + let exp = usedOps; |
| 77 | + power = 1n; |
| 78 | + |
| 79 | + while (exp > 0) { |
| 80 | + if (exp & 1) { |
| 81 | + power = (power * base) % MOD; |
| 82 | + } |
| 83 | + exp >>= 1; |
| 84 | + base = (base * base) % MOD; |
| 85 | + } |
| 86 | + |
| 87 | + powerCache[cacheKey] = power; |
| 88 | + } |
| 89 | + |
| 90 | + result = (result * power) % MOD; |
| 91 | + } |
| 92 | + |
| 93 | + return Number(result); |
| 94 | +}; |
0 commit comments