#3568

Minimum Moves to Clean the Classroom

Medium
ArrayHash TableBit ManipulationBreadth-First SearchMatrixHash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n*m*2^k)
Space
O(1)
O(n*m*2^k)
💡

Intuition

Time O(n*m*2^k)Space O(n*m*2^k)

Use BFS to explore the grid while tracking energy and collected litter efficiently. This approach minimizes redundant states and ensures we only explore viable paths.

⚙️

Algorithm

3 steps
  1. 1Step 1: Initialize BFS with the starting position, energy, and a bitmask for collected litter.
  2. 2Step 2: For each position, explore adjacent cells, updating energy and litter mask accordingly.
  3. 3Step 3: Use a 3D array to prune states with lower energy to avoid unnecessary exploration.
solution.py5 lines
1from collections import deque
2
3def minMoves(grid, energy):
4    # BFS implementation omitted for brevity
5    pass

Complexity note: The complexity accounts for exploring each cell and all combinations of collected litter (k is the number of litter pieces).

  • 1BFS is effective for shortest path problems in grids.
  • 2Using bitmasking helps efficiently track collected items.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.