def searchMatrix(matrix, target): for row in matrix: if row[-1] is target: return True elif row[-1] > target: for elem in row: if elem is target: return True return False matrix = [[-10],[-7],[-5]] target = -10 print searchMatrix(matrix, target)
This is the code I have on my local computer. OJ has the same thing excluding the
matrix = [[-10],[-7],[-5]] target = -10 print searchMatrix(matrix, target)
On OJ, my output is False, however on my local computer it is True. Anyone know why?
I tried this on OJ too, it failed at another example which is the one that reaaaally long. Something like "[[-8,-6,-5,-4,-2,-1,-1,0,2,4,5,7,7,7,7,9,9,9,9,11],[12,14,15,16,18,20,20,20,21,21,22,23,23,25,25,25,26,27,29,30],[31,31,32,32,33,35,37,39,39,39,40,41,43,44,46,48,48,48,48,50],......."
And on my computer it seems good.
I'm also curious about why this happened
I tried the same code in my machine and it goes with OJ's output. One issue i see in ur code is that the boundary condition is not rigid ie row[-1] > target should be modified to below to isolate the data row :
if row <= target and row[-1] >= target:
then search for data
Also if i were you i would do a binary search on the whole matrix or atleast within a data row so that the run times comes down. Ofcourse for practice you could start with your brute force and then expand to the suggested solution (interviewers often look for the most efficient solution )