You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LeetCode阶乘尾随零问题:超时原因及时间复杂度确认求助

Troubleshooting Your Trailing Zeroes Submission Timeout

Hey there! Let's unpack what's going on with your submission and break down the time complexity question clearly.

First: Why Your Local Run Works But LeetCode Fails

Your observation that local results match the AC code makes sense—this isn't a logic error, it's almost certainly a timeout issue. LeetCode enforces strict time limits (usually 1-2 seconds for Python solutions), and for extremely large inputs like 1808548329, inefficient algorithms simply can't finish in time, even if they produce the correct result eventually.

Calculating Your Code's Time Complexity

Let's assume your code follows a common (but inefficient) approach: iterating through every number from 1 to n, and counting the number of 5s in each number's prime factors. Here's how to compute its time complexity:

  • Outer loop: You're iterating over n numbers, so that's O(n) operations.
  • Inner factor counting: For each number, you divide by 5 repeatedly until it's no longer divisible. The maximum number of divisions needed for any number ≤n is log₅n (since 5^k ≤n → k = log₅n).
  • Total complexity: Multiplying these gives O(n * logn)—which is way too slow for n = 1.8e9. Even in fast languages, O(n) operations for 1e9 elements take seconds; in Python, it would take minutes (if not longer), way beyond LeetCode's limits.

If you optimized the outer loop to only step by 5 (since only multiples of 5 contribute 5 factors), the outer loop becomes O(n/5 + n/25 + n/125 + ...)—a geometric series that sums to O(n) (since the sum converges to n/4). But even O(n) is still impossible for n=1.8e9 in Python.

The Efficient AC Approach (O(logn) Time)

The standard solution for trailing zeroes avoids iterating every number entirely. Since trailing zeroes come from 2*5 pairs, and there are always more 2s than 5s in a factorial, we just need to count the total number of 5 factors across all numbers from 1 to n. This can be done in O(log₅n) time with this logic:

  1. Count how many multiples of 5 are ≤n (each contributes at least one 5).
  2. Add the count of multiples of 25 (each contributes an extra 5).
  3. Add the count of multiples of 125, and so on, until 5^k >n.

Here's what that code looks like:

def trailingZeroes(n):
    count = 0
    while n > 0:
        n = n // 5
        count += n
    return count

For n=1808548329, this loop runs only ~13 times (since 5^13 is about 1.2e9, 5^14 is 6e9 which is larger than the input). It's instant to run, no matter how big n gets.

Wrap-Up

Your original code's time complexity is either O(n) or O(n logn) depending on implementation, both of which are too slow for the largest test cases. Switching to the O(logn) approach will fix the timeout and let you pass all test cases.

内容的提问来源于stack exchange,提问作者ch-yk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:34:59