LeetCode //C - 699. Falling Squares

699. Falling Squares

There are several squares being dropped onto the X-axis of a 2D plane.

You are given a 2D integer array positions where p o s i t i o n s [ i ] = [ l e f t i , s i d e L e n g t h i ] positions[i] = [left_i, sideLength_i] positions[i]=[lefti,sideLengthi] represents the i t h i^{th} ith square with a side length of s i d e L e n g t h i sideLength_i sideLengthi that is dropped with its left edge aligned with X-coordinate l e f t i left_i lefti.

Each square is dropped one at a time from a height above any landed squares. It then falls downward (negative Y direction) until it either lands on the top side of another square or on the X-axis. A square brushing the left/right side of another square does not count as landing on it. Once it lands, it freezes in place and cannot be moved.

After each square is dropped, you must record the height of the current tallest stack of squares.

Return an integer array ans where ans[i] represents the height described above after dropping the $i^{th} $square.
 

Example 1:

Input: positions = [[1,2],[2,3],[6,1]]
Output: [2,5,5]
Explanation:
After the first drop, the tallest stack is square 1 with a height of 2.
After the second drop, the tallest stack is squares 1 and 2 with a height of 5.
After the third drop, the tallest stack is still squares 1 and 2 with a height of 5.
Thus, we return an answer of [2, 5, 5].

Example 2:

Input: positions = [[100,100],[200,100]]
Output: [100,100]
Explanation:
After the first drop, the tallest stack is square 1 with a height of 100.
After the second drop, the tallest stack is either square 1 or square 2, both with heights of 100.
Thus, we return an answer of [100, 100].
Note that square 2 only brushes the right side of square 1, which does not count as landing on it.

Constraints:
  • 1 <= positions.length <= 1000
  • 1 < = l e f t i < = 1 0 8 1 <= lefti <= 10^8 1<=lefti<=108
  • 1 < = s i d e L e n g t h i < = 1 0 6 1 <= sideLengthi <= 10^6 1<=sideLengthi<=106

From: LeetCode
Link: 699. Falling Squares


Solution:

Ideas:
  • For each square, determine its [left, right) interval.

  • For every previous square, check if the new square overlaps horizontally.

  • If yes, the square will fall on top of the max height it intersects.

  • After placing the square, update the maxHeight and record it.

Code:
#define MAX(a, b) ((a) > (b) ? (a) : (b))

int* fallingSquares(int** positions, int positionsSize, int* positionsColSize, int* returnSize){
    // To store left, right, and height of each placed square
    int lefts[positionsSize], rights[positionsSize], heights[positionsSize];
    int* result = (int*)malloc(sizeof(int) * positionsSize);
    int maxHeight = 0;

    for (int i = 0; i < positionsSize; i++) {
        int left = positions[i][0];
        int size = positions[i][1];
        int right = left + size;
        int baseHeight = 0;

        // Check all previous squares for overlap
        for (int j = 0; j < i; j++) {
            if (left < rights[j] && right > lefts[j]) {
                baseHeight = MAX(baseHeight, heights[j]);
            }
        }

        heights[i] = baseHeight + size;
        lefts[i] = left;
        rights[i] = right;

        maxHeight = MAX(maxHeight, heights[i]);
        result[i] = maxHeight;
    }

    *returnSize = positionsSize;
    return result;
}

你可能感兴趣的:(LeetCode,leetcode,c语言,算法)