Problem Statement |
| You are given a board consisting of infinitely many squares in a row, numbered from left to right starting with 1. There are some rectangles laid out on top of the board, all of height 1 and width width. The left side of the i-th rectangle is lined up with the the left side of square left[i], so it exactly covers the area of the board between squares left[i] and left[i] + width - 1, inclusive.
You want to move the rectangles so that they exactly cover the area between squares a and b, inclusive, and none of the other squares. Moving one rectangle one square to the left or to the right counts as one move. Rectangles are allowed to overlap. Return the minimal number of moves required to achieve the goal, or -1 if it's not possible. |
|
Definition |
| Class: | RectanglesArrangement | Method: | cover | Parameters: | int[], int, int, int | Returns: | int | Method signature: | int cover(int[] left, int width, int a, int b) | (be sure your method is public) |
|
|
|
|
Constraints |
- | left will contain between 1 and 50 elements, inclusive. |
- | width will be between 1 and 1000, inclusive. |
- | Each element of left will be between 1 and 1000, inclusive. |
- | a will be between 1 and 1000, inclusive. |
- | b will be between a and 1000, inclusive. |
|
Examples |
0) | |
| | Returns: 8 | Rectangles have width 1, so they're actually squares. We want to cover only the fourth square of the board, so we should just move all our rectangles there. |
|
|
1) | |
| | Returns: 2 | We move one rectangle one field to the right, and the other one field to the left. |
|
|
2) | |
| | Returns: -1 | With these rectangles we can cover at most 9 squares. |
|
|
3) | |
| { 12, 43, 67, 86, 93 } | 12 | 4 | 28 |
| Returns: 230 | |
|