Asked in: ShareChat
def breakTheBricks(bigHits, newtons):
newtons = [(i, idx+1) for idx,i in enumerate(newtons)]
newtons.sort(reverse=True)
first = []
// ... rest of solution available after purchase
```
To approach this problem, we must consider how to distribute the limited number of big hammer uses in a way that minimizes the total number of blows required to break all the bricks. Since the big hammer always takes exactly one blow to break any brick, and the small hammer takes a number of blows equal to the number of newtons a brick requires, the optimal use of the big hammer is to apply it to the bricks that would otherwise require the most effort with the small hammer.
Let’s first understand the setup. We are given a list of bricks, each with a specific number of newtons required to break it. We also have a constraint: a limited number of times we can use the big hammer. Each use of the big hammer counts as one blow, regardless of the brick’s strength. Conversely, the small hammer must apply a number of blows equal to the newton value of the brick, which makes it inefficient for stronger bricks.
The goal is threefold: calculate the minimum number of total blows needed, determine which bricks should be hit with the big hammer, and determine which should be hit with the small hammer. The result must be structured in a specific format: a list containing the total blows, a sorted list of indices of bricks hit with the big hammer, and a sorted list of indices of bricks hit with the small hammer.
To start thinking about a strategy, consider what would happen in an unrestricted scenario. If we could use the big hammer as many times as we want, we would use it on all bricks, resulting in exactly one blow per brick and a total of n blows. On the other hand, if we were only allowed to use the small hammer, the total number of blows would be the sum of all newton values in the array.
Since we’re constrained to a specific number of big hammer uses, we must select that many bricks for the big hammer in a way that minimizes the number of blows required for the rest using the small hammer. This naturally leads to a greedy approach. The bricks that take the most effort with the small hammer are the best candidates for the big hammer. Therefore, if we sort the bricks in descending order of newton values, and pick the top `bigHits` bricks for the big hammer, we will likely achieve the minimal number of total blows.
Once we’ve selected the bricks for the big hammer, the remaining bricks will be handled by the small hammer, contributing their newton values to the total blow count. The total number of blows is then the sum of:
1. The number of big hammer uses (each counts as one blow)
2. The sum of newton values for the bricks handled by the small hammer
From an implementation perspective, after deciding which bricks are best suited for the big hammer, we need to track their original indices to return the answer in the required format. This means maintaining a mapping between each brick’s value and its position in the original array. Care must be taken to ensure that the indices are adjusted to be 1-based and are sorted in ascending order for both hammers’ results.
Another detail is handling edge cases. For example, if the big hammer is not available (`bigHits` is zero), then all bricks must be handled by the small hammer, and we should return `-1` in place of the big hammer indices. Similarly, if all bricks are smashed with the big hammer, the small hammer result should be `-1`.
To summarize, the overall thought process includes:
1. Recognizing that using the big hammer on the bricks with the highest newton values reduces total blows most effectively.
2. Sorting the bricks by strength and selecting the strongest ones up to the bigHits limit.
3. Keeping track of original positions for both sets of bricks for proper result formatting.
4. Calculating the total number of blows by adding big hammer uses and the total newtons of remaining bricks.
5. Handling special cases where a hammer type is not used.
This approach provides a structured, logical path to minimize total effort and produce the result in the format specified.
```