leetcode 699. Falling Squares 俄罗斯方块的最高高度 + 暴力遍历即可

分手后的思念是犯贱 2022-06-03 09:35 250阅读 0赞

On an infinite number line (x-axis), we drop given squares in the order they are given.

The i-th square dropped (positions[i] = (left, side_length)) is a square with the left-most point being positions[i][0] and sidelength positions[i][1].

The square is dropped with the bottom edge parallel to the number line, and from a higher height than all currently landed squares. We wait for each square to stick before dropping the next.

The squares are infinitely sticky on their bottom edge, and will remain fixed to any positive length surface they touch (either the number line or another square). Squares dropped adjacent to each other will not stick together prematurely.

Return a list ans of heights. Each height ans[i] represents the current highest height of any square we have dropped, after dropping squares represented by positions[0], positions[1], …, positions[i].

Example 1:
Input: [[1, 2], [2, 3], [6, 1]]
Output: [2, 5, 5]
Explanation:

After the first drop of positions[0] = [1, 2]:

  1. _aa
  2. _aa
  3. -------

The maximum height of any square is 2.

After the second drop of positions[1] = [2, 3]:

  1. __aaa
  2. __aaa
  3. __aaa
  4. _aa__
  5. _aa__
  6. --------------

The maximum height of any square is 5.
The larger square stays on top of the smaller square despite where its center
of gravity is, because squares are infinitely sticky on their bottom edge.

After the third drop of positions[1] = [6, 1]:

  1. __aaa
  2. __aaa
  3. __aaa
  4. _aa
  5. _aa___a
  6. --------------

The maximum height of any square is still 5.

Thus, we return an answer of [2, 5, 5].

Example 2:
Input: [[100, 100], [200, 100]]
Output: [100, 100]
Explanation: Adjacent squares don’t get stuck prematurely - only their bottom edge can stick to surfaces.
Note:

1 <= positions.length <= 1000.
1 <= positions[i][0] <= 10^8.
1 <= positions[i][1] <= 10^6.

本题题意很简单,就是在一个一维的坐标轴上,依次从高处扔箱子,求箱子的最大的高度,直接暴力遍历即可

就是每当落入一个方块的时候,直接遍历求解最高的高度,
在求解最高高度的时候,就是寻找公共区间来判断最高高度,这个就很简单了

代码如下:

  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <set>
  5. #include <queue>
  6. #include <stack>
  7. #include <string>
  8. #include <climits>
  9. #include <algorithm>
  10. #include <sstream>
  11. #include <functional>
  12. #include <bitset>
  13. #include <numeric>
  14. #include <cmath>
  15. #include <regex>
  16. using namespace std;
  17. class Solution
  18. {
  19. public:
  20. vector<int> fallingSquares(vector<pair<int, int>>& pos)
  21. {
  22. vector<int> res;
  23. vector<vector<int>> all;
  24. int h = 0;
  25. for (auto p : pos)
  26. {
  27. vector<int> one = { p.first,p.first + p.second - 1,p.second };
  28. int tmp = getHeight(all,one);
  29. h = max(h, tmp);
  30. res.push_back(h);
  31. }
  32. return res;
  33. }
  34. int getHeight(vector<vector<int>>& all, vector<int>& one)
  35. {
  36. int left = one[0], right = one[1];
  37. int maxHeight = 0;
  38. for (auto i : all)
  39. {
  40. if (left > i[1] || right < i[0])
  41. continue;
  42. maxHeight = max(maxHeight, i[2]);
  43. }
  44. one[2] += maxHeight;
  45. all.push_back(one);
  46. return one[2];
  47. }
  48. };

发表评论

表情:
评论列表 (有 0 条评论,250人围观)

还没有评论,来说两句吧...

相关阅读