diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/4103. Remove Elements by Type and Color.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/4103. Remove Elements by Type and Color.java" new file mode 100644 index 00000000..da2ea62a --- /dev/null +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/4103. Remove Elements by Type and Color.java" @@ -0,0 +1,23 @@ +class Solution { + public boolean canRemoveAll(int[] types, int[] colors, int[] groupSize) { + Deque dq = new ArrayDeque<>(); + + for (int i = 0; i < types.length; i++) { + int t = types[i]; + int c = colors[i]; + + if (groupSize[t] == 1) continue; + + if (!dq.isEmpty() && dq.peekLast()[0] == t && dq.peekLast()[1] == c) { + int[] top = dq.pollLast(); + int nextCnt = top[2] + 1; + + if (nextCnt < groupSize[t]) dq.addLast(new int[]{t, c, nextCnt}); + } else { + dq.addLast(new int[]{t, c, 1}); + } + } + + return dq.isEmpty(); + } +} \ No newline at end of file