{"id":6857,"date":"2020-05-31T09:49:59","date_gmt":"2020-05-31T16:49:59","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6857"},"modified":"2020-05-31T09:50:06","modified_gmt":"2020-05-31T16:50:06","slug":"leetcode-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts"},"content":{"rendered":"\n<p>Given a rectangular cake with height&nbsp;<code>h<\/code>&nbsp;and width&nbsp;<code>w<\/code>, and two arrays of integers&nbsp;<code>horizontalCuts<\/code>&nbsp;and&nbsp;<code>verticalCuts<\/code>&nbsp;where&nbsp;<code>horizontalCuts[i]<\/code>&nbsp;is the distance from the top of the rectangular cake to the&nbsp;<code>ith<\/code>&nbsp;horizontal cut&nbsp;and similarly,&nbsp;<code>verticalCuts[j]<\/code>&nbsp;is the distance from the&nbsp;left of the rectangular cake to the&nbsp;<code>jth<\/code>&nbsp;vertical cut.<\/p>\n\n\n\n<p><em>Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays&nbsp;<code>horizontalCuts<\/code>&nbsp;and&nbsp;<code>verticalCuts<\/code>.&nbsp;<\/em>Since the answer can be a huge number, return this modulo 10^9 + 7.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/05\/14\/leetcode_max_area_2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]\n<strong>Output:<\/strong> 4 \n<strong>Explanation:<\/strong> 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.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/05\/14\/leetcode_max_area_3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> 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.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]\n<strong>Output:<\/strong> 9\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= h,&nbsp;w &lt;= 10^9<\/code><\/li><li><code>1 &lt;=&nbsp;horizontalCuts.length &lt;&nbsp;min(h, 10^5)<\/code><\/li><li><code>1 &lt;=&nbsp;verticalCuts.length &lt; min(w, 10^5)<\/code><\/li><li><code>1 &lt;=&nbsp;horizontalCuts[i] &lt; h<\/code><\/li><li><code>1 &lt;=&nbsp;verticalCuts[i] &lt; w<\/code><\/li><li>It is guaranteed that all elements in&nbsp;<code>horizontalCuts<\/code>&nbsp;are distinct.<\/li><li>It is guaranteed that all elements in&nbsp;<code>verticalCuts<\/code>&nbsp;are distinct.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Geometry<\/strong><\/h2>\n\n\n\n<p>Find the max gap between vertical cuts mx and max gap between horizontal cuts my. ans = mx * my<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1) if sort in place otherweise O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {\n    constexpr int kMod = 1e9 + 7;\n    sort(begin(verticalCuts), end(verticalCuts));\n    sort(begin(horizontalCuts), end(horizontalCuts));\n    int mx = max(verticalCuts[0], w - verticalCuts.back());\n    int my = max(horizontalCuts[0], h - horizontalCuts.back());\n    for (int i = 1; i < verticalCuts.size(); ++i)\n      mx = max(mx, verticalCuts[i] - verticalCuts[i - 1]);\n    for (int i = 1; i < horizontalCuts.size(); ++i)\n      my = max(my, horizontalCuts[i] - horizontalCuts[i - 1]);               \n    return static_cast<long>(mx) * my % kMod;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a rectangular cake with height&nbsp;h&nbsp;and width&nbsp;w, and two arrays of integers&nbsp;horizontalCuts&nbsp;and&nbsp;verticalCuts&nbsp;where&nbsp;horizontalCuts[i]&nbsp;is the distance from the top of the rectangular cake to the&nbsp;ith&nbsp;horizontal cut&nbsp;and similarly,&nbsp;verticalCuts[j]&nbsp;is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127],"tags":[613,284,177],"class_list":["post-6857","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-diff","tag-geometry","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6857"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6857\/revisions"}],"predecessor-version":[{"id":6859,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6857\/revisions\/6859"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}