返回介绍

solution / 2400-2499 / 2481.Minimum Cuts to Divide a Circle / README_EN

发布于 2024-06-17 01:03:05 字数 3968 浏览 0 评论 0 收藏 0

2481. Minimum Cuts to Divide a Circle

中文文档

Description

A valid cut in a circle can be:

  • A cut that is represented by a straight line that touches two points on the edge of the circle and passes through its center, or
  • A cut that is represented by a straight line that touches one point on the edge of the circle and its center.

Some valid and invalid cuts are shown in the figures below.

Given the integer n, return _the minimum number of cuts needed to divide a circle into _n_ equal slices_.

 

Example 1:

Input: n = 4
Output: 2
Explanation: 
The above figure shows how cutting the circle twice through the middle divides it into 4 equal slices.

Example 2:

Input: n = 3
Output: 3
Explanation:
At least 3 cuts are needed to divide the circle into 3 equal slices. 
It can be shown that less than 3 cuts cannot result in 3 slices of equal size and shape.
Also note that the first cut will not divide the circle into distinct parts.

 

Constraints:

  • 1 <= n <= 100

Solutions

Solution 1: Case Discussion

  • When $n=1$, no cutting is needed, so the number of cuts is $0$;
  • When $n$ is odd, there is no collinear situation, and at least $n$ cuts are needed;
  • When $n$ is even, they can be collinear in pairs, and at least $\frac{n}{2}$ cuts are needed.

In summary, we can get:

$$ \text{ans} = \begin{cases} n, & n \gt 1 \text{ and } n \text{ is odd} \ \frac{n}{2}, & n \text{ is even} \ \end{cases} $$

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def numberOfCuts(self, n: int) -> int:
    return n if (n > 1 and n & 1) else n >> 1
class Solution {
  public int numberOfCuts(int n) {
    return n > 1 && n % 2 == 1 ? n : n >> 1;
  }
}
class Solution {
public:
  int numberOfCuts(int n) {
    return n > 1 && n % 2 == 1 ? n : n >> 1;
  }
};
func numberOfCuts(n int) int {
  if n > 1 && n%2 == 1 {
    return n
  }
  return n >> 1
}
function numberOfCuts(n: number): number {
  return n > 1 && n & 1 ? n : n >> 1;
}
impl Solution {
  pub fn number_of_cuts(n: i32) -> i32 {
    if n > 1 && n % 2 == 1 {
      return n;
    }
    n >> 1
  }
}
public class Solution {
  public int NumberOfCuts(int n) {
    return n > 1 && n % 2 == 1 ? n : n >> 1;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文