其他分享
首页 > 其他分享> > 699. Falling Squares

699. Falling Squares

作者:互联网

问题:

无限横坐标中,下落正方块问题。

position[i]={x, len}

表示:每次落在坐标 x 的位置,正方块变长len。

求每次落下后,当前最高y坐标的高度。

Example 1:
Input: [[1, 2], [2, 3], [6, 1]]
Output: [2, 5, 5]
Explanation:
After the first drop of positions[0] = [1, 2]: _aa _aa ------- The maximum height of any square is 2.

After the second drop of positions[1] = [2, 3]: __aaa __aaa __aaa _aa__ _aa__ -------------- 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]: __aaa __aaa __aaa _aa _aa___a -------------- 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.

  

解法:map区段法

思路:

代码参考:

 1 class Solution {
 2 public:
 3     struct Sec{
 4         int start;
 5         int end;
 6         int height;
 7         Sec(int s, int e, int h):start(s), end(e), height(h) {};
 8     };
 9     
10     vector<int> fallingSquares(vector<vector<int>>& positions) {
11         vector<int> res;
12         map<pair<int, int>, int> sq;
13         int maxH = INT_MIN;
14         for(auto p:positions) {
15             //find the intersecting part
16             //[start~end): [p[0], p[0]+p[1])
17             //height: p[1]
18             Sec cur(p[0], p[0]+p[1], p[1]);
19             auto it = sq.lower_bound({cur.start, cur.end});
20             vector<Sec> bound2;//start,end
21             int base_h = 0;
22             if(it!=sq.begin()) it--;
23             //check the intersecting part:
24             //1. to get the base max height;
25             //2. erase all intersected section.
26             //3. memo the 2 bounds section.(for following appending)
27             while(it!=sq.end()) {
28                 Sec sc_cmp(it->first.first, it->first.second, it->second);
29                 //over 2 bounds:
30                 if(sc_cmp.end<=cur.start) {
31                     it++;
32                     continue;
33                 }
34                 if(sc_cmp.start>=cur.end) break;
35                 //2 bounds:
36                 //left
37                 if(sc_cmp.start<cur.start)
38                     bound2.emplace_back(Sec(sc_cmp.start, cur.start, sc_cmp.height));
39                 //right
40                 if(sc_cmp.end>cur.end)
41                     bound2.emplace_back(Sec(cur.end, sc_cmp.end, sc_cmp.height));
42                 base_h = max(base_h, sc_cmp.height);
43                 it = sq.erase(it);//cause erase action, it may be the next item.
44             }
45             //append:
46             //2 bounds +  cur section
47             sq[{cur.start, cur.end}] = base_h + cur.height;
48             for(auto b:bound2) {
49                 sq[{b.start, b.end}] = b.height;
50             }
51             //cout<<"cur_height:"<<base_h + cur.height<<endl;
52             maxH = max(maxH, base_h + cur.height);
53             res.push_back(maxH);
54         }
55         return res;
56     }
57 };

 

标签:end,cur,int,699,height,start,Falling,Squares,cmp
来源: https://www.cnblogs.com/habibah-chang/p/14717598.html