-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.py
55 lines (39 loc) · 1.48 KB
/
2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from collections import defaultdict
import sys
sys.setrecursionlimit(10**5)
def solution(maps):
answer = 0
N = len(maps)
M = len(maps[0])
finalDict = defaultdict(int)
visited = [[ 0 for _ in range(M)]for _ in range(N)]
def DFS(x,y):
visited[x][y]=1
word = maps[x][y]
count[word]+=1
for nx,ny in ([x+1,y],[x-1,y],[x,y+1],[x,y-1]):
if 0<=nx<N and 0<=ny<M:
if maps[nx][ny]!='.' and not visited[nx][ny]:
DFS(nx,ny)
for x in range(N):
for y in range(M):
if maps[x][y]!='.' and not visited[x][y]:
count=defaultdict(int)
DFS(x,y)
tmpW, tmpC = '',0
for word, cnt in count.items():
if cnt>tmpC:
tmpW = word
tmpC = cnt
elif cnt==tmpC:
if ord(tmpW) < ord(word):
tmpW = word
for word, cnt in count.items():
if tmpC > cnt or word==tmpW:
finalDict[tmpW]+=cnt
else:
finalDict[word]+=cnt
finalList = list(finalDict.items())
finalList.sort(key= lambda x :-x[1])
answer = finalList[0][1]
return answer