A warehouse has a circular conveyor belt with n slots numbered 0 to n - 1. Each slot either holds one package or is empty. Because the belt is circular, slot n - 1 is next to slot 0.
In one swap, you can pick any two different slots and exchange their contents. The two slots do not need to be next to each other.
Return the minimum number of swaps needed to group all packages together, so that they occupy consecutive slots on the belt. The group may wrap around from slot n - 1 to slot 0. If the belt has no packages, return 0.
int minSwapsToGroupPackages(List<Integer> belt)
belt[i] == 1 means slot i holds a package.belt[i] == 0 means slot i is empty.1 ≤ belt.size() ≤ 100,000belt[i] is either 0 or 1.minSwapsToGroupPackages(belt = [1, 0, 1, 0, 0, 1, 0])
Output: 1
Explanation:
0, 2 and 5.1 (empty) with slot 5 (package). The belt becomes [1, 1, 1, 0, 0, 0, 0].minSwapsToGroupPackages(belt = [1, 1, 0, 0, 0, 1])
Output: 0
Explanation:
5, 0 and 1.5 is next to slot 0 on the circular belt, so these 3 slots are already consecutive.minSwapsToGroupPackages(belt = [0, 1, 0, 1, 1, 0, 1, 0, 0, 1])
Output: 2
Explanation:
1, 3, 4, 6 and 9.1 with slot 2. The belt becomes [0, 0, 1, 1, 1, 0, 1, 0, 0, 1].9 with slot 5. The belt becomes [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], and all packages are in slots 2 to 6.minSwapsToGroupPackages(belt = [0, 0, 0])
Output: 0
Explanation: