Skip to content

Commit 37b2941

Browse files
authored
Merge pull request #106 from BrianLusina/feat/maximal-score-after-k-ops
feat(heaps): maximal score after k operations
2 parents a92accd + 221cb26 commit 37b2941

22 files changed

+181
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@
519519
* Unique Number Of Occurrences
520520
* [Test Unique Occurrences](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/hashmap/unique_number_of_occurrences/test_unique_occurrences.py)
521521
* Heap
522+
* Maximal Score After K Operations
523+
* [Test Maximal Score](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/heap/maximal_score_after_k_operations/test_maximal_score.py)
522524
* Maximum Subsequence Score
523525
* [Test Max Subsequence Score](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/heap/maximum_subsequence_score/test_max_subsequence_score.py)
524526
* Min Cost Hire K Workers
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Maximal Score After Applying K Operations
2+
3+
You are given an array of integers nums and an integer k. You want to perform the following operation exactly k times:
4+
5+
You are given a 0-indexed array of integer nums and an integer k. Your task is to maximize a score through a series of
6+
operations. Initially, your score is set to 0
7+
8+
In each operation:
9+
10+
1. Select an index i (where 0 ≤ i <nums.length).
11+
2. Add the value of nums[i] to your score.
12+
3. Replace nums[i] with ceil(nums[i] / 3).
13+
14+
Repeat this process exactly k times and return the highest score you can achieve.
15+
16+
> The ceiling function `ceil(value)` is the least integer greater than or equal to `value`.
17+
18+
Constraints:
19+
20+
- 1 ≤ nums.length, k ≤ 10^3
21+
- 1 ≤ nums[i] ≤ 10^5
22+
23+
## Examples
24+
25+
Example 1:
26+
27+
![Example 1](images/examples/maximal_score_after_k_operations_example_1.png)
28+
![Example 2](images/examples/maximal_score_after_k_operations_example_2.png)
29+
![Example 3](images/examples/maximal_score_after_k_operations_example_3.png)
30+
31+
---
32+
33+
## Solution
34+
35+
This algorithm maximizes the score by iteratively selecting and reducing the largest elements from the array. It uses a
36+
max heap to ensure efficient access to the largest element. Over k iterations, the algorithm repeatedly extracts the
37+
largest value, adds it to the total score, reduces it by dividing it by 3 and rounding it up, and reinserts the reduced
38+
value into the heap.
39+
40+
The steps of the algorithm are as follows:
41+
42+
1. Create a max heap to store all elements of nums.
43+
2. Initialize a variable score to 0 to keep track of the accumulated score.
44+
3. Iterate for k steps, and in each iteration, perform the following steps:
45+
- Pop the largest value from the heap using heapq.heappop and store this value in a variable largest.
46+
- Add this value to the score.
47+
- Calculate the reduced value of the extracted element as math.ceil(largest / 3).
48+
- Push the reduced value back into the heap using heapq.heappush to maintain the max heap.
49+
4. After k iterations, return the accumulated score.
50+
51+
Let’s look at the following illustration to get a better understanding of the solution:
52+
53+
![Solution 1](images/solution/maximal_score_after_k_operations_solution_1.png)
54+
![Solution 2](images/solution/maximal_score_after_k_operations_solution_2.png)
55+
![Solution 3](images/solution/maximal_score_after_k_operations_solution_3.png)
56+
![Solution 4](images/solution/maximal_score_after_k_operations_solution_4.png)
57+
![Solution 5](images/solution/maximal_score_after_k_operations_solution_5.png)
58+
![Solution 6](images/solution/maximal_score_after_k_operations_solution_6.png)
59+
![Solution 7](images/solution/maximal_score_after_k_operations_solution_7.png)
60+
![Solution 8](images/solution/maximal_score_after_k_operations_solution_8.png)
61+
![Solution 9](images/solution/maximal_score_after_k_operations_solution_9.png)
62+
![Solution 10](images/solution/maximal_score_after_k_operations_solution_10.png)
63+
![Solution 11](images/solution/maximal_score_after_k_operations_solution_11.png)
64+
![Solution 12](images/solution/maximal_score_after_k_operations_solution_12.png)
65+
![Solution 13](images/solution/maximal_score_after_k_operations_solution_13.png)
66+
![Solution 14](images/solution/maximal_score_after_k_operations_solution_14.png)
67+
![Solution 15](images/solution/maximal_score_after_k_operations_solution_15.png)
68+
69+
### Time Complexity
70+
71+
In each iteration of the loop, pop from the heap takes O(log(n)), while pushing the updated value into the heap also
72+
takes O(logn). As the loop runs k times, the total time complexity for the loop is O(klogn).
73+
74+
### Space Complexity
75+
76+
The space complexity is O(n) because the heap stores n elements, where n is the number of elements in nums.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
from math import ceil
3+
import heapq
4+
5+
6+
def max_score(nums: List[int], k: int) -> int:
7+
score = 0
8+
9+
# if there are no elements, just return the score of 0
10+
if len(nums) == 0:
11+
return score
12+
13+
# a heapq provides a in-heap, so we'll have to use negative values to simulate a max-heap
14+
# Create a max-heap by negating all values (heapq is a min-heap)
15+
# This allows us to efficiently get the maximum element each time
16+
max_heap = [-num for num in nums]
17+
heapq.heapify(max_heap)
18+
19+
for _ in range(k):
20+
# Extract the maximum element (most negative in our negated heap)
21+
current_largest = -heapq.heappop(max_heap)
22+
score += current_largest
23+
reduced_value = ceil(current_largest / 3)
24+
heapq.heappush(max_heap, -reduced_value)
25+
26+
return score
41.6 KB
Loading
51.5 KB
Loading
62.9 KB
Loading
22.4 KB
Loading
31.7 KB
Loading
34.5 KB
Loading
31.9 KB
Loading

0 commit comments

Comments
 (0)