Javatpoint Logo
Javatpoint Logo

Find the tasks completed by soldiers based on their ranks


Effective resource allocation is essential for optimizing task assignments in order to maximize productivity. A strategic approach is needed in situations where soldiers are assigned according to their ranks and tasks enter a system at different times. The objective is to optimize the process of task assignment given an array comprising the ranks of the soldiers and an array that represents the time needed for each task. Soldiers must be assigned tasks according to their rank; ties must be broken by choosing the soldier with the lowest index. Returning an array with the index of soldiers allocated to each task is the goal.

Overview of the Approach

The issue can be effectively resolved by keeping the ranks and indices of the soldiers in a priority queue, or min-heap. To further track the time at which each soldier becomes available, an array is used. The algorithm repeatedly runs through the tasks, determining at each time step whether soldiers are available and allocating tasks accordingly.



Find the tasks completed by soldiers based on their ranks

Code Explanation

Class Definition

  • TaskAssignmentOptimizer is the class name defined in the code.

Method Signature

  • There is a public static method in the class called assignTasks. It takes two parameters: a list of integers that represent the ranks of the soldiers (ranks) and a list of integers that represent the amount of time needed to complete each task (tasks).
  • A list of integers indicating the index of soldiers assigned to each task is returned by the method.

Initialization of the Priority Queue

  • Soldiers are arranged in a PriorityQueue called pq according to their ranks. Both rank and the original index determine the priority.
  • Soldiers with lower ranks are given preference according to the comparator used in the priority queue. Should there be a tie in ranks, priority is given to the soldier with the lower index.

Initialization of Data Structures

  • The allocated tasks are initially stored in an ArrayList called ans.
  • The initialization of a hash map called free is done in order to track the free soldiers at various times.
  • To track the current time, an integer variable called a timer is initialized.

Initializing Priority Queue with Soldiers

  • The code adds each soldier to the priority queue (pq) together with its rank and index after iterating over the soldier ranks (ranks).

Main Loop for Task Assignment

  • To iterate through the tasks, the code goes into a loop.
  • Soldiers who have finished their tasks at this point are released inside the loop. The soldiers are returned to the priority queue after being removed from the free map.
  • Next, the entries for the current time (timer) are removed from the free map.

Verifying the Availability of Soldiers

  • If there are no soldiers available at the moment, it indicates that the priority queue (pq) is empty.
  • In these situations, the code checks the keys in the free map to determine when a soldier first becomes free, then modifies the timer accordingly. In order to reassess the current task, it also decreases the loop index (i).

Task Assignment

  • The code pops the top soldier and gives that soldier the associated task if soldiers are available in the priority queue.
  • The ans list is updated with the assigned soldier's index.
  • Once the soldier has finished the current task, the free map is updated to show the soldier's next free time.
  • To proceed to the next time step, the timer is set back one.

Time and Space Complexity Analysis

Time Complexity

The operations within the main loop take either constant time (for priority queue operations) or logarithmic time (for each task iteration). Consequently, O(N log N), where N is the ranks array's size, represents the overall time complexity.

Space Complexity

The tasks array's size is M, and the ranks array's size is N. Therefore, the space complexity is O(M + N). This takes into consideration the amount of space needed for data structures such as the free map, priority queue, and extra variables.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA