-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC1465.cpp
More file actions
executable file
·35 lines (30 loc) · 965 Bytes
/
LC1465.cpp
File metadata and controls
executable file
·35 lines (30 loc) · 965 Bytes
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
/*
Problem Statement: https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/
Time: O(m + n)
Space: O(m + n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
int maxArea(int h, int w, vector<int> horizontalCuts, vector<int> verticalCuts) {
// preprocess
verticalCuts.push_back(0);
verticalCuts.push_back(w);
horizontalCuts.push_back(0);
horizontalCuts.push_back(h);
int mod, max_h, max_w, m, n;
mod = 1e9 + 7;
max_h = max_w = 0;
m = verticalCuts.size();
n = horizontalCuts.size();
// sort cuts
sort(verticalCuts.begin(), verticalCuts.end());
sort(horizontalCuts.begin(), horizontalCuts.end());
// get maximum gap
for (int i = 1; i < m; i++)
max_w = max(verticalCuts[i] - verticalCuts[i - 1], max_w);
for (int i = 1; i < n; i++)
max_h = max(horizontalCuts[i] - horizontalCuts[i - 1], max_h);
return ((int64_t) max_w * max_h) % mod;
}
};