Similar Problems

Similar Problems not available

Minimum Time To Kill All Monsters - Leetcode Solution

Companies:

LeetCode:  Minimum Time To Kill All Monsters Leetcode Solution

Difficulty: Hard

Topics: dynamic-programming bit-manipulation array  

Problem Statement:

You are playing a game in which you are a hero. There are n monsters on a straight line, where the ith monster is at position monsters[i] on the line. Each monster has a power p[i]. You have a weapon that can kill a monster with power x (i.e., x ≥ p[i]) in one second.

If you have no monster to fight, you win. If you have any monster to fight, you cannot win until all the monsters are dead. You can only have one fight at a time, so you have to decide the order of the fights.

You want to kill all the monsters in the minimum total time.

Determine the minimum total time to kill all the monsters.

Solution:

Approach:

The problem can be solved by using binary search. We can consider a time t and check if it is possible to kill all the monsters within this time. We can check this by iterating through the monsters, and for each monster, we can calculate the time required to kill it. If at any point during the iteration, the total time required to kill all the monsters exceeds t, then it is not possible to kill all the monsters in time t.

To implement this approach, we first sort the array of monsters in increasing order of their positions. Then we use binary search to find the minimum time required to kill all the monsters. We set the lower bound of the binary search as 0, and the upper bound as the maximum power of the monsters.

For each mid in the binary search, we check if it is possible to kill all the monsters in time mid. To do this, we iterate over the array of monsters and calculate the time required to kill each monster. We keep a track of the total time required to kill all the monsters. If at any point, the total time exceeds mid, we break out of the loop, and set the flag to False, indicating that it is not possible to kill all the monsters in time mid. Otherwise, we continue with the iteration and set the flag to True.

At the end of each iteration, if the flag is True, it means that it is possible to kill all the monsters in time mid. We update the minimum time variable to mid and set the upper bound of the binary search as mid - 1. Otherwise, if the flag is False, we set the lower bound of the binary search as mid + 1.

Implementation:

Here is the implementation of the above-described algorithm in Python:

def min_time_to_kill_monsters(monsters, power): n = len(monsters) monsters_power = [(monsters[i], power[i]) for i in range(n)] monsters_power.sort() # initial lower bound lo = 0 # initial upper bound hi = max(power) # minimum time to kill all the monsters ans = float('inf')

# binary search for the minimum time
while lo <= hi:
    mid = (lo + hi) // 2
    total_time = 0
    flag = True
    # iterate over the monsters
    for i in range(n):
        # calculate the time required to kill the current monster
        time = (monsters_power[i][1] + mid - 1) // mid
        # add the time to the total time
        total_time += time
        # if the total time exceeds mid, break out of the loop
        if total_time > mid:
            flag = False
            break
    # check the flag
    if flag:
        ans = mid
        hi = mid - 1
    else:
        lo = mid + 1
return ans

Time Complexity:

The time complexity of the above algorithm is O(n log p), where n is the number of monsters, and p is the maximum power of the monsters. The algorithm performs binary search in log p steps. For each mid value, the algorithm iterates over all the n monsters, and calculates the time required to kill each monster, which takes O(n) time. Therefore, the overall time complexity is O(n log p).

Minimum Time To Kill All Monsters Solution Code

1