Skip to content

Commit 6cf4982

Browse files
authored
Update falling-squares.cpp
1 parent efa8b60 commit 6cf4982

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

C++/falling-squares.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ class Solution2 {
132132
// Space: O(n)
133133
class Solution3 {
134134
public:
135-
vector<int> fallingSquares(vector<pair<int, int>>& positions) {
135+
vector<int> fallingSquares(vector<vector<int>>& positions) {
136136
set<int> index;
137137
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);
140140
}
141141
const auto W = index.size();
142142
const auto B = static_cast<int>(sqrt(W));
@@ -146,9 +146,9 @@ class Solution3 {
146146
auto max_height = 0;
147147
vector<int> result;
148148
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];
152152
update(B, h, L, R, &heights, &blocks, &blocks_read);
153153
max_height = max(max_height, h);
154154
result.emplace_back(max_height);
@@ -204,16 +204,14 @@ class Solution3 {
204204
// Space: O(n)
205205
class Solution4 {
206206
public:
207-
vector<int> fallingSquares(vector<pair<int, int>>& positions) {
207+
vector<int> fallingSquares(vector<vector<int>>& positions) {
208208
vector<int> heights(positions.size());
209209
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];
212211
int right_i = left_i + size_i;
213212
heights[i] += size_i;
214213
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];
217215
int right_j = left_j + size_j;
218216
if (left_j < right_i and left_i < right_j) { // intersect
219217
heights[j] = max(heights[j], heights[i]);

0 commit comments

Comments
 (0)