This repository has been archived by the owner on May 16, 2021. It is now read-only.
forked from zhuli19901106/leetcode-zhuli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest-meeting-point_1_AC.cpp
71 lines (63 loc) · 1.66 KB
/
best-meeting-point_1_AC.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Make sure you calculate the cumulated sum in the right way.
#include <algorithm>
#include <climits>
#include <vector>
using std::min;
using std::vector;
class Solution {
public:
int minTotalDistance(vector<vector<int>>& grid) {
auto &a = grid;
int n = a.size();
int m = (n > 0 ? a[0].size() : 0);
if (n == 0 || m == 0) {
return 0;
}
vector<int> ci(n, 0), cj(m, 0);
int i, j;
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j) {
if (a[i][j] == 1) {
++ci[i];
++cj[j];
}
}
}
vector<int> vl(m, 0), vr(m, 0), vu(n, 0), vd(n, 0);
int sum;
sum = cj[0];
for (j = 1; j <= m - 1; ++j) {
vl[j] = vl[j - 1] + sum;
sum += cj[j];
}
sum = cj[m - 1];
for (j = m - 2; j >= 0; --j) {
vr[j] = vr[j + 1] + sum;
sum += cj[j];
}
sum = ci[0];
for (i = 1; i <= n - 1; ++i) {
vu[i] = vu[i - 1] + sum;
sum += ci[i];
}
sum = ci[n - 1];
for (i = n - 2; i >= 0; --i) {
vd[i] = vd[i + 1] + sum;
sum += ci[i];
}
int res = INT_MAX;
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j) {
sum = vl[j] + vr[j] + vu[i] + vd[i];
res = min(res, sum);
}
}
ci.clear();
cj.clear();
vl.clear();
vr.clear();
vu.clear();
vd.clear();
return res;
}
};