wu :: forums
« wu :: forums - Maximum zeros »

Welcome, Guest. Please Login or Register.
Nov 28th, 2024, 3:53am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: ThudnBlunder, Grimbal, Eigenray, william wu, Icarus, SMQ, towr)
   Maximum zeros
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Maximum zeros  (Read 437 times)
mad
Junior Member
**





   


Posts: 118
Maximum zeros  
« on: Mar 8th, 2008, 5:39pm »
Quote Quote Modify Modify

You are given a NXN matrix, each element of which is either 0 or 1 with the restriction that no 0 can appear before a 1 in any row. Example-
 
1 0 0 0 0
1 1 0 0 0
1 0 0 0 0
0 0 0 0 0
1 1 1 1 1
 
Assuming the row and col indices to be zero based, you have to return the index of the row with maximum number of zero's.
 
Achieve:
Space- O(1)
Time- O(n)  
 
I think a similar question has been posted before.
But, that required checking each row making it O(n^2). Can there be an O(n) solution by say checking the diagonal(not sure).
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Maximum zeros  
« Reply #1 on: Mar 9th, 2008, 3:11am »
Quote Quote Modify Modify


1: start at the top right
2: move left until you meet the first one or leave the bound of the matrix  
3: if you made at least one step left save the index of the row
4: move down until you meet the first zero or leave the bounds of matrix
5: goto 2 unless you left bounds of the matrix
6: return the saved row index
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board