-
Notifications
You must be signed in to change notification settings - Fork 186
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 #1046 from 0xff-dev/2290
Add solution and test-cases for problem 2290
- Loading branch information
Showing
5 changed files
with
85 additions
and
22 deletions.
There are no files selected for viewing
Binary file added
BIN
+13.1 KB
leetcode/2201-2300/2290.Minimum-Obstacle-Removal-to-Reach-Corner/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.79 KB
leetcode/2201-2300/2290.Minimum-Obstacle-Removal-to-Reach-Corner/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 22 additions & 14 deletions
36
leetcode/2201-2300/2290.Minimum-Obstacle-Removal-to-Reach-Corner/README.md
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
58 changes: 57 additions & 1 deletion
58
leetcode/2201-2300/2290.Minimum-Obstacle-Removal-to-Reach-Corner/Solution.go
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,61 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
import "container/heap" | ||
|
||
type heap2290 [][3]int | ||
|
||
func (h *heap2290) Len() int { | ||
return len(*h) | ||
} | ||
|
||
func (h *heap2290) Swap(i, j int) { | ||
(*h)[i], (*h)[j] = (*h)[j], (*h)[i] | ||
} | ||
|
||
func (h *heap2290) Less(i, j int) bool { | ||
return (*h)[i][2] < (*h)[j][2] | ||
} | ||
|
||
func (h *heap2290) Push(x any) { | ||
*h = append(*h, x.([3]int)) | ||
} | ||
|
||
func (h *heap2290) Pop() any { | ||
old := *h | ||
l := len(old) | ||
x := old[l-1] | ||
*h = old[:l-1] | ||
return x | ||
} | ||
|
||
var dirs2290 = [][2]int{ | ||
{0, 1}, {1, 0}, {-1, 0}, {0, -1}, | ||
} | ||
|
||
func Solution(grid [][]int) int { | ||
m, n := len(grid), len(grid[0]) | ||
h := heap2290{{0, 0, 0}} | ||
v := map[[2]int]int{ | ||
[2]int{0, 0}: 0, | ||
} | ||
for h.Len() > 0 { | ||
cur := heap.Pop(&h).([3]int) | ||
if cur[0] == m-1 && cur[1] == n-1 { | ||
return cur[2] | ||
} | ||
for _, dir := range dirs2290 { | ||
nx, ny := cur[0]+dir[0], cur[1]+dir[1] | ||
if !(nx >= 0 && nx < m && ny >= 0 && ny < n) { | ||
continue | ||
} | ||
need := cur[2] + grid[nx][ny] | ||
key := [2]int{nx, ny} | ||
//key = [3]int{nx, ny, need} | ||
if vv, ok := v[key]; !ok || vv > need { | ||
heap.Push(&h, [3]int{nx, ny, need}) | ||
v[key] = need | ||
} | ||
} | ||
} | ||
return -1 | ||
} |
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