-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path90.cpp
More file actions
51 lines (41 loc) · 1.31 KB
/
Copy path90.cpp
File metadata and controls
51 lines (41 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <string>
#include <vector>
using namespace std;
struct CompressionResult {
int zeros = 0;
int ones = 0;
};
CompressionResult compress(const vector<vector<int>>& arr, int y, int x, int size) {
CompressionResult result;
if (size == 1) {
arr[y][x] == 0 ? result.zeros++ : result.ones++;
return result;
}
int firstValue = arr[y][x];
bool canCompress = true;
for (int i = y; i < y + size && canCompress; i++) {
for (int j = x; j < x + size; j++) {
if (arr[i][j] != firstValue) {
canCompress = false;
break;
}
}
}
if (canCompress) {
firstValue == 0 ? result.zeros++ : result.ones++;
} else {
int halfSize = size / 2;
CompressionResult parts[4];
int offsets[4][2] = {{0, 0}, {0, halfSize}, {halfSize, 0}, {halfSize, halfSize}};
for (int i = 0; i < 4; i++) {
parts[i] = compress(arr, y + offsets[i][0], x + offsets[i][1], halfSize);
result.zeros += parts[i].zeros;
result.ones += parts[i].ones;
}
}
return result;
}
vector<int> solution(vector<vector<int>> arr) {
CompressionResult finalResult = compress(arr, 0, 0, arr.size());
return {finalResult.zeros, finalResult.ones};
}