-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaximumArea.cpp
More file actions
48 lines (36 loc) · 2.58 KB
/
maximumArea.cpp
File metadata and controls
48 lines (36 loc) · 2.58 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
class Solution {
public:
int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
sort(horizontalCuts.begin(), horizontalCuts.end()); //Sorting both the vectors using
// the in-built sort function.
sort(verticalCuts.begin(), verticalCuts.end()); // Sorting is done in order to
// arrange the horizontal cuts in
// the order from top to bottom, and vertical from left to right.
// This is done to get the difference i.e. the length and breadth
// of the rectangle piece which results from cutting the cake.
int maxH = 0, maxV = 0; //These will store the maximum height(length) and
// width(breadth) of the piece.
int m = horizontalCuts.size(), n = verticalCuts.size();
maxH = max(maxH, horizontalCuts[0]); // Getting maximum of current maxH and length
// of piece from top to first cut.
for(int i = 1; i < m; i++){ //Iterating through horizontal cuts.
maxH = max(maxH, horizontalCuts[i] - horizontalCuts[i-1]);
// Comparing and storing max of maxH and the distance(height) between
// the previous and current cut.
}
maxH = max(maxH, h - horizontalCuts[m - 1]); // Getting maximum of maxH and length
// of piece from last cut to bottom of cake.
maxV = max(maxV, verticalCuts[0]); // Getting maximum of current maxV and breadth
// of piece from left(start) to first cut.
for(int i = 1; i < n; i++){ //Iterating through vertical cuts.
maxV = max(maxV, verticalCuts[i] - verticalCuts[i-1]);
// Comparing and storing max of maxV and the distance(breadth) between
// the previous and current cut.
}
maxV = max(maxV, w - verticalCuts[n - 1]); // Getting maximum of maxV and length
// of piece from last cut to right of cake.
// Returning area modulo 10^9 + 7
return (long)maxH * maxV % 1000000007; // Using long as such a big number
// cannot be stored in int.
}
};