엔지니어 게시판
LeetCode 솔루션 분류

[7/2] 1465. 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.

 

Example 1:

Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4 
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.

Example 2:

Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.

Example 3:

Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9

 

Constraints:

  • 2 <= h, w <= 109
  • 1 <= horizontalCuts.length <= min(h - 1, 105)
  • 1 <= verticalCuts.length <= min(w - 1, 105)
  • 1 <= horizontalCuts[i] < h
  • 1 <= verticalCuts[i] < w
  • All the elements in horizontalCuts are distinct.
  • All the elements in verticalCuts are distinct.

관련자료

댓글 1

mingki님의 댓글

  • 익명
  • 작성일
C++
Runtime: 89 ms, faster than 74.55% of C++ online submissions for Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts.
Memory Usage: 32.4 MB, less than 40.04% of C++ online submissions for Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts.
class Solution {
public:
    int maxArea(int h, int w, vector<int>& hc, vector<int>& vc) {
        int MOD = 1000000000 + 7;
        
        hc.push_back(h);
        vc.push_back(w);
        
        sort(hc.begin(), hc.end());
        sort(vc.begin(), vc.end());
        
        int hcsize = hc.size();
        int vcsize = vc.size();
        
        int hdiff = hcsize > 0 ? hc[0] : h;
        int wdiff = vcsize > 0 ? vc[0] : w;
        
        for (int i = 1; i < hcsize; ++i) {
            hdiff = max(hdiff, hc[i] - hc[i - 1]);
        }
        for (int i = 1; i < vcsize; ++i) {
            wdiff = max(wdiff, vc[i] - vc[i - 1]);
        }
        
        return ((long)hdiff * (long)wdiff) % MOD;
    }
};
전체 94 / 2 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 721 명
  • 오늘 방문자 4,024 명
  • 어제 방문자 7,431 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,542 명
알림 0