8000 Create Apply Operations to Maximize Score.py · erjan/coding_exercises@410cb0f · GitHub
[go: up one dir, main page]

Skip to content

Commit 410cb0f

Browse files
authored
Create Apply Operations to Maximize Score.py
1 parent fdc354f commit 410cb0f

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

Apply Operations to Maximize Score.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
'''
2+
You are given an array nums of n positive integers and an integer k.
3+
4+
Initially, you start with a score of 1. You have to maximize your score by applying the following operation at most k times:
5+
6+
Choose any non-empty subarray nums[l, ..., r] that you haven't chosen previously.
7+
Choose an element x of nums[l, ..., r] with the highest prime score. If multiple such elements exist, choose the one with the smallest index.
8+
Multiply your score by x.
9+
Here, nums[l, ..., r] denotes the subarray of nums starting at index l and ending at the index r, both ends being inclusive.
10+
11+
The prime score of an integer x is equal to the number of distinct prime factors of x. For example, the prime score of 300 is 3 since 300 = 2 * 2 * 3 * 5 * 5.
12+
13+
Return the maximum possible score after applying at most k operations.
14+
15+
Since the answer may be large, return it modulo 109 + 7.
16+
'''
17+
18+
#just sketch
19+
class Solution:
20+
def maximumScore(self, nums: List[int], k: int) -> int:
21+
n = len(nums)
22+
23+
res = 1
24+
25+
#def prime_check()....
26+
27+
for i in range(k):
28+
29+
l = 0
30+
31+
r = n-1
32+
33+
while l<=r:
34+
sub = nums[l:r]
35+
36+
l+=1
37+
r-=1
38+
39+
for val in sub:
40+
temp= prime_check(val):
41+
if temp:
42+
res = max(res, temp)
43+
return res
44+
45+
--------------------------------------------
46+
47+
class Solution:
48+
MOD = 10**9 + 7
49+
50+
def maximumScore(self, nums, k):
51+
n = len(nums)
52+
prime_scores = [0] * n
53+
54+
# Calculate the prime score for each number in nums
55+
for index in range(n):
56+
num = nums[index]
57+
58+
# Check for prime factors from 2 to sqrt(n)
59+
for factor in range(2, int(math.sqrt(num)) + 1):
60+
if num % factor == 0:
61+
# Increment prime score for each prime factor
62+
prime_scores[index] += 1
63+
64+
# Remove all occurrences of the prime factor from num
65+
while num % factor == 0:
66+
num //= factor
67+
68+
# If num is still greater than or equal to 2, it's a prime factor
69+
if num >= 2:
70+
prime_scores[index] += 1
71+
72+
# Initialize next and previous dominant index arrays
73+
next_dominant = [n] * n
74+
prev_dominant = [-1] * n
75+
76+
# Stack to store indices for monotonic decreasing prime score
77+
decreasing_prime_score_stack = []
78+
79+
# Calculate the next and previous dominant indices for each number
80+
for index in range(n):
81+
# While the stack is not empty and the current prime score is greater than the stack's top
82+
while (
83+
decreasing_prime_score_stack
84+
and prime_scores[decreasing_prime_score_stack[-1]]
85+
< prime_scores[index]
86+
):
87+
top_index = decreasing_prime_score_stack.pop()
88+
89+
# Set the next dominant element for the popped index
90+
next_dominant[top_index] = index
91+
92+
# If the stack is not empty, set the previous dominant element for the current index
93+
if decreasing_prime_score_stack:
94+
prev_dominant[index] = decreasing_prime_score_stack[-1]
95+
96+
# Push the current index onto the stack
97+
decreasing_prime_score_stack.append(index)
98+
99+
# Calculate the number of subarrays in which each element is dominant
100+
num_of_subarrays = [0] * n
101+
for index in range(n):
102+
num_of_subarrays[index] = (next_dominant[index] - index) * (
103+
index - prev_dominant[index]
104+
)
105+
106+
# Priority queue to process elements in decreasing order of their value
107+
processing_queue = []
108+
109+
# Push each number and its index onto the priority queue
110+
for index in range(n):
111+
heapq.heappush(processing_queue, (-nums[index], index))
112+
113+
score = 1
114+
115+
# Helper function to compute the power of a number modulo MOD
116+
def _power(base, exponent):
117+
res = 1
118+
119+
# Calculate the exponentiation using binary exponentiation
120+
while exponent > 0:
121+
# If the exponent is odd, multiply the result by the base
122+
if exponent % 2 == 1:
123+
res = (res * base) % self.MOD
124+
125+
# Square the base and halve the exponent
126+
base = (base * base) % self.MOD
127+
exponent //= 2
128+
129+
return res
130+
131+
# Process elements while there are operations left
132+
while k > 0:
133+
# Get the element with the maximum value from the queue
134+
num, index = heapq.heappop(processing_queue)
135+
num = -num # Negate back to positive
136+
137+
# Calculate the number of operations to apply on the current element
138+
operations = min(k, num_of_subarrays[index])
139+
140+
# Update the score by raising the element to the power of operations
141+
score = (score * _power(num, operations)) % self.MOD
142+
143+
# Reduce the remaining operations count
144+
k -= operations
145+
146+
return score
147+

0 commit comments

Comments
 (0)
0