Created
June 24, 2020 03:38
-
-
Save bhaveshmunot1/a2cab68a4a83889e32090b6babc765a6 to your computer and use it in GitHub Desktop.
Leetcode #1465: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts (https://www.InterviewRecipes.com/leetcode-1465)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
const int mod = (1e9)+7; // As the question expects. | |
int getMax(int length, vector<int> &cuts) { | |
sort(cuts.begin(), cuts.end()); // Sort so that we will easily get the thickness | |
// of each piece after cutting it. | |
int n = cuts.size(); | |
int maxCut = max(length-cuts[n-1], // Thickness of the last piece. | |
cuts[0]); // Thickness of the first piece. | |
for (int i=1; i<n; i++) { // For each cut - | |
maxCut = max(maxCut, cuts[i]-cuts[i-1]); // Thickness of each piece. | |
} | |
return maxCut; | |
} | |
public: | |
int maxArea(int height, int width, vector<int>& h, vector<int>& w) { | |
long maxHeight = getMax(height, h); | |
long maxWidth = getMax(width, w); | |
return (int)(maxHeight * maxWidth % mod); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment