DESHAW Coding Question β Solved
A company is organizing a batch process, where each batch consists of several task groups arranged in a line and numbered from left to right. Each group currently has a certain number of tasks assigned, represented by the array tasks[], where tasks[i] is the number of tasks in the i-th group.
To optimize the process, the company wants the number of tasks in each group to be non-increasing from left to right.
In one move, a single task can be shifted from any group to its immediate neighbor (either to the left or right). More formally, you can select a group i (tasks[i] > 0) and do one of the following:
- If i > 1, move one task from group i to group i-1 (the previous group). After this move, tasks[i] decreases by 1 and tasks[i-1] increases by 1.
- If i < n, move one task from group i to group i+1 (the next group). After this move, tasks[i] decreases by 1 and tasks[i+1] increases by 1.
Determine the minimum number of moves required to rearrange the tasks so that the array becomes non-increasing.
Example:
n = 6
tasks = [5, 3, 2, 3, 3, 3]
In this example, you first need to move one task from group 1 to group 2, resulting in [4, 4, 2, 3, 3, 3]. After that, you need to move one task from group 2 to group 3, and the array will become non-increasing tasks = [4, 3, 3, 3, 3, 3].
Hence, the answer is 2.
Function Description:
Complete the function getMinimumMoves in the editor below.
getMinimumMoves has the following parameters:
int tasks[n]: the number of tasks in each group.
Returns:
int: the minimum number of moves required.
Constraints:
- 1 β€ n β€ 250
- 0 β€ tasks[i] β€ 250
- It is guaranteed that the total number of tasks across all groups will not exceed 250.
Input Format For Custom Testing
Sample Case 0
Sample Input For Custom Testing
STDIN:
tasks = [3, 4, 3, 3, 5, 1]
Asked in:
DESHAW