@@ -132,11 +132,11 @@ class Solution2 {
132
132
// Space: O(n)
133
133
class Solution3 {
134
134
public:
135
- vector<int > fallingSquares (vector<pair< int , int >>& positions) {
135
+ vector<int > fallingSquares (vector<vector< int >>& positions) {
136
136
set<int > index ;
137
137
for (const auto & position : positions) {
138
- index .emplace (position. first );
139
- index .emplace (position. first + position. second - 1 );
138
+ index .emplace (position[ 0 ] );
139
+ index .emplace (position[ 0 ] + position[ 1 ] - 1 );
140
140
}
141
141
const auto W = index .size ();
142
142
const auto B = static_cast <int >(sqrt (W));
@@ -146,9 +146,9 @@ class Solution3 {
146
146
auto max_height = 0 ;
147
147
vector<int > result;
148
148
for (const auto & position : positions) {
149
- const auto L = distance (index .begin (), index .find (position. first ));
150
- const auto R = distance (index .begin (), index .find (position. first + position. second - 1 ));
151
- const auto h = query (B, L, R, heights, blocks, blocks_read) + position. second ;
149
+ const auto L = distance (index .begin (), index .find (position[ 0 ] ));
150
+ const auto R = distance (index .begin (), index .find (position[ 0 ] + position[ 1 ] - 1 ));
151
+ const auto h = query (B, L, R, heights, blocks, blocks_read) + position[ 1 ] ;
152
152
update (B, h, L, R, &heights, &blocks, &blocks_read);
153
153
max_height = max (max_height, h);
154
154
result.emplace_back (max_height);
@@ -204,16 +204,14 @@ class Solution3 {
204
204
// Space: O(n)
205
205
class Solution4 {
206
206
public:
207
- vector<int > fallingSquares (vector<pair< int , int >>& positions) {
207
+ vector<int > fallingSquares (vector<vector< int >>& positions) {
208
208
vector<int > heights (positions.size ());
209
209
for (int i = 0 ; i < positions.size (); ++i) {
210
- int left_i, size_i;
211
- tie (left_i, size_i) = positions[i];
210
+ int left_i = positions[i][0 ], size_i = positions[i][1 ];
212
211
int right_i = left_i + size_i;
213
212
heights[i] += size_i;
214
213
for (int j = i + 1 ; j < positions.size (); ++j) {
215
- int left_j, size_j;
216
- tie (left_j, size_j) = positions[j];
214
+ int left_j = positions[j][0 ], size_j = positions[j][1 ];
217
215
int right_j = left_j + size_j;
218
216
if (left_j < right_i and left_i < right_j) { // intersect
219
217
heights[j] = max (heights[j], heights[i]);
0 commit comments