Leetcode 2481. Minimum Cuts to Divide a Circle
You are given a perfect circle. You need to determine the minimum number of cuts required to divide this circle into exactly n
equal segments. A cut is defined as a straight line that passes through the center of the circle.
4
Output:
2
n
be less than or equal to 0?
n
should be 1. Considering lesser values would be nonsensical in this context.n
?
n
guaranteed to be a positive integer?
n
should be a positive integer given the problem context.To identify the minimum number of cuts needed:
n
is 1:
n
is 2
:
n
greater than 2
:
n
is even: Each cut through the center can divide the circle into two additional segments. Thus, n / 2
straight cuts will generate n
segments.n
is odd: Similarly, n
odd segments can be achieved optimally with n
cuts, as one cut whenever placed still requires a separate consideration.Based on these observations, the formula can be generalized:
n
segments, the number of cuts required is ceil(n / 2)
, or equivalently (n + 1) // 2
in integer arithmetic.public class MinimumCutsCircle {
public static int minimumCuts(int n) {
if (n == 1) {
return 0;
}
return (n + 1) / 2; // This handles both odd and even n appropriately
}
public static void main(String[] args) {
System.out.println(minimumCuts(1)); // Output: 0
System.out.println(minimumCuts(2)); // Output: 1
System.out.println(minimumCuts(3)); // Output: 2
System.out.println(minimumCuts(4)); // Output: 2
System.out.println(minimumCuts(5)); // Output: 3
}
}
The time complexity of this solution is O(1) as we are performing a constant-time calculation regardless of the input size. It’s a direct arithmetic operation with a conditional check, thus very efficient in terms of computation.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?