I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts, the solution in this repository is mainly a greedy solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are moduloMultiplication, maxArea.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 204 ms, faster than 82.11% of C++ online submissions for Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts.
02//Memory Usage: 32.3 MB, less than 100.00% of C++ online submissions for Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts.
03class Solution {
04public:
05 long long moduloMultiplication(long long a,
06 long long b,
07 long long mod)
08 {
09 long long res = 0; // Initialize result
10
11 // Update a if it is more than
12 // or equal to mod
13 a %= mod;
14
15 while (b)
16 {
17 // If b is odd, add a with result
18 if (b & 1)
19 res = (res + a) % mod;
20
21 // Here we assume that doing 2*a
22 // doesn't cause overflow
23 a = (2 * a) % mod;
24
25 b >>= 1; // b = b / 2
26 }
27
28 return res;
29 }
30
31 int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
32 sort(horizontalCuts.begin(), horizontalCuts.end());
33 sort(verticalCuts.begin(), verticalCuts.end());
34
35 int maxh = INT_MIN, maxw = INT_MIN;
36
37 for(int i = 0; i < horizontalCuts.size(); i++){
38 maxh = max(maxh, horizontalCuts[i] - ((i > 0) ? horizontalCuts[i-1] : 0));
39 }
40 maxh = max(maxh, h - horizontalCuts[horizontalCuts.size()-1]);
41
42 for(int i = 0; i < verticalCuts.size(); i++){
43 maxw = max(maxw, verticalCuts[i] - ((i > 0) ? verticalCuts[i-1] : 0));
44 }
45 maxw = max(maxw, w - verticalCuts[verticalCuts.size()-1]);
46
47 return moduloMultiplication(maxh, maxw, 1e9+7);
48 }
49};
Cost