Skip to content

feat: add solutions to lc problem: No.3696 #4529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3596.Minimum%20Cost%20Path%20with%20Alternating%20Directions%20I/README.md
---

<!-- problem:start -->

# [3596. 最小花费路径交替方向 I 🔒](https://leetcode.cn/problems/minimum-cost-path-with-alternating-directions-i)

[English Version](/solution/3500-3599/3596.Minimum%20Cost%20Path%20with%20Alternating%20Directions%20I/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给定两个整数&nbsp;<code>m</code> 和&nbsp;<code>n</code>&nbsp;分别表示一个网格的行数和列数。</p>

<p>进入单元格&nbsp;<code>(i, j)</code>&nbsp;的花费定义为&nbsp;<code>(i + 1) * (j + 1)</code>。</p>

<p>你在第 1 步时从单元格 <code>(0, 0)</code> 开始。</p>

<p>在每一步,你移动到 <strong>相邻</strong>&nbsp;的单元格,遵循交替的模式:</p>

<ul>
<li>在 <strong>奇数次</strong> 移动,你必须向 <strong>右方</strong> 或 <strong>下方</strong> 移动。</li>
<li>在 <strong>偶数次</strong> 移动,你必须向 <strong>左方</strong> 或 <strong>上方</strong> 移动。</li>
</ul>

<p>返回到达 <code>(m - 1, n - 1)</code>&nbsp;的最小总花费。如果不可能到达,返回 -1。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">m = 1, n = 1</span></p>

<p><span class="example-io"><b>输出:</b>1</span></p>

<p><strong>解释:</strong></p>

<ul>
<li>你从单元格&nbsp;<code>(0, 0)</code>&nbsp;开始。</li>
<li>进入&nbsp;<code>(0, 0)</code>&nbsp;的花费是&nbsp;<code>(0 + 1) * (0 + 1) = 1</code>。</li>
<li>由于你已经到达了目标,总花费为 1。</li>
</ul>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">m = 2, n = 1</span></p>

<p><span class="example-io"><b>输出:</b>3</span></p>

<p><strong>解释:</strong></p>

<ul>
<li>你从单元格&nbsp;<code>(0, 0)</code>&nbsp;开始,花费为&nbsp;<code>(0 + 1) * (0 + 1) = 1</code>。</li>
<li>第 1 次移动(奇数次):你可以向下移动到&nbsp;<code>(1, 0)</code>,花费为&nbsp;<code>(1 + 1) * (0 + 1) = 2</code>。</li>
<li>因此,总花费是&nbsp;<code>1 + 2 = 3</code>。</li>
</ul>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= m, n &lt;= 10<sup>6</sup></code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:脑筋急转弯

由于题目中给定的移动规则,实际上只有以下三种情况可以到达目标单元格:

1. 行列数为 $1 \times 1$ 的网格,花费为 $1$。
2. 行数为 $2$,列数为 $1$ 的网格,花费为 $3$。
3. 行数为 $1$,列数为 $2$ 的网格,花费为 $3$。

对于其他情况,无法到达目标单元格,返回 $-1$。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def minCost(self, m: int, n: int) -> int:
if m == 1 and n == 1:
return 1
if m == 2 and n == 1:
return 3
if m == 1 and n == 2:
return 3
return -1
```

#### Java

```java
class Solution {
public int minCost(int m, int n) {
if (m == 1 && n == 1) {
return 1;
}
if (m == 1 && n == 2) {
return 3;
}
if (m == 2 && n == 1) {
return 3;
}
return -1;
}
}
```

#### C++

```cpp
class Solution {
public:
int minCost(int m, int n) {
if (m == 1 && n == 1) {
return 1;
}
if (m == 1 && n == 2) {
return 3;
}
if (m == 2 && n == 1) {
return 3;
}
return -1;
}
};
```

#### Go

```go
func minCost(m int, n int) int {
if m == 1 && n == 1 {
return 1
}
if m == 1 && n == 2 {
return 3
}
if m == 2 && n == 1 {
return 3
}
return -1
}
```

#### TypeScript

```ts
function minCost(m: number, n: number): number {
if (m === 1 && n === 1) {
return 1;
}
if (m === 1 && n === 2) {
return 3;
}
if (m === 2 && n === 1) {
return 3;
}
return -1;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
---
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3596.Minimum%20Cost%20Path%20with%20Alternating%20Directions%20I/README_EN.md
---

<!-- problem:start -->

# [3596. Minimum Cost Path with Alternating Directions I 🔒](https://leetcode.com/problems/minimum-cost-path-with-alternating-directions-i)

[中文文档](/solution/3500-3599/3596.Minimum%20Cost%20Path%20with%20Alternating%20Directions%20I/README.md)

## Description

<!-- description:start -->

<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>

<p>The cost to enter cell <code>(i, j)</code> is defined as <code>(i + 1) * (j + 1)</code>.</p>

<p>You start at cell <code>(0, 0)</code> on move 1.</p>

<p>At each step, you move to an <strong>adjacent</strong> cell, following an alternating pattern:</p>

<ul>
<li>On <strong>odd-numbered</strong> moves, you must move either <strong>right</strong> or <strong>down</strong>.</li>
<li>On <strong>even-numbered</strong> moves, you must move either<strong> left</strong> or <strong>up</strong>.</li>
</ul>

<p>Return the <strong>minimum</strong> total cost required to reach <code>(m - 1, n - 1)</code>. If it is impossible, return -1.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">m = 1, n = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">1</span></p>

<p><strong>Explanation:</strong></p>

<ul>
<li>You start at cell <code>(0, 0)</code>.</li>
<li>The cost to enter <code>(0, 0)</code> is <code>(0 + 1) * (0 + 1) = 1</code>.</li>
<li>Since you&#39;re at the destination, the total cost is 1.</li>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">m = 2, n = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<ul>
<li>You start at cell <code>(0, 0)</code> with cost <code>(0 + 1) * (0 + 1) = 1</code>.</li>
<li>Move 1 (odd): You can move down to <code>(1, 0)</code> with cost <code>(1 + 1) * (0 + 1) = 2</code>.</li>
<li>Thus, the total cost is <code>1 + 2 = 3</code>.</li>
</ul>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= m, n &lt;= 10<sup>6</sup></code></li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1: Brain Teaser

Due to the movement rules given in the problem, in fact, only the following three cases can reach the target cell:

1. A $1 \times 1$ grid, with a cost of $1$.
2. A grid with $2$ rows and $1$ column, with a cost of $3$.
3. A grid with $1$ row and $2$ columns, with a cost of $3$.

For all other cases, it is impossible to reach the target cell, so return $-1$.

The time complexity is $O(1)$, and the space complexity is

<!-- tabs:start -->

#### Python3

```python
class Solution:
def minCost(self, m: int, n: int) -> int:
if m == 1 and n == 1:
return 1
if m == 2 and n == 1:
return 3
if m == 1 and n == 2:
return 3
return -1
```

#### Java

```java
class Solution {
public int minCost(int m, int n) {
if (m == 1 && n == 1) {
return 1;
}
if (m == 1 && n == 2) {
return 3;
}
if (m == 2 && n == 1) {
return 3;
}
return -1;
}
}
```

#### C++

```cpp
class Solution {
public:
int minCost(int m, int n) {
if (m == 1 && n == 1) {
return 1;
}
if (m == 1 && n == 2) {
return 3;
}
if (m == 2 && n == 1) {
return 3;
}
return -1;
}
};
```

#### Go

```go
func minCost(m int, n int) int {
if m == 1 && n == 1 {
return 1
}
if m == 1 && n == 2 {
return 3
}
if m == 2 && n == 1 {
return 3
}
return -1
}
```

#### TypeScript

```ts
function minCost(m: number, n: number): number {
if (m === 1 && n === 1) {
return 1;
}
if (m === 1 && n === 2) {
return 3;
}
if (m === 2 && n === 1) {
return 3;
}
return -1;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int minCost(int m, int n) {
if (m == 1 && n == 1) {
return 1;
}
if (m == 1 && n == 2) {
return 3;
}
if (m == 2 && n == 1) {
return 3;
}
return -1;
}
};
Loading