|
| 1 | +# 2481. Minimum Cuts to Divide a Circle |
| 2 | +# 🟢 Easy |
| 3 | +# |
| 4 | +# https://leetcode.com/problems/minimum-cuts-to-divide-a-circle/ |
| 5 | +# |
| 6 | +# Tags: Math - Geometry |
| 7 | + |
| 8 | +import timeit |
| 9 | + |
| 10 | + |
| 11 | +# If n == 1, we do not need to cut, otherwise, if the number is uneven |
| 12 | +# we need to make n radial cuts, but if n is uneven, we can make edge |
| 13 | +# to edge cuts, diametral, and we only need n // 2 of them. |
| 14 | +# |
| 15 | +# Time complexity: O(1) |
| 16 | +# Space complexity: O(1) |
| 17 | +# |
| 18 | +# Runtime: 59 ms, faster than 40.00% |
| 19 | +# Memory Usage: 13.9 MB, less than 20.00% |
| 20 | +class Solution: |
| 21 | + def numberOfCuts(self, n: int) -> int: |
| 22 | + if n % 2: |
| 23 | + return n if n != 1 else 0 |
| 24 | + return n // 2 |
| 25 | + |
| 26 | + |
| 27 | +def test(): |
| 28 | + executors = [Solution] |
| 29 | + tests = [ |
| 30 | + [1, 0], |
| 31 | + [3, 3], |
| 32 | + [4, 2], |
| 33 | + ] |
| 34 | + for executor in executors: |
| 35 | + start = timeit.default_timer() |
| 36 | + for _ in range(1): |
| 37 | + for col, t in enumerate(tests): |
| 38 | + sol = executor() |
| 39 | + result = sol.numberOfCuts(t[0]) |
| 40 | + exp = t[1] |
| 41 | + assert result == exp, ( |
| 42 | + f"\033[93m» {result} <> {exp}\033[91m for" |
| 43 | + + f" test {col} using \033[1m{executor.__name__}" |
| 44 | + ) |
| 45 | + stop = timeit.default_timer() |
| 46 | + used = str(round(stop - start, 5)) |
| 47 | + cols = "{0:20}{1:10}{2:10}" |
| 48 | + res = cols.format(executor.__name__, used, "seconds") |
| 49 | + print(f"\033[92m» {res}\033[0m") |
| 50 | + |
| 51 | + |
| 52 | +test() |
0 commit comments