Skip to content

Instantly share code, notes, and snippets.

@bhaveshmunot1
Created June 24, 2020 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhaveshmunot1/a2cab68a4a83889e32090b6babc765a6 to your computer and use it in GitHub Desktop.
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)
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