#947
Most Stones Removed with Same Row or Column
MediumApproaches
💡
Intuition
Time O(n²)Space O(1)
The brute force approach involves checking every stone and attempting to remove it by checking if it shares a row or column with any other stone. This method is straightforward but inefficient as it requires comparing each stone with every other stone.
⚙️
Algorithm
4 steps- 1Step 1: Initialize a counter for removed stones.
- 2Step 2: For each stone, check if there is another stone in the same row or column.
- 3Step 3: If a stone can be removed, increment the counter and mark it as removed.
- 4Step 4: Repeat until no more stones can be removed.
solution.py11 lines
1# Full working Python code
2class Solution:
3 def removeStones(self, stones):
4 removed = 0
5 n = len(stones)
6 for i in range(n):
7 for j in range(n):
8 if i != j and (stones[i][0] == stones[j][0] or stones[i][1] == stones[j][1]):
9 removed += 1
10 break
11 return removedℹ
Complexity note: This complexity arises because we are comparing each stone with every other stone, leading to a quadratic number of comparisons.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.