|
| 1 | +# Approach 1: Monotonic Stack & Priority Queue |
| 2 | + |
| 3 | +class Solution: |
| 4 | + MOD = 10**9 + 7 |
| 5 | + |
| 6 | + def maximumScore(self, nums, k): |
| 7 | + n = len(nums) |
| 8 | + prime_scores = [0] * n |
| 9 | + |
| 10 | + # Calculate the prime score for each number in nums |
| 11 | + for index in range(n): |
| 12 | + num = nums[index] |
| 13 | + |
| 14 | + # Check for prime factors from 2 to sqrt(n) |
| 15 | + for factor in range(2, int(math.sqrt(num)) + 1): |
| 16 | + if num % factor == 0: |
| 17 | + # Increment prime score for each prime factor |
| 18 | + prime_scores[index] += 1 |
| 19 | + |
| 20 | + # Remove all occurrences of the prime factor from num |
| 21 | + while num % factor == 0: |
| 22 | + num //= factor |
| 23 | + |
| 24 | + # If num is still greater than or equal to 2, it's a prime factor |
| 25 | + if num >= 2: |
| 26 | + prime_scores[index] += 1 |
| 27 | + |
| 28 | + # Initialize next and previous dominant index arrays |
| 29 | + next_dominant = [n] * n |
| 30 | + prev_dominant = [-1] * n |
| 31 | + |
| 32 | + # Stack to store indices for monotonic decreasing prime score |
| 33 | + decreasing_prime_score_stack = [] |
| 34 | + |
| 35 | + # Calculate the next and previous dominant indices for each number |
| 36 | + for index in range(n): |
| 37 | + # While the stack is not empty and the current prime score is greater than the stack's top |
| 38 | + while ( |
| 39 | + decreasing_prime_score_stack |
| 40 | + and prime_scores[decreasing_prime_score_stack[-1]] |
| 41 | + < prime_scores[index] |
| 42 | + ): |
| 43 | + top_index = decreasing_prime_score_stack.pop() |
| 44 | + |
| 45 | + # Set the next dominant element for the popped index |
| 46 | + next_dominant[top_index] = index |
| 47 | + |
| 48 | + # If the stack is not empty, set the previous dominant element for the current index |
| 49 | + if decreasing_prime_score_stack: |
| 50 | + prev_dominant[index] = decreasing_prime_score_stack[-1] |
| 51 | + |
| 52 | + # Push the current index onto the stack |
| 53 | + decreasing_prime_score_stack.append(index) |
| 54 | + |
| 55 | + # Calculate the number of subarrays in which each element is dominant |
| 56 | + num_of_subarrays = [0] * n |
| 57 | + for index in range(n): |
| 58 | + num_of_subarrays[index] = (next_dominant[index] - index) * ( |
| 59 | + index - prev_dominant[index] |
| 60 | + ) |
| 61 | + |
| 62 | + # Priority queue to process elements in decreasing order of their value |
| 63 | + processing_queue = [] |
| 64 | + |
| 65 | + # Push each number and its index onto the priority queue |
| 66 | + for index in range(n): |
| 67 | + heapq.heappush(processing_queue, (-nums[index], index)) |
| 68 | + |
| 69 | + score = 1 |
| 70 | + |
| 71 | + # Helper function to compute the power of a number modulo MOD |
| 72 | + def _power(base, exponent): |
| 73 | + res = 1 |
| 74 | + |
| 75 | + # Calculate the exponentiation using binary exponentiation |
| 76 | + while exponent > 0: |
| 77 | + # If the exponent is odd, multiply the result by the base |
| 78 | + if exponent % 2 == 1: |
| 79 | + res = (res * base) % self.MOD |
| 80 | + |
| 81 | + # Square the base and halve the exponent |
| 82 | + base = (base * base) % self.MOD |
| 83 | + exponent //= 2 |
| 84 | + |
| 85 | + return res |
| 86 | + |
| 87 | + # Process elements while there are operations left |
| 88 | + while k > 0: |
| 89 | + # Get the element with the maximum value from the queue |
| 90 | + num, index = heapq.heappop(processing_queue) |
| 91 | + num = -num # Negate back to positive |
| 92 | + |
| 93 | + # Calculate the number of operations to apply on the current element |
| 94 | + operations = min(k, num_of_subarrays[index]) |
| 95 | + |
| 96 | + # Update the score by raising the element to the power of operations |
| 97 | + score = (score * _power(num, operations)) % self.MOD |
| 98 | + |
| 99 | + # Reduce the remaining operations count |
| 100 | + k -= operations |
| 101 | + |
| 102 | + return score |
0 commit comments