# Output Limit Exceeded

• My ugly code below (haven't refactored because trying to have the question pass test cases) works as follows. The first color is chosen in the first row, then the program recursively evaluates every possible value that can come from that choice. I use memoization to speed it up. It passes the first 50 or so test cases, and on the 51st it fails due to "Output Limit Exceeded." However--the same test case works on Run Code. Any ideas what's going on? Pretty new to Leetcode..

``````# @param {Integer[][]} costs
# @return {Integer}

@@memo = {}
def min_cost(costs)
@@memo = {}
return nil if costs.nil?
return 0 if costs.empty?
[costs[0][0] + cost_of(costs, 1, 0), costs[0][1] + cost_of(costs, 1, 1), costs[0][2] + cost_of(costs, 1, 2)].min
end

def cost_of(costs, house_num, prev_color_index)
return 0 if house_num >= costs.length
if prev_color_index == 0
@@memo["#{house_num}#{prev_color_index}"] ||= [costs[house_num][1] + cost_of(costs, house_num+1, 1), costs[house_num][2] + cost_of(costs, house_num+1, 2)].min
elsif prev_color_index == 1
@@memo["#{house_num}#{prev_color_index}"] ||= [costs[house_num][0] + cost_of(costs, house_num+1, 0), costs[house_num][2] + cost_of(costs, house_num+1, 2)].min
else
@@memo["#{house_num}#{prev_color_index}"] ||= [costs[house_num][0] + cost_of(costs, house_num+1, 0), costs[house_num][1] + cost_of(costs, house_num+1, 1)].min
end
end
``````

• Don't print any output; just return the answer.

• @Avidiax What print statement are you referring to?

• @srekhi

You may also be returning the wrong type.

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