Maximum Placement with Spacing Constraints
Greedy & IntervalsAuto-graded · 29 test casesLast updated
Tests greedy iteration with adjacency rules, requiring precise edge-case handling and index management.
You are given an integer array plots where each element is either 0 (vacant) or 1 (occupied), and an integer k representing the number of items you want to place.
Your goal is to determine whether you can place exactly k new items into the vacant plots while respecting the spacing rule: no two items may occupy adjacent positions.
An item may be placed in a vacant plot (value 0) only when both neighboring positions are also vacant or do not exist. Specifically:
- If the plot is at the left boundary, only the right neighbor must be vacant.
- If the plot is at the right boundary, only the left neighbor must be vacant.
- For interior plots, both neighbors must be vacant.
Return True if it is possible to place k items under these rules, otherwise return False.
Constraints
1 <= plots.length <= 2 * 10^4plots[i]is either0or10 <= k <= plots.length
Examples
Example 1
Input: can_place_items([1, 0, 0, 0, 1], 1)
Output: True
Example 2
Input: can_place_items([1, 0, 0, 0, 1], 2)
Output: False
Example 3
Input: can_place_items([0, 0, 0, 0, 0], 2)
Output: True
Example 4
Input: can_place_items([1], 0)
Output: True
Hints
Hint 1
Consider a greedy strategy: place an item whenever you encounter the first valid vacant plot from left to right.
Hint 2
After placing an item at position i, update the array so that position i is marked as occupied before checking subsequent positions.
Hint 3
Handle boundary positions carefully—the leftmost and rightmost plots have only one neighbor to check.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The optimal strategy is greedy: scan the array from left to right and place an item in the first valid vacant plot you encounter. A plot qualifies for placement when it is currently vacant and both its neighbors (or the single neighbor for boundary positions) are also vacant.
After placing an item at index i, mark that position as occupied before continuing. This ensures subsequent checks correctly recognize the newly placed item. Continue until you have placed k items or exhausted all positions.
Why Greedy Works
Placing items as early as possible never reduces the total capacity. If an optimal solution skips an eligible position i in favor of a later position j, you can always move that item from j to i without creating conflicts. The position i is valid (both neighbors are clear up to that point), and moving an item leftward cannot interfere with placements to its right. This exchange argument shows that some optimal solution matches the greedy choice at every step.
Additionally, for a contiguous segment of m vacant plots:
- Between two occupied plots: capacity is
(m - 1) // 2 - At one array boundary: capacity is
m // 2 - Spanning the entire array: capacity is
(m + 1) // 2
The greedy approach achieves exactly these theoretical maximums.
Complexity
- Time: O(n) where n is the length of
plots. Each position is visited once with constant-time checks. Early termination whenplaced >= koften improves practical performance. - Space: O(n) because we create a copy of the input array to avoid modifying the caller's data. (This can be reduced to O(1) by using index-jumping techniques that avoid writing back.)
Edge Cases
- When
k = 0, returnTrueimmediately regardless of array contents. - Single-element arrays:
[0]allows one placement;[1]allows none. - Boundary positions only check their single neighbor.
- Arrays with many consecutive vacancies follow the formulas above.
- The update
arr[i] = 1is critical; without it,[0, 0, 0]would incorrectly count three placements instead of two.
Solution
from typing import List
def can_place_items(plots: List[int], k: int) -> bool:
if k == 0:
return True
arr = list(plots)
placed = 0
for i in range(len(arr)):
if arr[i] == 0:
left_ok = i == 0 or arr[i - 1] == 0
right_ok = i == len(arr) - 1 or arr[i + 1] == 0
if left_ok and right_ok:
arr[i] = 1
placed += 1
if placed >= k:
return True
return False