{"id":8820,"date":"2021-11-27T09:42:32","date_gmt":"2021-11-27T17:42:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8820"},"modified":"2021-11-27T12:21:44","modified_gmt":"2021-11-27T20:21:44","slug":"leetcode-2088-count-fertile-pyramids-in-a-land","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2088-count-fertile-pyramids-in-a-land\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2088. Count Fertile Pyramids in a Land"},"content":{"rendered":"\n<p>A farmer has a&nbsp;<strong>rectangular grid<\/strong>&nbsp;of land with&nbsp;<code>m<\/code>&nbsp;rows and&nbsp;<code>n<\/code>&nbsp;columns that can be divided into unit cells. Each cell is either&nbsp;<strong>fertile<\/strong>&nbsp;(represented by a&nbsp;<code>1<\/code>) or&nbsp;<strong>barren<\/strong>&nbsp;(represented by a&nbsp;<code>0<\/code>). All cells outside the grid are considered barren.<\/p>\n\n\n\n<p>A&nbsp;<strong>pyramidal plot<\/strong>&nbsp;of land can be defined as a set of cells with the following criteria:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The number of cells in the set has to be&nbsp;<strong>greater than&nbsp;<\/strong><code>1<\/code>&nbsp;and all cells must be&nbsp;<strong>fertile<\/strong>.<\/li><li>The&nbsp;<strong>apex<\/strong>&nbsp;of a pyramid is the&nbsp;<strong>topmost<\/strong>&nbsp;cell of the pyramid. The&nbsp;<strong>height<\/strong>&nbsp;of a pyramid is the number of rows it covers. Let&nbsp;<code>(r, c)<\/code>&nbsp;be the apex of the pyramid, and its height be&nbsp;<code>h<\/code>. Then, the plot comprises of cells&nbsp;<code>(i, j)<\/code>&nbsp;where&nbsp;<code>r &lt;= i &lt;= r + h - 1<\/code>&nbsp;<strong>and<\/strong>&nbsp;<code>c - (i - r) &lt;= j &lt;= c + (i - r)<\/code>.<\/li><\/ol>\n\n\n\n<p>An&nbsp;<strong>inverse pyramidal plot<\/strong>&nbsp;of land can be defined as a set of cells with similar criteria:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The number of cells in the set has to be&nbsp;<strong>greater than&nbsp;<\/strong><code>1<\/code>&nbsp;and all cells must be&nbsp;<strong>fertile<\/strong>.<\/li><li>The&nbsp;<strong>apex<\/strong>&nbsp;of an inverse pyramid is the&nbsp;<strong>bottommost<\/strong>&nbsp;cell of the inverse pyramid. The&nbsp;<strong>height<\/strong>&nbsp;of an inverse pyramid is the number of rows it covers. Let&nbsp;<code>(r, c)<\/code>&nbsp;be the apex of the pyramid, and its height be&nbsp;<code>h<\/code>. Then, the plot comprises of cells&nbsp;<code>(i, j)<\/code>&nbsp;where&nbsp;<code>r - h + 1 &lt;= i &lt;= r<\/code>&nbsp;<strong>and<\/strong>&nbsp;<code>c - (r - i) &lt;= j &lt;= c + (r - i)<\/code>.<\/li><\/ol>\n\n\n\n<p>Some examples of valid and invalid pyramidal (and inverse pyramidal) plots are shown below. Black cells indicate fertile cells.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/11\/08\/image.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;<code>m x n<\/code>&nbsp;binary matrix&nbsp;<code>grid<\/code>&nbsp;representing the farmland, return&nbsp;<em>the&nbsp;<strong>total number<\/strong>&nbsp;of pyramidal and inverse pyramidal plots that can be found in<\/em>&nbsp;<code>grid<\/code>.<\/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\/2021\/10\/23\/eg11.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/23\/exa12.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/23\/exa13.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[0,1,1,0],[1,1,1,1]]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nThe 2 possible pyramidal plots are shown in blue and red respectively.\nThere are no inverse pyramidal plots in this grid.&nbsp;\nHence total number of pyramidal and inverse pyramidal plots is 2 + 0 = 2.\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\/2021\/10\/23\/eg21.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/23\/exa22.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/23\/exa23.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,1,1],[1,1,1]]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nThe pyramidal plot is shown in blue, and the inverse pyramidal plot is shown in red.&nbsp;\nHence the total number of plots is 1 + 1 = 2.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/23\/eg3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,0,1],[0,0,0],[1,0,1]]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong>\nThere are no pyramidal or inverse pyramidal plots in the grid.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/23\/eg41.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/23\/eg42.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/23\/eg43.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/23\/eg44.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,1,1,1,0],[1,1,1,1,1],[1,1,1,1,1],[0,1,0,0,1]]\n<strong>Output:<\/strong> 13\n<strong>Explanation:<\/strong>\nThere are 7 pyramidal plots, 3 of which are shown in the 2nd and 3rd figures.\nThere are 6 inverse pyramidal plots, 2 of which are shown in the last figure.\nThe total number of plots is 7 + 6 = 13.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == grid.length<\/code><\/li><li><code>n == grid[i].length<\/code><\/li><li><code>1 &lt;= m, n &lt;= 1000<\/code><\/li><li><code>1 &lt;= m * n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>grid[i][j]<\/code>&nbsp;is either&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Let dp[i][j] be the height+1 of a Pyramid tops at i, j<br>dp[i][j] = min(dp[i+d][j &#8211; 1], dp[i + d][j + 1]) + 1 if dp[i-1][j] else grid[i][j] <\/p>\n\n\n\n<p>Time complexity: O(mn)<br>Space complexity: O(mn)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def countPyramids(self, grid: List[List[int]]) -> int:\n    m = len(grid)\n    n = len(grid[0])\n    ans = 0\n\n    @cache\n    def dp(i: int, j: int, d: int) -> int:\n      if grid[i][j] and 0 <= i + d < m and 0 < j < n - 1 and grid[i + d][j]:\n        return min(dp(i + d, j - 1, d), dp(i + d, j + 1, d)) + 1\n      return grid[i][j]\n    \n    for i, j in product(range(m), range(n)):\n      ans += max(0, dp(i, j, 1) - 1)\n      ans += max(0, dp(i, j, -1) - 1)\n    \n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A farmer has a&nbsp;rectangular grid&nbsp;of land with&nbsp;m&nbsp;rows and&nbsp;n&nbsp;columns that can be divided into unit cells. Each cell is either&nbsp;fertile&nbsp;(represented by a&nbsp;1) or&nbsp;barren&nbsp;(represented by a&nbsp;0). All&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,217],"class_list":["post-8820","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8820","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=8820"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8820\/revisions"}],"predecessor-version":[{"id":8824,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8820\/revisions\/8824"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}