Skip to content

Commit 74b8a69

Browse files
authored
feat: add solutions to lc problem: No.3696 (#4529)
No.3596.Minimum Cost Path with Alternating Directions I
1 parent 5ea879f commit 74b8a69

File tree

9 files changed

+432
-0
lines changed

9 files changed

+432
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3596.Minimum%20Cost%20Path%20with%20Alternating%20Directions%20I/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3596. 最小花费路径交替方向 I 🔒](https://leetcode.cn/problems/minimum-cost-path-with-alternating-directions-i)
10+
11+
[English Version](/solution/3500-3599/3596.Minimum%20Cost%20Path%20with%20Alternating%20Directions%20I/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给定两个整数&nbsp;<code>m</code> 和&nbsp;<code>n</code>&nbsp;分别表示一个网格的行数和列数。</p>
18+
19+
<p>进入单元格&nbsp;<code>(i, j)</code>&nbsp;的花费定义为&nbsp;<code>(i + 1) * (j + 1)</code>。</p>
20+
21+
<p>你在第 1 步时从单元格 <code>(0, 0)</code> 开始。</p>
22+
23+
<p>在每一步,你移动到 <strong>相邻</strong>&nbsp;的单元格,遵循交替的模式:</p>
24+
25+
<ul>
26+
<li>在 <strong>奇数次</strong> 移动,你必须向 <strong>右方</strong> 或 <strong>下方</strong> 移动。</li>
27+
<li>在 <strong>偶数次</strong> 移动,你必须向 <strong>左方</strong> 或 <strong>上方</strong> 移动。</li>
28+
</ul>
29+
30+
<p>返回到达 <code>(m - 1, n - 1)</code>&nbsp;的最小总花费。如果不可能到达,返回 -1。</p>
31+
32+
<p>&nbsp;</p>
33+
34+
<p><strong class="example">示例 1:</strong></p>
35+
36+
<div class="example-block">
37+
<p><strong>输入:</strong><span class="example-io">m = 1, n = 1</span></p>
38+
39+
<p><span class="example-io"><b>输出:</b>1</span></p>
40+
41+
<p><strong>解释:</strong></p>
42+
43+
<ul>
44+
<li>你从单元格&nbsp;<code>(0, 0)</code>&nbsp;开始。</li>
45+
<li>进入&nbsp;<code>(0, 0)</code>&nbsp;的花费是&nbsp;<code>(0 + 1) * (0 + 1) = 1</code>。</li>
46+
<li>由于你已经到达了目标,总花费为 1。</li>
47+
</ul>
48+
</div>
49+
50+
<p><strong class="example">示例 2:</strong></p>
51+
52+
<div class="example-block">
53+
<p><strong>输入:</strong><span class="example-io">m = 2, n = 1</span></p>
54+
55+
<p><span class="example-io"><b>输出:</b>3</span></p>
56+
57+
<p><strong>解释:</strong></p>
58+
59+
<ul>
60+
<li>你从单元格&nbsp;<code>(0, 0)</code>&nbsp;开始,花费为&nbsp;<code>(0 + 1) * (0 + 1) = 1</code>。</li>
61+
<li>第 1 次移动(奇数次):你可以向下移动到&nbsp;<code>(1, 0)</code>,花费为&nbsp;<code>(1 + 1) * (0 + 1) = 2</code>。</li>
62+
<li>因此,总花费是&nbsp;<code>1 + 2 = 3</code>。</li>
63+
</ul>
64+
</div>
65+
66+
<p>&nbsp;</p>
67+
68+
<p><strong>提示:</strong></p>
69+
70+
<ul>
71+
<li><code>1 &lt;= m, n &lt;= 10<sup>6</sup></code></li>
72+
</ul>
73+
74+
<!-- description:end -->
75+
76+
## 解法
77+
78+
<!-- solution:start -->
79+
80+
### 方法一:脑筋急转弯
81+
82+
由于题目中给定的移动规则,实际上只有以下三种情况可以到达目标单元格:
83+
84+
1. 行列数为 $1 \times 1$ 的网格,花费为 $1$。
85+
2. 行数为 $2$,列数为 $1$ 的网格,花费为 $3$。
86+
3. 行数为 $1$,列数为 $2$ 的网格,花费为 $3$。
87+
88+
对于其他情况,无法到达目标单元格,返回 $-1$。
89+
90+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
91+
92+
<!-- tabs:start -->
93+
94+
#### Python3
95+
96+
```python
97+
class Solution:
98+
def minCost(self, m: int, n: int) -> int:
99+
if m == 1 and n == 1:
100+
return 1
101+
if m == 2 and n == 1:
102+
return 3
103+
if m == 1 and n == 2:
104+
return 3
105+
return -1
106+
```
107+
108+
#### Java
109+
110+
```java
111+
class Solution {
112+
public int minCost(int m, int n) {
113+
if (m == 1 && n == 1) {
114+
return 1;
115+
}
116+
if (m == 1 && n == 2) {
117+
return 3;
118+
}
119+
if (m == 2 && n == 1) {
120+
return 3;
121+
}
122+
return -1;
123+
}
124+
}
125+
```
126+
127+
#### C++
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
int minCost(int m, int n) {
133+
if (m == 1 && n == 1) {
134+
return 1;
135+
}
136+
if (m == 1 && n == 2) {
137+
return 3;
138+
}
139+
if (m == 2 && n == 1) {
140+
return 3;
141+
}
142+
return -1;
143+
}
144+
};
145+
```
146+
147+
#### Go
148+
149+
```go
150+
func minCost(m int, n int) int {
151+
if m == 1 && n == 1 {
152+
return 1
153+
}
154+
if m == 1 && n == 2 {
155+
return 3
156+
}
157+
if m == 2 && n == 1 {
158+
return 3
159+
}
160+
return -1
161+
}
162+
```
163+
164+
#### TypeScript
165+
166+
```ts
167+
function minCost(m: number, n: number): number {
168+
if (m === 1 && n === 1) {
169+
return 1;
170+
}
171+
if (m === 1 && n === 2) {
172+
return 3;
173+
}
174+
if (m === 2 && n === 1) {
175+
return 3;
176+
}
177+
return -1;
178+
}
179+
```
180+
181+
<!-- tabs:end -->
182+
183+
<!-- solution:end -->
184+
185+
<!-- problem:end -->
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3596.Minimum%20Cost%20Path%20with%20Alternating%20Directions%20I/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3596. Minimum Cost Path with Alternating Directions I 🔒](https://leetcode.com/problems/minimum-cost-path-with-alternating-directions-i)
10+
11+
[中文文档](/solution/3500-3599/3596.Minimum%20Cost%20Path%20with%20Alternating%20Directions%20I/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given two integers <code>m</code> and <code>n</code> representing the number of rows and columns of a grid, respectively.</p>
18+
19+
<p>The cost to enter cell <code>(i, j)</code> is defined as <code>(i + 1) * (j + 1)</code>.</p>
20+
21+
<p>You start at cell <code>(0, 0)</code> on move 1.</p>
22+
23+
<p>At each step, you move to an <strong>adjacent</strong> cell, following an alternating pattern:</p>
24+
25+
<ul>
26+
<li>On <strong>odd-numbered</strong> moves, you must move either <strong>right</strong> or <strong>down</strong>.</li>
27+
<li>On <strong>even-numbered</strong> moves, you must move either<strong> left</strong> or <strong>up</strong>.</li>
28+
</ul>
29+
30+
<p>Return the <strong>minimum</strong> total cost required to reach <code>(m - 1, n - 1)</code>. If it is impossible, return -1.</p>
31+
32+
<p>&nbsp;</p>
33+
<p><strong class="example">Example 1:</strong></p>
34+
35+
<div class="example-block">
36+
<p><strong>Input:</strong> <span class="example-io">m = 1, n = 1</span></p>
37+
38+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
39+
40+
<p><strong>Explanation:</strong></p>
41+
42+
<ul>
43+
<li>You start at cell <code>(0, 0)</code>.</li>
44+
<li>The cost to enter <code>(0, 0)</code> is <code>(0 + 1) * (0 + 1) = 1</code>.</li>
45+
<li>Since you&#39;re at the destination, the total cost is 1.</li>
46+
</ul>
47+
</div>
48+
49+
<p><strong class="example">Example 2:</strong></p>
50+
51+
<div class="example-block">
52+
<p><strong>Input:</strong> <span class="example-io">m = 2, n = 1</span></p>
53+
54+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
55+
56+
<p><strong>Explanation:</strong></p>
57+
58+
<ul>
59+
<li>You start at cell <code>(0, 0)</code> with cost <code>(0 + 1) * (0 + 1) = 1</code>.</li>
60+
<li>Move 1 (odd): You can move down to <code>(1, 0)</code> with cost <code>(1 + 1) * (0 + 1) = 2</code>.</li>
61+
<li>Thus, the total cost is <code>1 + 2 = 3</code>.</li>
62+
</ul>
63+
</div>
64+
65+
<p>&nbsp;</p>
66+
<p><strong>Constraints:</strong></p>
67+
68+
<ul>
69+
<li><code>1 &lt;= m, n &lt;= 10<sup>6</sup></code></li>
70+
</ul>
71+
72+
<!-- description:end -->
73+
74+
## Solutions
75+
76+
<!-- solution:start -->
77+
78+
### Solution 1: Brain Teaser
79+
80+
Due to the movement rules given in the problem, in fact, only the following three cases can reach the target cell:
81+
82+
1. A $1 \times 1$ grid, with a cost of $1$.
83+
2. A grid with $2$ rows and $1$ column, with a cost of $3$.
84+
3. A grid with $1$ row and $2$ columns, with a cost of $3$.
85+
86+
For all other cases, it is impossible to reach the target cell, so return $-1$.
87+
88+
The time complexity is $O(1)$, and the space complexity is
89+
90+
<!-- tabs:start -->
91+
92+
#### Python3
93+
94+
```python
95+
class Solution:
96+
def minCost(self, m: int, n: int) -> int:
97+
if m == 1 and n == 1:
98+
return 1
99+
if m == 2 and n == 1:
100+
return 3
101+
if m == 1 and n == 2:
102+
return 3
103+
return -1
104+
```
105+
106+
#### Java
107+
108+
```java
109+
class Solution {
110+
public int minCost(int m, int n) {
111+
if (m == 1 && n == 1) {
112+
return 1;
113+
}
114+
if (m == 1 && n == 2) {
115+
return 3;
116+
}
117+
if (m == 2 && n == 1) {
118+
return 3;
119+
}
120+
return -1;
121+
}
122+
}
123+
```
124+
125+
#### C++
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
int minCost(int m, int n) {
131+
if (m == 1 && n == 1) {
132+
return 1;
133+
}
134+
if (m == 1 && n == 2) {
135+
return 3;
136+
}
137+
if (m == 2 && n == 1) {
138+
return 3;
139+
}
140+
return -1;
141+
}
142+
};
143+
```
144+
145+
#### Go
146+
147+
```go
148+
func minCost(m int, n int) int {
149+
if m == 1 && n == 1 {
150+
return 1
151+
}
152+
if m == 1 && n == 2 {
153+
return 3
154+
}
155+
if m == 2 && n == 1 {
156+
return 3
157+
}
158+
return -1
159+
}
160+
```
161+
162+
#### TypeScript
163+
164+
```ts
165+
function minCost(m: number, n: number): number {
166+
if (m === 1 && n === 1) {
167+
return 1;
168+
}
169+
if (m === 1 && n === 2) {
170+
return 3;
171+
}
172+
if (m === 2 && n === 1) {
173+
return 3;
174+
}
175+
return -1;
176+
}
177+
```
178+
179+
<!-- tabs:end -->
180+
181+
<!-- solution:end -->
182+
183+
<!-- problem:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int minCost(int m, int n) {
4+
if (m == 1 && n == 1) {
5+
return 1;
6+
}
7+
if (m == 1 && n == 2) {
8+
return 3;
9+
}
10+
if (m == 2 && n == 1) {
11+
return 3;
12+
}
13+
return -1;
14+
}
15+
};

0 commit comments

Comments
 (0)