Golang solution gets TLE


  • 0
    B

    I wrote a solution in Go that ended up being almost identical to the proposed solution #1. It runs very quickly on my local machine as well as the playground, but somehow always gets TLE when submitting. Code:

    func dailyTemperatures(temperatures []int) []int {
    	max := 0
    	closest := make([]int, 101)
    	for i := range closest {
    		closest[i] = 30002
    	}
    	result := make([]int, len(temperatures))
    
    	for i := len(temperatures) - 1; i >= 0; i-- {
    		currTemp := temperatures[i]
    		if currTemp > max {
    			max = currTemp
    		}
    		closest[currTemp] = i
    		next := 30002
    		for temp := currTemp + 1; temp <= max; temp++ {
    			if next > closest[temp] {
    				next = closest[temp]
    			}
    		}
    		if next != 30002 {
    			result[i] = next - i
    		}
    	}
    
    	return result
    }
    

    Has anyone implemented a solution of this style in Go and not exceeded the time limit?


Log in to reply
 

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