- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 55 lines of C++ from the credited upstream file find-maximum-area-of-a-triangle.cpp.
- The implementation visibly relies on sequence storage, hash lookup.
- 3 loop blocks detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 45class Solution {6public:7 long long maxArea(vector<vector<int>>& coords) {8 static const int POS_INF = numeric_limits<int>::max();9 static const int NEG_INF = numeric_limits<int>::min();10 int mx_x = NEG_INF;11 int mn_x = POS_INF;12 int mx_y = NEG_INF;13 int mn_y = POS_INF;14 unordered_map<int, int> lookup_mx_y;15 unordered_map<int, int> lookup_mn_y;16 unordered_map<int, int> lookup_mx_x;17 unordered_map<int, int> lookup_mn_x;18 for (const auto& c : coords) {19 const auto& x = c[0], &y = c[1];20 mx_x = max(mx_x, x);21 mn_x = min(mn_x, x);22 mx_y = max(mx_y, y);23 mn_y = min(mn_y, y);24 if (!lookup_mx_y.count(x)) {25 lookup_mx_y[x] = y;26 } else {27 lookup_mx_y[x] = max(lookup_mx_y[x], y);28 }29 if (!lookup_mn_y.count(x)) {30 lookup_mn_y[x] = y;31 } else {32 lookup_mn_y[x] = min(lookup_mn_y[x], y);33 }34 if (!lookup_mx_x.count(y)) {35 lookup_mx_x[y] = x;36 } else {37 lookup_mx_x[y] = max(lookup_mx_x[y], x);38 }39 if (!lookup_mn_x.count(y)) {40 lookup_mn_x[y] = x;41 } else {42 lookup_mn_x[y] = min(lookup_mn_x[y], x);43 }44 }45 int64_t result = 0;46 for (const auto& [x, _] : lookup_mx_y) {47 result = max(result, static_cast<int64_t>(lookup_mx_y[x] - lookup_mn_y[x]) * max(mx_x - x, x - mn_x));48 }49 for (const auto& [y, _] : lookup_mx_x) {50 result = max(result, static_cast<int64_t>(lookup_mx_x[y] - lookup_mn_x[y]) * max(mx_y - y, y - mn_y));51 }52 return result ? result : -1;53 }54};55