-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxAreaCakeHorizVertCuts.java
45 lines (36 loc) · 1.62 KB
/
MaxAreaCakeHorizVertCuts.java
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
package techQuestions;
/*
Purpose: Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts
and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith
horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided
in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Author: Erich Meissner
Date: 5/30/2020
Time: 10:37 PM
*/
import java.util.Arrays;
public class MaxAreaCakeHorizVertCuts {
public static void main(String[] args) {
int h = 5;
int w = 4;
int[] horCuts = {3};
int[] vertCuts = {3};
System.out.println(maxArea(h,w,horCuts,vertCuts));
}
public static int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
Arrays.sort(horizontalCuts);
Arrays.sort(verticalCuts);
int maxHor = horizontalCuts[0] - 0;
int maxVert = verticalCuts[0] - 0;
for (int i = 1; i < horizontalCuts.length; i++) {
maxHor = Math.max(maxHor, horizontalCuts[i] - horizontalCuts[i-1]);
}
for (int j = 1; j < verticalCuts.length; j++) {
maxVert = Math.max(maxVert, verticalCuts[j] - verticalCuts[j-1]);
}
maxHor = Math.max(maxHor, h - horizontalCuts[horizontalCuts.length-1]);
maxVert = Math.max(maxVert, w - verticalCuts[verticalCuts.length-1]);
return maxHor * maxVert;
}
}