-
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 #596 from 0xff-dev/542
Add solution and test-cases for problem 542
- Loading branch information
Showing
5 changed files
with
54 additions
and
23 deletions.
There are no files selected for viewing
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.
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,37 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
import "math" | ||
|
||
func Solution(mat [][]int) [][]int { | ||
rows := len(mat) | ||
cols := len(mat[0]) | ||
zeroPoints := make([][2]int, 0) | ||
for r := 0; r < rows; r++ { | ||
for c := 0; c < cols; c++ { | ||
if mat[r][c] == 0 { | ||
zeroPoints = append(zeroPoints, [2]int{r, c}) | ||
continue | ||
} | ||
mat[r][c] = math.MaxInt | ||
} | ||
} | ||
var dirs = [][]int{ | ||
{1, 0}, {0, 1}, {-1, 0}, {0, -1}, | ||
} | ||
for len(zeroPoints) > 0 { | ||
next := make([][2]int, 0) | ||
for _, item := range zeroPoints { | ||
for _, dir := range dirs { | ||
nx, ny := item[0]+dir[0], item[1]+dir[1] | ||
if nx < 0 || nx >= rows || ny < 0 || ny >= cols || mat[nx][ny] <= mat[item[0]][item[1]]+1 { | ||
continue | ||
} | ||
mat[nx][ny] = mat[item[0]][item[1]] + 1 | ||
next = append(next, [2]int{nx, ny}) | ||
} | ||
} | ||
zeroPoints = next | ||
} | ||
return mat | ||
|
||
} |
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