diff --git a/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/README.md b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/README.md new file mode 100644 index 0000000000000..37d8cbfab596f --- /dev/null +++ b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/README.md @@ -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 +--- + + + +# [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) + +## 题目描述 + + + +

给定两个整数 m 和 n 分别表示一个网格的行数和列数。

+ +

进入单元格 (i, j) 的花费定义为 (i + 1) * (j + 1)

+ +

你在第 1 步时从单元格 (0, 0) 开始。

+ +

在每一步,你移动到 相邻 的单元格,遵循交替的模式:

+ + + +

返回到达 (m - 1, n - 1) 的最小总花费。如果不可能到达,返回 -1。

+ +

 

+ +

示例 1:

+ +
+

输入:m = 1, n = 1

+ +

输出:1

+ +

解释:

+ + +
+ +

示例 2:

+ +
+

输入:m = 2, n = 1

+ +

输出:3

+ +

解释:

+ + +
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一:脑筋急转弯 + +由于题目中给定的移动规则,实际上只有以下三种情况可以到达目标单元格: + +1. 行列数为 $1 \times 1$ 的网格,花费为 $1$。 +2. 行数为 $2$,列数为 $1$ 的网格,花费为 $3$。 +3. 行数为 $1$,列数为 $2$ 的网格,花费为 $3$。 + +对于其他情况,无法到达目标单元格,返回 $-1$。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 + + + +#### 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; +} +``` + + + + + + diff --git a/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/README_EN.md b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/README_EN.md new file mode 100644 index 0000000000000..e76c96890d34f --- /dev/null +++ b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/README_EN.md @@ -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 +--- + + + +# [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 + + + +

You are given two integers m and n representing the number of rows and columns of a grid, respectively.

+ +

The cost to enter cell (i, j) is defined as (i + 1) * (j + 1).

+ +

You start at cell (0, 0) on move 1.

+ +

At each step, you move to an adjacent cell, following an alternating pattern:

+ + + +

Return the minimum total cost required to reach (m - 1, n - 1). If it is impossible, return -1.

+ +

 

+

Example 1:

+ +
+

Input: m = 1, n = 1

+ +

Output: 1

+ +

Explanation:

+ + +
+ +

Example 2:

+ +
+

Input: m = 2, n = 1

+ +

Output: 3

+ +

Explanation:

+ + +
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### 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 + + + +#### 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; +} +``` + + + + + + diff --git a/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.cpp b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.cpp new file mode 100644 index 0000000000000..fb1c4aa10f47a --- /dev/null +++ b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.cpp @@ -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; + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.go b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.go new file mode 100644 index 0000000000000..3881af4f1a3cd --- /dev/null +++ b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.go @@ -0,0 +1,12 @@ +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 +} \ No newline at end of file diff --git a/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.java b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.java new file mode 100644 index 0000000000000..822f09605d438 --- /dev/null +++ b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.java @@ -0,0 +1,14 @@ +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; + } +} \ No newline at end of file diff --git a/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.py b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.py new file mode 100644 index 0000000000000..ce4dabfe719a5 --- /dev/null +++ b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.py @@ -0,0 +1,9 @@ +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 diff --git a/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.ts b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.ts new file mode 100644 index 0000000000000..9b217aa1a43ef --- /dev/null +++ b/solution/3500-3599/3596.Minimum Cost Path with Alternating Directions I/Solution.ts @@ -0,0 +1,12 @@ +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; +} diff --git a/solution/README.md b/solution/README.md index cc0437262d62e..7ef7275d8d717 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3606,6 +3606,7 @@ | 3593 | [使叶子路径成本相等的最小增量](/solution/3500-3599/3593.Minimum%20Increments%20to%20Equalize%20Leaf%20Paths/README.md) | `树`,`深度优先搜索`,`数组`,`动态规划` | 中等 | 第 455 场周赛 | | 3594 | [所有人渡河所需的最短时间](/solution/3500-3599/3594.Minimum%20Time%20to%20Transport%20All%20Individuals/README.md) | `位运算`,`图`,`数组`,`动态规划`,`状态压缩`,`最短路`,`堆(优先队列)` | 困难 | 第 455 场周赛 | | 3595 | [一次或两次](/solution/3500-3599/3595.Once%20Twice/README.md) | | 中等 | 🔒 | +| 3596 | [最小花费路径交替方向 I](/solution/3500-3599/3596.Minimum%20Cost%20Path%20with%20Alternating%20Directions%20I/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 24f9ab8a72c71..c1fb12f084255 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3604,6 +3604,7 @@ Press Control + F(or Command + F on | 3593 | [Minimum Increments to Equalize Leaf Paths](/solution/3500-3599/3593.Minimum%20Increments%20to%20Equalize%20Leaf%20Paths/README_EN.md) | `Tree`,`Depth-First Search`,`Array`,`Dynamic Programming` | Medium | Weekly Contest 455 | | 3594 | [Minimum Time to Transport All Individuals](/solution/3500-3599/3594.Minimum%20Time%20to%20Transport%20All%20Individuals/README_EN.md) | `Bit Manipulation`,`Graph`,`Array`,`Dynamic Programming`,`Bitmask`,`Shortest Path`,`Heap (Priority Queue)` | Hard | Weekly Contest 455 | | 3595 | [Once Twice](/solution/3500-3599/3595.Once%20Twice/README_EN.md) | | Medium | 🔒 | +| 3596 | [Minimum Cost Path with Alternating Directions I](/solution/3500-3599/3596.Minimum%20Cost%20Path%20with%20Alternating%20Directions%20I/README_EN.md) | | Medium | 🔒 | ## Copyright