Skip to content

Commit 14703ff

Browse files
committed
Completed 1368. Minimum Cost to Make at Least One Valid Path in a Grid
1 parent 4c252a5 commit 14703ff

File tree

2 files changed

+133
-2
lines changed

2 files changed

+133
-2
lines changed
Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,83 @@
1-
package minimum_cost_to_make_at_least_one_valid_path_in_a_grid
1+
package minimum_cost_to_make_at_least_one_valid_path_in_a_grid
2+
3+
import (
4+
"container/heap"
5+
"math"
6+
)
7+
8+
func minCost(grid [][]int) int {
9+
directions := [][]int {
10+
{0, 1},
11+
{0, -1},
12+
{1, 0},
13+
{-1, 0},
14+
}
15+
16+
ROWS := len(grid)
17+
COLS := len(grid[0])
18+
19+
minCost := make([][]int, ROWS)
20+
21+
for r := range minCost{
22+
minCost[r] = make([]int, COLS)
23+
for c := range minCost[r] {
24+
minCost[r][c] = math.MaxInt
25+
}
26+
}
27+
minCost[0][0] = 0
28+
29+
q := &PriorityQueue{}
30+
heap.Init(q)
31+
heap.Push(q, &Node{row: 0, col: 0, cost: 0})
32+
33+
for q.Len() > 0 {
34+
current := heap.Pop(q).(*Node)
35+
row, col, currCost := current.row, current.col, current.cost
36+
37+
if currCost > minCost[row][col] {
38+
continue
39+
}
40+
41+
for dirIdx, dir := range directions {
42+
dr, dc := dir[0], dir[1]
43+
newRow, newCol := row+dr, col+dc
44+
45+
cost := 0
46+
if grid[row][col] != dirIdx+1 {
47+
cost = 1
48+
}
49+
50+
if newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS &&
51+
currCost+cost < minCost[newRow][newCol] {
52+
minCost[newRow][newCol] = currCost + cost
53+
if cost == 1 {
54+
heap.Push(q, &Node{row: newRow, col: newCol, cost: currCost + cost})
55+
} else {
56+
heap.Push(q, &Node{row: newRow, col: newCol, cost: currCost})
57+
}
58+
}
59+
}
60+
}
61+
62+
return minCost[ROWS -1][COLS -1]
63+
}
64+
65+
66+
type Node struct {
67+
row, col int
68+
cost int
69+
}
70+
71+
type PriorityQueue []*Node
72+
73+
func (pq PriorityQueue) Len() int { return len(pq) }
74+
func (pq PriorityQueue) Less(i, j int) bool { return pq[i].cost < pq[j].cost }
75+
func (pq PriorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
76+
func (pq *PriorityQueue) Push(x interface{}) { *pq = append(*pq, x.(*Node)) }
77+
func (pq *PriorityQueue) Pop() interface{} {
78+
old := *pq
79+
n := len(old)
80+
item := old[n-1]
81+
*pq = old[:n-1]
82+
return item
83+
}
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
package minimum_cost_to_make_at_least_one_valid_path_in_a_grid
1+
package minimum_cost_to_make_at_least_one_valid_path_in_a_grid
2+
3+
import "testing"
4+
5+
6+
func TestMinCost(t *testing.T) {
7+
tests := []struct {
8+
name string
9+
grid [][]int
10+
expectedResult int
11+
} {
12+
{
13+
name: "Test Case 1",
14+
grid: [][]int {
15+
{1,1,1,1},
16+
{2,2,2,2},
17+
{1,1,1,1},
18+
{2,2,2,2},
19+
},
20+
expectedResult: 3,
21+
},
22+
{
23+
name: "Test Case 2",
24+
grid: [][]int {
25+
{1,1,3},
26+
{3,2,2},
27+
{1,1,4},
28+
},
29+
expectedResult: 0,
30+
},
31+
{
32+
name: "Test Case 3",
33+
grid: [][]int {
34+
{1,2},
35+
{4,3},
36+
},
37+
expectedResult: 1,
38+
},
39+
}
40+
41+
42+
for _, test := range tests {
43+
t.Run(test.name, func(t *testing.T) {
44+
got := minCost(test.grid)
45+
if got != test.expectedResult {
46+
t.Errorf("minCost(%v) = %d; want = %d", test.grid, got, test.expectedResult)
47+
}
48+
})
49+
}
50+
}

0 commit comments

Comments
 (0)