Approach
Breadth-first search
For Process Tasks Using Servers, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 47 lines of Java from the credited upstream file 1882.java.
- The implementation visibly relies on sequence storage, work queue.
- 3 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class T {2 public int weight;3 public int index;4 public int freeTime;5 public T(int weight, int index, int freeTime) {6 this.weight = weight;7 this.index = index;8 this.freeTime = freeTime;9 }10}11 12class Solution {13 public int[] assignTasks(int[] servers, int[] tasks) {14 final int n = servers.length;15 final int m = tasks.length;16 int[] ans = new int[m];17 Queue<T> free = new PriorityQueue<>(18 Comparator.comparing((T t) -> t.weight).thenComparing((T t) -> t.index));19 Queue<T> used = new PriorityQueue<>(Comparator.comparing((T t) -> t.freeTime)20 .thenComparing((T t) -> t.weight)21 .thenComparing((T t) -> t.index));22 23 for (int i = 0; i < n; ++i)24 free.offer(new T(servers[i], i, 0));25 26 for (int i = 0; i < m; ++i) { 27 final int executionTime = tasks[i];28 29 while (!used.isEmpty() && used.peek().freeTime <= i)30 free.offer(used.poll());31 if (free.isEmpty()) {32 T server = used.poll();33 ans[i] = server.index;34 server.freeTime += executionTime;35 used.offer(server);36 } else {37 T server = free.poll();38 ans[i] = server.index;39 server.freeTime = i + executionTime;40 used.offer(server);41 }42 }43 44 return ans;45 }46}47