Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 552e2b3

Browse files
Merge pull request #89 from zhi-zhi-zhi/master
fix: 更新 right+1 边界时,未考虑是否会破坏原 right +1;
2 parents 87d0899 + cb9a348 commit 552e2b3

File tree

2 files changed

+60
-64
lines changed

2 files changed

+60
-64
lines changed
Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
11
class Solution {
22
public:
3-
vector<int> fallingSquares(vector<pair<int, int>>& positions)
4-
{
5-
map<int,int>Map;
6-
7-
Map[0]=0;
8-
Map[INT_MAX]=0;
9-
10-
vector<int>results;
11-
int cur=0;
12-
13-
for (auto p:positions)
14-
{
15-
int left=p.first;
16-
int right=p.first+p.second-1;
17-
int h=p.second;
18-
int maxH=0;
19-
3+
vector<int> fallingSquares(vector <vector<int>> &positions) {
4+
map<int, int> Map;
5+
6+
Map[0] = 0;
7+
Map[INT_MAX] = 0;
8+
9+
vector<int> results;
10+
int cur = 0;
11+
12+
for (auto p: positions) {
13+
int left = p[0];
14+
int right = p[0] + p[1] - 1;
15+
int h = p[1];
16+
int maxH = 0;
17+
2018
auto ptri = Map.lower_bound(left);
2119
auto ptrj = Map.upper_bound(right);
22-
23-
int temp = prev(ptrj,1)->second;
24-
25-
auto ptr = ptri->first==left? ptri:prev(ptri,1);
26-
while (ptr!=ptrj)
27-
{
28-
maxH=max(maxH, ptr->second);
29-
ptr = next(ptr,1);
20+
21+
int temp = prev(ptrj, 1)->second;
22+
23+
auto ptr = ptri->first == left ? ptri : prev(ptri, 1);
24+
while (ptr != ptrj) {
25+
maxH = max(maxH, ptr->second);
26+
ptr = next(ptr, 1);
3027
}
31-
if (ptri!=ptrj)
32-
Map.erase(ptri,ptrj);
33-
34-
Map[left] = maxH+h;
35-
Map[right+1] = temp;
36-
cur = max(cur, maxH+h);
37-
38-
results.push_back(cur);
28+
if (ptri != ptrj)
29+
Map.erase(ptri, ptrj);
30+
31+
Map[left] = maxH + h;
32+
if (right + 1 < ptrj->first)
33+
Map[right + 1] = temp;
34+
cur = max(cur, maxH + h);
35+
36+
results.push_back(cur);
3937
}
40-
38+
4139
return results;
42-
40+
4341
}
4442
};
Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
class Solution {
22
public:
3-
vector<int> fallingSquares(vector<pair<int, int>>& positions)
4-
{
5-
map<int,int>Map;
6-
Map[0]=0;
7-
Map[INT_MAX]=0;
8-
int cur=0;
9-
vector<int>results;
10-
11-
for (int i=0; i<positions.size(); i++)
12-
{
13-
int left=positions[i].first;
14-
int len=positions[i].second;
15-
int right=left+len-1;
16-
3+
vector<int> fallingSquares(vector <vector<int>> &positions) {
4+
map<int, int> Map;
5+
Map[0] = 0;
6+
Map[INT_MAX] = 0;
7+
int cur = 0;
8+
vector<int> results;
9+
10+
for (int i = 0; i < positions.size(); i++) {
11+
int left = positions[i][0];
12+
int len = positions[i][1];
13+
int right = left + len - 1;
14+
1715
auto pos1 = Map.lower_bound(left);
18-
19-
int Hmax=0;
20-
auto pos=pos1;
21-
if (pos->first!=left) pos=prev(pos,1);
22-
while (pos->first <= right)
23-
{
16+
17+
int Hmax = 0;
18+
auto pos = pos1;
19+
if (pos->first != left) pos = prev(pos, 1);
20+
while (pos->first <= right) {
2421
Hmax = max(Hmax, pos->second);
25-
pos = next(pos,1);
22+
pos = next(pos,1);
2623
}
27-
int rightHeight = prev(pos,1)->second;
28-
29-
Map.erase(pos1,pos);
30-
Map[left]=Hmax+len;
31-
Map[right+1]=rightHeight;
32-
33-
cur = max(cur, Hmax+len);
24+
int rightHeight = prev(pos, 1)->second;
25+
26+
Map.erase(pos1, pos);
27+
Map[left] = Hmax + len;
28+
if (right + 1 < pos->first)
29+
Map[right + 1] = rightHeight;
30+
31+
cur = max(cur, Hmax + len);
3432
results.push_back(cur);
3533
}
36-
34+
3735
return results;
3836
}
3937
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /