1 class Solution(object):
 2     def maximumMinimumPath(self, A):
 3         """
 4         :type A: List[List[int]]
 5         :rtype: int
 6         """
 7         dire = [[0,1],[1,0],[-1,0],[0,-1]]
 8         def check(grid,tar):
 9             if grid[0][0] < tar:
10                 return False
11             cur = (0,0)
12             seen = set([(0,0)])
13             def dfs(node):
14                 if node == (len(grid)-1,len(grid[0])-1):
15                     return True
16                 for x,y in dire:
17                     if 0<= x+node[0] < len(A) and 0<=y +node[1] < len(A[0]) and grid[node[0]+x][node[1]+y] >= tar and (node[0]+x,node[1]+y) not in seen:
18                         seen.add((node[0]+x,node[1]+y))
19                         if dfs((node[0]+x,node[1]+y)):
20                             return True
21                 return False
22             return dfs(cur)
23             
24         L = []
25         for row in A:
26             L += [i for i in row if i <= A[-1][-1]]
27         L = set(L)
28         L = sorted(L,reverse = True)
29         
30         low = 0 
31         high = len(L)-1
32         while low < high:
33             idx = (low+high)//2
34             if check(A,L[idx]):
35                 high = idx
36             else:
37                 low = idx + 1
38         return min(L[high],A[0][0])

上面的代码参考:https://leetcode.com/problems/path-with-maximum-minimum-value/discuss/322978/Python-Binary-Search-%2B-DFS

 

我做出来一个TLE的解,只使用了DFS:

 1 class Solution:
 2     def __init__(self):
 3         self.visited = []
 4         self.maxValue = -1
 5 
 6     def dfs(self,A,row,column,i,j,track,direction):
 7         if i == row - 1 and j == column - 1:
 8             minTrack = min(track[:])
 9             self.maxValue = max(self.maxValue,minTrack)
10             return
11         nextdirct = []
12         for dirct in direction:
13             x = i + dirct[0]
14             y = j + dirct[1]
15             if x < 0 or x >= row or y < 0 or y >= column:
16                 continue
17             if self.visited[x][y] == 1:
18                 continue
19             nextdirct.append([A[x][y],x,y])
20         nextdirct = sorted(nextdirct,key=lambda H:[-H[0]])
21         if len(nextdirct) > 0:
22             for d in nextdirct:
23                 x,y = d[1],d[2]
24                 self.visited[x][y] = 1
25                 if A[x][y] < self.maxValue:
26                     return
27                 track.append(A[x][y])
28                 self.dfs(A,row,column,x,y,track,direction)
29                 track.pop(-1)
30                 self.visited[x][y] = 0
31 
32     def maximumMinimumPath(self, A: 'List[List[int]]') -> int:
33         direction = [[0,1],[1,0],[0,-1],[-1,0]]
34         row = len(A)
35         column = len(A[0])
36         track = [A[0][0]]
37         self.visited = [[0 for _ in range(column)] for _ in range(row)]
38         self.visited[0][0] = 1
39         
40         self.dfs(A,row,column,0,0,track,direction)
41         return self.maxValue

 

posted on 2019-06-30 01:33  Sempron2800+  阅读(472)  评论(0编辑  收藏  举报