class Solution(object):
def minMeetingRooms(self, intervals):
"""
:type intervals: List[Interval]
:rtype: int
"""
# Keep start and end times as Events and sort them by event timestamp
events = []
for i in intervals:
events.append((i.start, True))
events.append((i.end, False))
events.sort()
nrooms = 0
maxrooms = 0
for ev,start in events:
if start:
nrooms += 1
if nrooms > maxrooms:
maxrooms = nrooms
else:
nrooms = 1
return maxrooms
sripanyam
@sripanyam
Posts made by sripanyam

Python solution by sorting start and end events

Similar to Spiral Matrix I  Using a list of unit vectors for current and next directions
class Solution(object): def generateMatrix(self, n): """ :type n: int :rtype: List[List[int]] """ EAST = 0 SOUTH = 1 WEST = 2 NORTH = 3 height = width = n dirs = [[0,1],[1,0],[0,1],[1,0]] currdir = EAST currdim = 0 dims = [width,height] pos = [0,1] print "Dims: ", dims, currdir index = 1 out = [ [0] * width for i in xrange(0, height) ] while dims[0] and dims[1]: currvector = dirs[currdir] # print "CurrDir, CurrVector, dims[currdir]: ", currdir, currvector, dims[currdim] for i in xrange(0, dims[currdim]): pos[0],pos[1] = pos[0] + currvector[0], pos[1] + currvector[1] # print "Pos: ", pos out[pos[0]][pos[1]] = index index += 1 currdir = (currdir + 1) % 4 currdim = (currdim + 1) % 2 dims[currdim] = 1 return out

Python  Use vectors to describe "next point" in traversal
Effectively we maintain 4 unit vectors  one for each direction (North, East, South, West) and starting from a given point (in this case 0,1), we move in each direction and update the position based on the magnitude of the vector. Also for each of the directions we update the remaining value in each dimension (ie width or height) until all values are exhausted.
class Solution(object): def spiralOrder(self, matrix): """ :type matrix: List[List[int]] :rtype: List[int] """ EAST = 0 SOUTH = 1 WEST = 2 NORTH = 3 height = len(matrix) if height == 0: return [] width = len(matrix[0]) dirs = [[0,1],[1,0],[0,1],[1,0]] currdir = EAST currdim = 0 out = [ ] dims = [width,height] pos = [0,1] print "Dims: ", dims, currdir while dims[0] and dims[1]: currvector = dirs[currdir] # print "CurrDir, CurrVector, dims[currdir]: ", currdir, currvector, dims[currdim] for i in xrange(0, dims[currdim]): pos[0],pos[1] = pos[0] + currvector[0], pos[1] + currvector[1] # print "Pos: ", pos out.append(matrix[pos[0]][pos[1]]) currdir = (currdir + 1) % 4 currdim = (currdim + 1) % 2 # Reduce the number of entries in the next dimension by one dims[currdim] = 1 return out

RE: "You have attempted to resubmit too soon" errors happening all the time
That's right. I just found out the same. I guess it helps to spread out practise through out the year instead of just the days before an interview :)

"You have attempted to resubmit too soon" errors happening all the time
Hey Guys
I coudlnt find a site wide contactus so wanted to see if anybody here could help. Basically I have been preparing whole of yesterday and last night I started seeing the "You have attempted to submit too soon" error on about 5 problems and it hasnt recovered since. This was last night (about 10 hours ago) and I am still getting this error when I try to submit. I am not sure what else to do. Any ideas?
cheers
Sri