LeetCode 2481. The minimum number of cuts to divide a circle

【LetMeFly】2481. The minimum number of cuts to divide the circle

Leetcode topic link: https://leetcode.cn/problems/minimum-cuts-to-divide-a-circle/

A valid  is either:

  • The cut is a line segment with two endpoints on the circle that passes through the center of the circle.
  • The cut is a line segment with one end at the center and the other on the circle.

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

Given an integer  n , please return  the minimumn number  of   cuts to cut the circle into equal parts.

 

Example 1:

Input: n = 4
 Output: 2
 Explanation: 
The figure above shows that the circle is cut 2 times to get quarters.

Example 2:

Input: n = 3
 Output: 3
 Explanation: 
At least 3 cuts are required to cut the circle into thirds. 
Fewer than 3 cuts cannot cut the circle into 3 equal parts of equal size and same area. 
At the same time, it can be observed that the first cut cannot cut the circle.

 

hint:

  • 1 <= n <= 100

Method One: Geometry

The meaning of this question is: at least how many lines can be used 直径/半径to make the circle nnn equal parts.

In fact, there are two cases to consider:

  • if nnn is an even number, we use diameter division, each additional diameter, the number of blocks divided into+ 2 +2+2
  • if nnIf n is an odd number, diameter division cannot be used (otherwise equal division cannot be achieved), and each additional radius will divide the number of blocks into12 1212

Special: If the circle is to be divided into 1 pieces, it does not make sense to use a radius to divide (thus returning 0 00

  • Time complexity O ( 1 ) O(1)O(1)
  • Space complexity O ( 1 ) O(1)O(1)

AC code

C++

class Solution {
    
    
public:
    int numberOfCuts(int n) {
    
    
        if (n == 1) {
    
    
            return 0;
        }
        return n % 2 ? n : n / 2;
    }
};

Python

class Solution:
    def numberOfCuts(self, n: int) -> int:
        if n == 1:
            return 0
        return n if n % 2 else n // 2

Published on CSDN at the same time, it is not easy to be original, please attach the link to the original text for reprint ~
Tisfy: https://letmefly.blog.csdn.net/article/details/131257304

Guess you like

Origin blog.csdn.net/Tisfy/article/details/131257304
Recommended