LeetCode 题解工作台

分割圆的最少切割次数

圆内一个 有效切割 ,符合以下二者之一: 该切割是两个端点在圆上的线段,且该线段经过圆心。 该切割是一端在圆心另一端在圆上的线段。 一些有效和无效的切割如下图所示。 给你一个整数 n ,请你返回将圆切割成相等的 n 等分的 最少 切割次数。 示例 1: 输入: n = 4 输出: 2 解释: 上图展…

category

2

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · 数学·结合·几何

bolt

答案摘要

- 当 时,不需要切割,即切割次数为 ; - 当 为奇数时,不存在共线的情况,最少需要 次切割;

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数学·结合·几何 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

圆内一个 有效切割 ,符合以下二者之一:

  • 该切割是两个端点在圆上的线段,且该线段经过圆心。
  • 该切割是一端在圆心另一端在圆上的线段。

一些有效和无效的切割如下图所示。

给你一个整数 n ,请你返回将圆切割成相等的 n 等分的 最少 切割次数。

 

示例 1:

输入:n = 4
输出:2
解释:
上图展示了切割圆 2 次,得到四等分。

示例 2:

输入:n = 3
输出:3
解释:
最少需要切割 3 次,将圆切成三等分。
少于 3 次切割无法将圆切成大小相等面积相同的 3 等分。
同时可以观察到,第一次切割无法将圆切割开。

 

提示:

  • 1 <= n <= 100
lightbulb

解题思路

方法一:分类讨论

  • n=1n=1 时,不需要切割,即切割次数为 00
  • nn 为奇数时,不存在共线的情况,最少需要 nn 次切割;
  • nn 为偶数时,可以两两共线,最少需要 n2\frac{n}{2} 次切割。

综上,可以得到:

ans={n,n>1  n 为奇数n2,n 为其它\textit{ans} = \begin{cases} n, & n \gt 1 \textit{ 且 } n \textit{ 为奇数} \\ \frac{n}{2}, & n \textit{ 为其它} \\ \end{cases}

时间复杂度 O(1)O(1),空间复杂度 O(1)O(1)

1
2
3
4
class Solution:
    def numberOfCuts(self, n: int) -> int:
        return n if (n > 1 and n & 1) else n >> 1
speed

复杂度分析

指标
时间complexity is O(1) because the solution is a single conditional check based on parity. Space complexity is O(1) as no extra data structures are needed.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Asks for the minimum number of cuts for small n like 4 or 5.

  • question_mark

    Hints at separating odd and even n to reduce overcutting.

  • question_mark

    Checks if candidate can explain why fewer cuts fail for odd n.

warning

常见陷阱

外企场景
  • error

    Assuming each cut always doubles slices, which fails for odd n.

  • error

    Ignoring the center-cross requirement, leading to invalid slices.

  • error

    Confusing minimum number of cuts with total slices created.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Determine cuts if slices must be non-overlapping sectors of different sizes.

  • arrow_right_alt

    Find minimum cuts for a circle with concentric rings instead of one layer.

  • arrow_right_alt

    Calculate cuts if the circle can only be cut along specific angles.

help

常见问题

外企场景

分割圆的最少切割次数题解:数学·结合·几何 | LeetCode #2481 简单