[InterviewBit] Maximum Area of Triangle!

Maximum Area of Triangle!

  • Time :
  • Space :
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
int Solution::solve(vector<string> &A) {
vector<int> R(A[0].length()), G(A[0].length()), B(A[0].length());
for(int i = 0; i < A.size(); i++) {
for(int j = 0; j < A[i].size(); j++) {
if(A[i][j] == 'r') R[j] += 1;
if(A[i][j] == 'g') G[j] += 1;
if(A[i][j] == 'b') B[j] += 1;
}
}
int res = 0;
auto work = [](pair<int, int> A, pair<int, int> B, vector<int>& C, int y) {
if(A.first == INT_MAX or B.first == INT_MAX) return 0;
int h = max({abs(A.first - B.first), abs(A.first - B.second), abs(A.second - B.first), abs(A.second - B.second)}) + 1;
int miw = INT_MAX, maw = INT_MIN;
for(int i = 0; i < C.size(); i++) {
if(C[i]) {
miw = min(miw, i);
maw = max(maw, i);
}
}
if(miw == INT_MAX) return 0;
return (h * (max(abs(miw - y), abs(maw - y)) + 1) + 1) / 2;
};
for(int i = 0; i < A[0].size(); i++) {
pair<int, int> r{INT_MAX, INT_MIN};
pair<int, int> g{INT_MAX, INT_MIN};
pair<int, int> b{INT_MAX, INT_MIN};
for(int j = 0; j < A.size(); j++) {
if(A[j][i] == 'r') r.first = min(r.first, j), r.second = max(r.second, j);
if(A[j][i] == 'g') g.first = min(g.first, j), g.second = max(g.second, j);
if(A[j][i] == 'b') b.first = min(b.first, j), b.second = max(b.second, j);
}
res = max(res, work(r,g,B,i));
res = max(res, work(r,b,G,i));
res = max(res, work(g,b,R,i));
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/03/PS/interviewbit/maximum-area-of-triangle/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.