Similar Problems

Similar Problems not available

Sum Of Total Strength Of Wizards - Leetcode Solution

Companies:

  • amazon

LeetCode:  Sum Of Total Strength Of Wizards Leetcode Solution

Difficulty: Hard

Topics: stack prefix-sum array  

Problem Statement:

You are given an integer array, representing the strengths of N wizards.

You have to perform a total of K operations.

In each operation, you can pick any two wizards and combine their strength. The cost of this operation is equal to the sum of strengths of the two wizards being combined.

You can perform the same operation multiple times, and combine the same two wizards multiple times.

You need to minimize the total cost of all the operations given that you can only combine two wizards at a time and you can only perform K operations.

Return the minimum total cost.

Solution:

To solve this problem, we can use a greedy approach. We can sort the array of wizard strengths in increasing order, and then start combining the weakest wizards first.

For each operation, we can pick the two weakest wizards, combine their strength, and update the array to reflect their new strength. We can keep track of the total cost of all the operations in a variable, and return this variable after performing K operations.

Algorithm:

  1. Sort the array of wizard strengths in increasing order.
  2. For K times: a. Pick the two weakest wizards. b. Combine their strength and update the array to reflect their new strength. c. Add the cost of this operation to a variable that keeps track of the total cost.
  3. Return the total cost.

Code:

The following is the Python code implementation of the algorithm described above.

def sum_total_strength_of_wizards(n, k, strengths): strengths.sort() total_cost = 0 for i in range(k): a = strengths.pop(0) b = strengths.pop(0) new_strength = a + b strengths.append(new_strength) total_cost += new_strength strengths.sort() return total_cost

Explanation:

We first sort the array of wizard strengths in increasing order using the sort() method. We then loop through K times, where K is the total number of operations that we have to perform.

For each iteration of the loop, we pick the two weakest wizards by popping the first two elements from the sorted array. We then combine their strength to get the new strength and update the array to reflect this.

We calculate the cost of the operation as the sum of the strengths of the two wizards being combined. We add this cost to the total cost variable that keeps track of the total cost of all the operations done so far.

Finally, we sort the array again so that it remains in sorted order for the next iteration of the loop.

After the loop, we return the total cost variable as the answer to the problem.

Time Complexity:

The time complexity of this solution is O(KlogN), where N is the number of wizards and K is the number of operations to be performed. This is because we are sorting the array N times, which takes O(NlogN) time, and we are doing this K times, which takes O(KlogN) time.

Space Complexity:

The space complexity of this solution is O(N), where N is the number of wizards. This is because we are using an array of size N to store the strengths of the wizards.

Sum Of Total Strength Of Wizards Solution Code

1