-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #899 from 0xff-dev/778
Add solution and test-cases for problem 778
- Loading branch information
Showing
5 changed files
with
77 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
func dfs778(grid [][]int, visited [][]bool, x, y, t int) bool { | ||
if x < 0 || x >= len(grid) || y < 0 || y >= len(grid) || visited[x][y] || grid[x][y] > t { | ||
return false | ||
} | ||
if x == len(grid)-1 && y == len(grid)-1 { | ||
return true | ||
} | ||
visited[x][y] = true | ||
return dfs778(grid, visited, x-1, y, t) || | ||
dfs778(grid, visited, x+1, y, t) || | ||
dfs778(grid, visited, x, y-1, t) || | ||
dfs778(grid, visited, x, y+1, t) | ||
|
||
} | ||
func do778(grid [][]int, x, y, t int) bool { | ||
gridCopy := make([][]int, len(grid)) | ||
v := make([][]bool, len(grid)) | ||
for i := 0; i < len(grid); i++ { | ||
gridCopy[i] = make([]int, len(grid)) | ||
v[i] = make([]bool, len(grid)) | ||
copy(gridCopy[i], grid[i]) | ||
} | ||
|
||
return dfs778(gridCopy, v, x, y, t) | ||
} | ||
func Solution(grid [][]int) int { | ||
|
||
// 一般这种dfs或者bfs都不太好确定的题,可以尝试binary search+dfs | ||
// 直接尝试搜索每个答案 | ||
ans := -1 | ||
l, r := 0, 1<<31-1 | ||
for l <= r { | ||
mid := l + (r-l)/2 | ||
if do778(grid, 0, 0, mid) { | ||
ans = mid | ||
r = mid - 1 | ||
} else { | ||
l = mid + 1 | ||
} | ||
} | ||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.