Skip to content

Commit 5880c4d

Browse files
authored
Create 2818-apply-operations-to-maximize-score.js
1 parent c8c16d7 commit 5880c4d

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var maximumScore = function (nums, k) {
7+
const n = nums.length
8+
const factors = Array(n).fill(0) // To store prime factors count for each number
9+
const count = Array(n).fill(0) // To store the count of reachable elements for each element
10+
11+
// Calculate prime factors count for each number
12+
for (let i = 0; i < n; i++) {
13+
factors[i] = primeFactors(nums[i])
14+
}
15+
16+
const stack = [-1] // Monotonic stack to keep track of elements in decreasing order of factors
17+
let curr = 0
18+
19+
// Calculate the count of reachable elements for each element
20+
for (let i = 0; i < n; i++) {
21+
while (
22+
stack[stack.length - 1] !== -1 &&
23+
factors[stack[stack.length - 1]] < factors[i]
24+
) {
25+
curr = stack.pop()
26+
count[curr] = (curr - stack[stack.length - 1]) * (i - curr)
27+
}
28+
stack.push(i)
29+
}
30+
31+
// Process any remaining elements in the stack
32+
while (stack[stack.length - 1] !== -1) {
33+
curr = stack.pop()
34+
count[curr] = (curr - stack[stack.length - 1]) * (n - curr)
35+
}
36+
37+
// Create an array of pairs containing elements and their corresponding reachable count
38+
const pairs = Array(n)
39+
for (let i = 0; i < n; i++) {
40+
pairs[i] = [nums[i], count[i]]
41+
}
42+
43+
// Sort the pairs in descending order of elements
44+
pairs.sort((a, b) => b[0] - a[0])
45+
46+
let res = BigInt(1)
47+
const mod = BigInt(1e9 + 7)
48+
49+
// Calculate the maximum score using modPow and available moves
50+
for (let i = 0; i < pairs.length && k > 0; i++) {
51+
const curr = Math.min(pairs[i][1], k)
52+
res = (res * modPow(BigInt(pairs[i][0]), BigInt(curr), mod)) % mod
53+
k -= curr
54+
}
55+
56+
return Number(res) // Convert the result to a regular number before returning
57+
}
58+
59+
/**
60+
* Function to calculate modular exponentiation.
61+
* @param {bigint} x - Base.
62+
* @param {bigint} y - Exponent.
63+
* @param {bigint} m - Modulus.
64+
* @return {bigint} - Result of modular exponentiation.
65+
*/
66+
function modPow(x, y, m) {
67+
if (y === 0n) {
68+
return 1n
69+
}
70+
let p = modPow(x, y / 2n, m) % m
71+
p = (p * p) % m
72+
if (y % 2n === 0n) {
73+
return p
74+
}
75+
return (p * x) % m
76+
}
77+
78+
/**
79+
* Function to calculate the count of prime factors for a number.
80+
* @param {number} num - Input number.
81+
* @return {number} - Count of prime factors for the input number.
82+
*/
83+
function primeFactors(num) {
84+
let count = 0
85+
let factor = 2
86+
const end = Math.sqrt(num)
87+
88+
while (num > 1 && factor <= end) {
89+
let inc = false
90+
while (num % factor === 0) {
91+
if (!inc) {
92+
count++
93+
inc = true
94+
}
95+
num /= factor
96+
}
97+
factor++
98+
}
99+
100+
if (num > 1) {
101+
count++
102+
}
103+
104+
return count
105+
}

0 commit comments

Comments
 (0)