前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >包子培训 leetcode solution 2320 Count Number of Ways to Place Houses

包子培训 leetcode solution 2320 Count Number of Ways to Place Houses

作者头像
包子面试培训
发布2022-11-11 14:56:04
2810
发布2022-11-11 14:56:04
举报
文章被收录于专栏:包子铺里聊IT包子铺里聊IT

Leetcode solution 2320. Count Number of Ways to Place Houses

Blogger:https://blog.baozitraining.org/2022/06/leetcode-solution-2304-minimum-path.html

Youtube: https://www.youtube.com/shorts/eV0kLfZuYZE

B站: https://b23.tv/CN3wUVx

博客园: https://www.cnblogs.com/baozitraining/p/16464560.html

Cover credit: Photo: Art Shutterstock; Mike Sullivan

Problem Statement

There is a street with n * 2 plots, where there are n plots on each side of the street. The plots on each side are numbered from 1 to n. On each plot, a house can be placed.

Return the number of ways houses can be placed such that no two houses are adjacent to each other on the same side of the street. Since the answer may be very large, return it modulo 109 + 7.

Note that if a house is placed on the ith plot on one side of the street, a house can also be placed on the ith plot on the other side of the street.

Example 1:

代码语言:javascript
复制
Input: n = 1Output: 4Explanation: Possible arrangements:
1. All plots are empty.
2. A house is placed on one side of the street.
3. A house is placed on the other side of the street.
4. Two houses are placed, one on each side of the street.

Example 2:

代码语言:javascript
复制
Input: n = 2Output: 9Explanation: The 9 possible arrangements are shown in the diagram above.

Constraints:

  • 1 <= n <= 104

Problem link

Video Tutorial

You can find the detailed video tutorial here

  • Youtube
  • B站

Thought Process

A classic DP problem (Dynamic Programming) because it's either ask you a boolean yes or no questions, Or ask for extreme values, e.g., min, max, or just a number of solutions etc.

  • Two sides are independent, each side is similar to Climb Stairs, House Robber House Robber II
  • Given each side is independent, the combo would be multiplied together.
  • Implementation wise
    • could use two variables to replace the array
    • be careful about overflow (use long to cast back to int)

Solutions

代码语言:javascript
复制
public int countHousePlacements(int n) {
  if (n < 0) {
    return -1;
  }
  long divider = (long)(Math.pow(10, 9) + 7);
  // could be replaced by two variables, similar to climb stairs, so space is O(1) as opposed to O(N)
  long[] result = new long[n + 1];
  result[0] = 1;
  result[1] = 2;
  for (int i = 2; i <= n; i++) {
    result[i] = result[i - 1]  % divider + result[i - 2] % divider;
  }
  return (int)(result[n] * result[n] % divider);
}

Time Complexity: O(N) since going through each number till n

Space Complexity: O(1) if we use two variables to track, the above implementation is O(N) since an extra array is used.

References

  • House Robber
  • House Robber II
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-07-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 包子铺里聊IT 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Statement
  • Video Tutorial
  • Thought Process
  • Solutions
  • References
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档