N

(Leet Code c++)Maximum Area of a Piece of cake after horizontal and vertical cuts 본문

Leet Code 알고리즘

(Leet Code c++)Maximum Area of a Piece of cake after horizontal and vertical cuts

naeunchan 2021. 9. 14. 10:15
728x90
반응형

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.

 

주어진 horizontalCuts와 verticalCuts에서 차이가 가장 큰 값을 찾아 답을 리턴해야 한다.

 

우선 각 벡터 배열을 오름차순으로 정렬한다.

이후 getMaxDiff 함수를 이용한다.

 

getMaxDiff()

매개변수로 각 벡터와 시작값, 각 벡터의 0번째 값, h or w를 넘겨준다.

for문은 1 ~ v.size() 만큼 반복.

i번째 값과 i - 1번째 값을 빼서 가장 큰 값을 diff로 갱신한다.

 

for문을 빠져 나오게 되면, diff 값과 length - v.back() 값을 비교해 더 큰 값을 다시 diff로 갱신한다.

(v의 가장 마지막 위치 ~ h or w까지 자르는 경우를 생각)

 

이후 MOD 값으로 modulo 연산을 한 후 리턴하면 된다.

class Solution {
public:
    void getMaxDiff(vector<int> v, int start, int end, long long &diff, int length){
        for(int i = 1; i < v.size(); i++){
            int tmpStart = v[i - 1];
            int tmpEnd = v[i];
            int tmpDiff = tmpEnd - tmpStart;
            
            if(diff < tmpDiff){
                diff = tmpDiff;
                start = tmpStart;
                end = tmpEnd;
            }
        }
        
        diff = diff < length - v.back() ? length - v.back() : diff;
    }
    
    int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
        const int MOD = 1000000007;
        long long answer = 0;
        long long rowDiff = 0;
        long long colDiff = 0;
        
        sort(horizontalCuts.begin(), horizontalCuts.end());
        sort(verticalCuts.begin(), verticalCuts.end());
        
        rowDiff = horizontalCuts[0];
        colDiff = verticalCuts[0];
        
        getMaxDiff(horizontalCuts, 0, horizontalCuts[0], rowDiff, h);
        getMaxDiff(verticalCuts, 0, verticalCuts[0], colDiff, w);
        
        answer = (rowDiff * colDiff) % MOD;
        
        return answer;
    }
};
728x90
반응형