[Leetcode 172] Factoring Trailing Zeros

이재윤·2025년 2월 11일

https://leetcode.com/problems/factorial-trailing-zeroes/

1) 코드

class Solution:
    def trailingZeroes(self, n: int) -> int:
        
        twoCnt = 0
        fiveCnt = 0 

        num1 = 2
        num2 = 5

        while True:
            tmp = n // num1

            if tmp == 0:
                break
            else:
                twoCnt += tmp
                num1 *= 2

        while True:
            tmp = n // num2

            if tmp == 0:
                break
            else:
                fiveCnt += tmp
                num2 *= 5

        return min(twoCnt, fiveCnt)

2) 해설

  • Factorial에 곱해진 2와 5의 개수를 구하고,
    그 중에 최소 개수를 구하면 되는 문제이다.

0개의 댓글