Forum

Post 05.06.2009   # 1
Subject Stupid mistakes
I chose this topic so that all competitors can publish their "stupid" mistakes that they made during some of the codefu competitions. Some of the mistakes could be "I used int instead of long" etc... We all make mistakes so one of the purposes of this topic is to learn from them.
igorkulev is offline Reply
Post 05.06.2009   # 2
I'll start first. I wrote the dynamic programming solution for the 500 on the codefu final, but i had some bug... I always got some number -2147483648. Analizing the code at first i saw that only operations i use is Math.min and Math... I wondered why that result, as time was running out i became more and more nervous and didn't see the stupid bug.

I initialize the three dimensional matrix with Integer.MAX_VALUE

In the line:

opt[i][b][d] = Math.min(opt[i][b][d], opt[i-1][a][m]+pom);


I indirectly used the operation "sum" and caused overflow That is why i got that "strange" change from Integer.MAX_VALUE to Integer.MIN_VALUE. After the competition i didn't like to try again this problem because i still didn't know what was the problem with my code, but i rewrote the code now and discovered the bug. The thing is bigger because i tested my "repaired" solution to discover that my solution gives correct answers on all test cases... I learned one more lesson from this, always check for overflow, and before using extreme values like MAX_VALUE or MIN_VALUE first check if you make some "illegal" action with the variable which owns that value.
igorkulev is offline Reply

Please login to post reply.