头条算法题,,就做出两个,,泪目
头条就做出来两个。。。。
贴一下:
第一题:
(m,n) =map(lambda x:int(x), raw_input().split(',')) seats = [] for i in range(m): seats.append(map(lambda x:int(x),raw_input().split(','))) maxGroupn = 0; Groupn = 0; def search(i,j): if i<0 or j<0 or i>=m or j>=n or seats[i][j]<=0: return 0 seats[i][j]=-1; count=1+search(i+1,j)+search(i-1,j)+search(i,j-1)+search(i,j+1)+search(i-1,j-1)+search(i-1,j+1)+search(i+1,j-1)+search(i+1,j+1) return count; for i in range(m): for j in range(n): curGroupn = search(i,j) if curGroupn>0: Groupn+=1 if curGroupn > maxGroupn: maxGroupn = curGroupn print str(Groupn)+','+str(maxGroupn)
第二题:
m = input() words = [] for i in range(m): words += map(lambda item:map(lambda x:int(x),item.split(',')),raw_input().split(';')) words.sort(lambda t1,t2:t1[0]-t2[0]) i=1 while i < len(words): if i>0 and words[i][0]<=words[i-1][1]: if words[i][1] > words[i-1][1]: words[i-1][1] = words[i][1] del words[i] else: i+=1 strn = "" for k1,k2 in words: strn += str(k1)+','+str(k2)+";" print strn[:-1]
#笔试题目#