DESHAW Coding Question – Solved

7 Live
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

Image of the Question

Question Image Question Image Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| A digital art gallery has an art collection which is represented as a binary st… |
| Given a series of integer intervals, determine the size of the smallest set tha… |
| A forklift operator navigates products within an automotive parts warehouse. Th… |
| School renovation Given N classrooms in a school and each classroom has a ca… |
| Number in a range You are given three integers L, R, and K. A number X repre… |
| You are given a board of size M Γ— N where each cell can be either empty ('O') o… |