Leetcode - Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
You are given a rectangular cake of size h x 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, and 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 large number, return this modulo 109 + 7.
Solution,#
μ€λ λ³Έ μ½λ© ν
μ€νΈμμ λμ¨ λ¬Έμ μΈλ°, μ΄μΌ μ΄κ±Έ λͺ»νμλ€λκ² μ°Έ λ©μ²νλ€. μ°μ , hc
μ vc
κ° μ λ ¬λμ΄ μλ€λ κ°μ νμ (μλμ΄ μμ μ μμΌλ μ λ ¬ λλ¦¬κ³ ) μμνλ€.
μ¬λμ νμ΄λ μμΈλ‘ 보λ΄λΌ νλκ°. μλ κ²°κ΅ ν΅μ¬μ νλλ€. λͺ¨λ κ²½μ°μ μλ₯Ό λ€ νμν μ΄μ κ° μλ€.
- λ΄κ° μ΄ μΌμμ λμ μλ€
- μ리λ λ²μλ₯Ό μλ€
μ¦, μ΅λλ‘ μλ₯Ό μ μλ λ²μλ₯Ό μ°μ ꡬνλ€. κ·Έλ¦¬κ³ λ΄κ° μλ₯Έ κ²λ³΄λ€ λ¨μκ² λ ν¬λ©΄ κ·Έκ² μ΅λ κ°μΌ κ²μ΄λ€.
import java.util.*;
import java.math.*;
class Solution {
public int maxArea(int h, int w, int[] hc, int[] vc) {
Arrays.sort(hc);
Arrays.sort(vc);
int curHeight = 0;
long maxHeight = 0;
for (int i = 0; i < hc.length; ++i) {
maxHeight = Math.max(hc[i] - curHeight, maxHeight);
curHeight = hc[i];
}
int curWidth = 0;
long maxWidth = 0;
for (int i = 0; i < vc.length; ++i) {
maxWidth = Math.max(vc[i] - curWidth, maxWidth);
curWidth = vc[i];
}
maxWidth = Math.max(maxWidth, w - vc[vc.length - 1]);
maxHeight = Math.max(maxHeight, h - hc[hc.length - 1]);
long result = maxHeight * maxWidth;
return (int) ((maxHeight * maxWidth) % 1000000007);
}
}
μκ° λ³΅μ‘λλ O(n + log n + m + log m)
μ λ κ°λ€.
μ§μμ 10λΆμ νΌκ±Έ λͺ»νΈλ€.. μΆμ΄λ μ°Έ 기ꡬν λ²μ΄λ€..