Skip to content

Commit dc0b67e

Browse files
authored
Merge pull request #2
feat: add minCost solution in Python and Go #3603
2 parents 593e789 + 8e85b73 commit dc0b67e

File tree

4 files changed

+148
-4
lines changed

4 files changed

+148
-4
lines changed

solution/3600-3699/3603.Minimum Cost Path with Alternating Directions II/README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,34 @@ tags:
125125
#### Python3
126126

127127
```python
128-
128+
class Solution:
129+
def minCost(self, m: int, n: int, waitCost: List[List[int]]) -> int:
130+
directions = [(1, 0), (0, 1)] # only down and right
131+
visited = dict()
132+
133+
heap = [(1 * 1, 0, 0, 1)] # (cost, i, j, time)
134+
135+
while heap:
136+
cost, i, j, time = heapq.heappop(heap)
137+
138+
if (i, j, time % 2) in visited and visited[(i, j, time % 2)] <= cost:
139+
continue
140+
visited[(i, j, time % 2)] = cost
141+
142+
if i == m - 1 and j == n - 1:
143+
return cost
144+
145+
if time % 2 == 1: # move step
146+
for dx, dy in directions:
147+
ni, nj = i + dx, j + dy
148+
if 0 <= ni < m and 0 <= nj < n:
149+
next_cost = cost + (ni + 1) * (nj + 1)
150+
heapq.heappush(heap, (next_cost, ni, nj, time + 1))
151+
else: # wait step
152+
next_cost = cost + waitCost[i][j]
153+
heapq.heappush(heap, (next_cost, i, j, time + 1))
154+
155+
return -1
129156
```
130157

131158
#### Java
@@ -143,7 +170,25 @@ tags:
143170
#### Go
144171

145172
```go
146-
173+
func minCost(m int, n int, cost [][]int) int64 {
174+
dp := make([]int64, n)
175+
for i := 0; i < n; i++ {
176+
dp[i] = int64(i + 1)
177+
}
178+
for i := 1; i < n; i++ {
179+
dp[i] += dp[i-1] + int64(cost[0][i])
180+
}
181+
182+
for y := 1; y < m; y++ {
183+
dp[0] += int64(cost[y][0]) + int64(y+1)
184+
for x := 1; x < n; x++ {
185+
enter := int64(y+1) * int64(x+1)
186+
dp[x] = min(dp[x], dp[x-1]) + int64(cost[y][x]) + enter
187+
}
188+
}
189+
190+
return dp[n-1] - int64(cost[m-1][n-1])
191+
}
147192
```
148193

149194
<!-- tabs:end -->

solution/3600-3699/3603.Minimum Cost Path with Alternating Directions II/README_EN.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,34 @@ tags:
123123
#### Python3
124124

125125
```python
126-
126+
class Solution:
127+
def minCost(self, m: int, n: int, waitCost: List[List[int]]) -> int:
128+
directions = [(1, 0), (0, 1)] # only down and right
129+
visited = dict()
130+
131+
heap = [(1 * 1, 0, 0, 1)] # (cost, i, j, time)
132+
133+
while heap:
134+
cost, i, j, time = heapq.heappop(heap)
135+
136+
if (i, j, time % 2) in visited and visited[(i, j, time % 2)] <= cost:
137+
continue
138+
visited[(i, j, time % 2)] = cost
139+
140+
if i == m - 1 and j == n - 1:
141+
return cost
142+
143+
if time % 2 == 1: # move step
144+
for dx, dy in directions:
145+
ni, nj = i + dx, j + dy
146+
if 0 <= ni < m and 0 <= nj < n:
147+
next_cost = cost + (ni + 1) * (nj + 1)
148+
heapq.heappush(heap, (next_cost, ni, nj, time + 1))
149+
else: # wait step
150+
next_cost = cost + waitCost[i][j]
151+
heapq.heappush(heap, (next_cost, i, j, time + 1))
152+
153+
return -1
127154
```
128155

129156
#### Java
@@ -141,7 +168,25 @@ tags:
141168
#### Go
142169

143170
```go
144-
171+
func minCost(m int, n int, cost [][]int) int64 {
172+
dp := make([]int64, n)
173+
for i := 0; i < n; i++ {
174+
dp[i] = int64(i + 1)
175+
}
176+
for i := 1; i < n; i++ {
177+
dp[i] += dp[i-1] + int64(cost[0][i])
178+
}
179+
180+
for y := 1; y < m; y++ {
181+
dp[0] += int64(cost[y][0]) + int64(y+1)
182+
for x := 1; x < n; x++ {
183+
enter := int64(y+1) * int64(x+1)
184+
dp[x] = min(dp[x], dp[x-1]) + int64(cost[y][x]) + enter
185+
}
186+
}
187+
188+
return dp[n-1] - int64(cost[m-1][n-1])
189+
}
145190
```
146191

147192
<!-- tabs:end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func minCost(m int, n int, cost [][]int) int64 {
2+
dp := make([]int64, n)
3+
for i := 0; i < n; i++ {
4+
dp[i] = int64(i + 1)
5+
}
6+
for i := 1; i < n; i++ {
7+
dp[i] += dp[i-1] + int64(cost[0][i])
8+
}
9+
10+
for y := 1; y < m; y++ {
11+
dp[0] += int64(cost[y][0]) + int64(y+1)
12+
for x := 1; x < n; x++ {
13+
enter := int64(y+1) * int64(x+1)
14+
dp[x] = min64(dp[x], dp[x-1]) + int64(cost[y][x]) + enter
15+
}
16+
}
17+
18+
return dp[n-1] - int64(cost[m-1][n-1])
19+
}
20+
21+
func min64(a, b int64) int64 {
22+
if a < b {
23+
return a
24+
}
25+
return b
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def minCost(self, m: int, n: int, waitCost: List[List[int]]) -> int:
3+
directions = [(1, 0), (0, 1)] # only down and right
4+
visited = dict()
5+
6+
heap = [(1 * 1, 0, 0, 1)] # (cost, i, j, time)
7+
8+
while heap:
9+
cost, i, j, time = heapq.heappop(heap)
10+
11+
if (i, j, time % 2) in visited and visited[(i, j, time % 2)] <= cost:
12+
continue
13+
visited[(i, j, time % 2)] = cost
14+
15+
if i == m - 1 and j == n - 1:
16+
return cost
17+
18+
if time % 2 == 1: # move step
19+
for dx, dy in directions:
20+
ni, nj = i + dx, j + dy
21+
if 0 <= ni < m and 0 <= nj < n:
22+
next_cost = cost + (ni + 1) * (nj + 1)
23+
heapq.heappush(heap, (next_cost, ni, nj, time + 1))
24+
else: # wait step
25+
next_cost = cost + waitCost[i][j]
26+
heapq.heappush(heap, (next_cost, i, j, time + 1))
27+
28+
return -1

0 commit comments

Comments
 (0)