699. Falling Squares
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to simulate dropping a series of squares onto a 2D plane. For each square, we need to determine its final resting height and then report the maximum height achieved by any stack of squares up to that point. A new square falls until it lands on the x-axis or on top of a previously placed square. The core of the problem is, for each new square, to find the height of the highest point on the ground beneath its horizontal span.
Solution
Falling Squares in Python
class Solution:
def fallingSquares(self, positions: List[List[int]]) -> List[int]:
# A list to store information about landed squares: (left, right, height)
intervals = []
ans = []
max_h = 0
for left, side_len in positions:
right = left + side_len
base_height = 0
# Find the height of the surface the current square will land on
for l, r, h in intervals:
# Check for overlap. The intervals are [left, right) and [l, r).
# Overlap condition is left < r and l < right.
if left < r and l < right:
base_height = max(base_height, h)
# Calculate the height of the current square's top
current_height = base_height + side_len
# Add the current square's final state to our list
intervals.append((left, right, current_height))
# Update the overall maximum height seen so far
max_h = max(max_h, current_height)
ans.append(max_h)
return ans
Falling Squares in Java
class Solution {
public List<Integer> fallingSquares(int[][] positions) {
// A list to store information about landed squares: [left, right, height]
List<int[]> intervals = new ArrayList<>();
List<Integer> ans = new ArrayList<>();
int maxH = 0;
for (int[] pos : positions) {
int left = pos[0];
int sideLength = pos[1];
int right = left + sideLength;
int baseHeight = 0;
// Find the height of the surface the current square will land on
for (int[] interval : intervals) {
int prevL = interval[0];
int prevR = interval[1];
int prevH = interval[2];
// Check for overlap. The intervals are [left, right) and [prevL, prevR).
// Overlap condition is left < prevR and prevL < right.
if (left < prevR && prevL < right) {
baseHeight = Math.max(baseHeight, prevH);
}
}
// Calculate the height of the current square's top
int currentHeight = baseHeight + sideLength;
// Add the current square's final state to our list
intervals.add(new int[]{left, right, currentHeight});
// Update the overall maximum height seen so far
maxH = Math.max(maxH, currentHeight);
ans.add(maxH);
}
return ans;
}
}
Falling Squares in C++
class Solution {
public:
vector<int> fallingSquares(vector<vector<int>>& positions) {
// A vector to store information about landed squares: {left, right, height}
vector<vector<int>> intervals;
vector<int> ans;
int maxH = 0;
for (const auto& pos : positions) {
int left = pos[0];
int sideLength = pos[1];
int right = left + sideLength;
int baseHeight = 0;
// Find the height of the surface the current square will land on
for (const auto& interval : intervals) {
int prevL = interval[0];
int prevR = interval[1];
int prevH = interval[2];
// Check for overlap. If they overlap, the current square might land on this previous one.
// The intervals are [left, right) and [prevL, prevR).
// Overlap condition is left < prevR and prevL < right.
if (left < prevR && prevL < right) {
baseHeight = max(baseHeight, prevH);
}
}
// Calculate the height of the current square's top
int currentHeight = baseHeight + sideLength;
// Add the current square's final state to our list
intervals.push_back({left, right, currentHeight});
// Update the overall maximum height seen so far
maxH = max(maxH, currentHeight);
ans.push_back(maxH);
}
return ans;
}
};
Falling Squares in TypeScript
function fallingSquares(positions: number[][]): number[] {
// A list to store information about landed squares: [left, right, height]
const intervals: number[][] = [];
const ans: number[] = [];
let maxH = 0;
for (const pos of positions) {
const left = pos[0];
const sideLength = pos[1];
const right = left + sideLength;
let baseHeight = 0;
// Find the height of the surface the current square will land on
for (const interval of intervals) {
const prevL = interval[0];
const prevR = interval[1];
const prevH = interval[2];
// Check for overlap. The intervals are [left, right) and [prevL, prevR).
// Overlap condition is left < prevR and prevL < right.
if (left < prevR && prevL < right) {
baseHeight = Math.max(baseHeight, prevH);
}
}
// Calculate the height of the current square's top
const currentHeight = baseHeight + sideLength;
// Add the current square's final state to our list
intervals.push([left, right, currentHeight]);
// Update the overall maximum height seen so far
maxH = Math.max(maxH, currentHeight);
ans.push(maxH);
}
return ans;
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.