TLE for python code, please help fix


  • 0
    R
    class Solution(object):
        def read(self, buf, n):
            buf4 = ['' for i in range(4)]
            if n <= 4:
                charsRead = read4(buf4)
                buf = buf4
                return charsRead
            charsLeft = n
            charsRead = 0
            while charsLeft > 4:
                nchars = read4(buf4)
                buf += buf4
                charsLeft -= nchars
                charsRead += nchars
            charsRead += read4(buf4)
            buf += buf4
            return charsRead
    

    where can I optimize?


  • 0
    S

    There's an infinite loop in the "while". charsLeft will not go below 4 when there's not enough to read.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.