ShareChat Coding Question – Solved

5 Live
Break The Bricks There are n bricks arranged in a row at positions numbered from 1 through n, inclusive. There is an array, newtons[n], that contains an integer indicating the number of newtons required to smash a brick., (A newton is a unit of force.) There are two hammers, one big and one small. The big hammer can smash any brick with one blow. The small hammer reduces the newtons required by 1 for each blow to a brick. For example, a brick requires 3 newtons of force. It will take 1 blow with the big hammer, or 3 blows with the small hammer to smash it. There is a limit to how many times the big hammer can be used. Determine 3 values: 1. the minimum number of blows to smash all the bricks 2. the 1-based indices of the bricks smashed by the big hammer, sorted ascending 3. the 1-based indices of the bricks smashed by the small hammer, sorted ascending Return the values as a 2-dimensional integer array, [[total hits], [big hammer hits], [small hammer hits]]. If a hammer is not used, its index array should be [-1]. Example bigHits = 0 newtons = [2] The big hammer cannot be used. The small hammer takes 2 blows to smash the single brick at index 1. The return array is [[2], [-1], [1]]. bigHits = 4 newtons = [3, 2, 5, 4, 6, 7, 9] In this case, it is best to use the big hammer on bricks at sorted indices [3, 5, 6, 7], using 4 hits to smash them all. The small hammer is used on sorted indices [1, 2, 4] which have newtons of 3, 2, and 4. It takes a total of 3 + 2 + 4 = 9 hits with the small hammer. The total blows required = 4 + 9 = 13. The return array is [[13], [3, 5, 6, 7], [1, 2, Function Description Complete the function breakTheBricks in the editor below. breakTheBricks has the following parameters: int bigHits: the maximum blows with the big hammer int newtons[n]: an array of distinct integers representing newtons required to smash each brick

Asked in: ShareChat

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


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

🔒 Please login to view the solution

Explanation


```
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.
```


Related Questions