-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxAreaCake.cpp
More file actions
57 lines (53 loc) · 1.66 KB
/
Copy pathmaxAreaCake.cpp
File metadata and controls
57 lines (53 loc) · 1.66 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
52
53
54
55
56
/**
* https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts
* Array, Greedy Sorting
* Medium
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
sort(horizontalCuts.begin(), horizontalCuts.end());
sort(verticalCuts.begin(), verticalCuts.end());
int hCutsSize = horizontalCuts.size();
int vCutsSize = verticalCuts.size();
long maxHCut = (long)max(horizontalCuts[0], h - horizontalCuts[hCutsSize - 1]);
for (int i = 1; i < hCutsSize; i++) {
long hDiff = (long)(horizontalCuts[i] - horizontalCuts[i - 1]);
if (hDiff > maxHCut) {
maxHCut = hDiff;
}
}
long maxVCut = (long)(max(verticalCuts[0], w - verticalCuts[vCutsSize - 1]));
for (int i = 1; i < vCutsSize; i++) {
long vDiff = (long)(verticalCuts[i] - verticalCuts[i - 1]);
if (vDiff > maxVCut) {
maxVCut = vDiff;
}
}
long ans = (maxHCut * maxVCut) % 1000000007;
return (int)ans;
}
};
int main() {
int h, w;
cout << "h: ";
cin >> h;
cout << "w: ";
cin >> w;
vector<int> hCuts, vCuts;
cout << "Horizontal cuts:" << endl;
int cut;
while (cin >> cut && cut > 0) {
hCuts.push_back(cut);
}
cout << "Vertical cuts:" << endl;
while (cin >> cut && cut > 0) {
vCuts.push_back(cut);
}
Solution solution;
int maxArea = solution.maxArea(h, w, hCuts, vCuts);
cout << "Maximum area = " << maxArea << endl;
}