Skip to content

Commit 19e1b09

Browse files
committedMar 29, 2025
Add solution #2818
1 parent 5f466c0 commit 19e1b09

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,050 LeetCode solutions in JavaScript
1+
# 1,051 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -1031,6 +1031,7 @@
10311031
2726|[Calculator with Method Chaining](./solutions/2726-calculator-with-method-chaining.js)|Easy|
10321032
2727|[Is Object Empty](./solutions/2727-is-object-empty.js)|Easy|
10331033
2780|[Minimum Index of a Valid Split](./solutions/2780-minimum-index-of-a-valid-split.js)|Medium|
1034+
2818|[Apply Operations to Maximize Score](./solutions/2818-apply-operations-to-maximize-score.js)|Hard|
10341035
2948|[Make Lexicographically Smallest Array by Swapping Elements](./solutions/2948-make-lexicographically-smallest-array-by-swapping-elements.js)|Medium|
10351036
2965|[Find Missing and Repeated Values](./solutions/2965-find-missing-and-repeated-values.js)|Easy|
10361037
3042|[Count Prefix and Suffix Pairs I](./solutions/3042-count-prefix-and-suffix-pairs-i.js)|Easy|
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)
Please sign in to comment.