Remove Elements by Type and Color Problem
Description
LeetCode Problem 4103.
You are given three integer arrays types, colors, and groupSize, where types and colors have length n, and groupSize has length m.
types[i]represents the type of the element at indexi.colors[i]represents the color of the element at indexi.groupSize[t]represents the required number of elements in a removable group of typet.
In one operation, you may remove exactly groupSize[t] consecutive elements if all of them have type t and the same color.
After each removal, the remaining elements become adjacent again while maintaining their relative order.
Return true if it is possible to remove all elements using a sequence of valid operations. Otherwise, return false.
Example 1:
Input: types = [0,1,1,0], colors = [5,3,3,5] , groupSize = [2,2]
Output: true
Explanation:
- First, remove the consecutive elements at indices 1 and 2. Both elements have type 1, color 3, and the group size is equal to groupSize[1] = 2.
- The remaining sequence is: types = [0, 0], colors = [5, 5]
- Now remove the two remaining elements. They have type 0, color 5, and the group size is equal to groupSize[0] = 2.
- All elements have been removed, so the answer is true.
Example 2:
Input: types = [0,1,0], colors = [9,9,9], groupSize = [3,2]
Output: false
Explanation:
- No valid operation can be performed because no consecutive group satisfies its required size.
- Therefore, it is impossible to remove all elements, so the answer is false.
Example 3:
Input: types = [2,0,1,2], colors = [7,5,4,7], groupSize = [3,1,2]
Output: false
Explanation:
- Remove the element at index 2. It has type 1, color 4, and forms a consecutive group of size groupSize[1] = 1.
- The remaining sequence is: types = [2, 0, 2], colors = [7, 5, 7].
- No further operations can be performed, so the answer is false.
Constraints:
1 <= n == types.length == colors.length <= 10^51 <= m == groupSize.length <= 10^50 <= types[i] <= m - 11 <= colors[i] <= 10^51 <= groupSize[t] <= n
Solution
Approach: Stack of Runs
Think of each element as a pair (type, color), and of groupSize[type] consecutive equal pairs as something that cancels. Removing a full group never blocks another removal, so the order of removals does not matter, and a greedy stack finds out whether everything disappears.
Keep a stack of runs (type, color, count). For each element:
- If it equals the top run, increase that run's count; otherwise push a new run with count 1.
- If the top run's count reaches
groupSize[type], pop it. The elements on both sides become neighbors, so the next element can continue the run underneath.
The answer is true when the stack is empty at the end. A run never has more than groupSize elements, because it is removed the moment it is full.
Complexity: O(n) time and O(n) space.
class Solution:
def canRemoveAll(self, types: List[int], colors: List[int], groupSize: List[int]) -> bool:
stack = [] # [type, color, count] for each run of equal elements
for t, c in zip(types, colors):
if stack and stack[-1][0] == t and stack[-1][1] == c:
stack[-1][2] += 1
else:
stack.append([t, c, 1])
if stack[-1][2] == groupSize[t]:
stack.pop() # the run is a full group: remove it
return not stack