From e08f608dc59cae303da24c55f0f54a0b99b2d44d Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 29 Mar 2024 23:42:26 +0800 Subject: [PATCH] feat: update solutions to lc problmes * NO.2733.Neither Minimum nor Maximum * NO.2736.Maximum Sum Queries * NO.2737.Find the Closest Marked Node * NO.2739.Total Distance Traveled * NO.2740.Find the Value of the Partition * NO.2741.Special Permutations * NO.2743.Count Substrings Without Repeating Character * NO.2881.Create a New Column * NO.2912.Number of Ways to Reach Destination in the Grid --- .../README.md | 14 ++- .../README_EN.md | 14 ++- .../Solution.cpp | 5 +- .../Solution.py | 3 +- .../Solution2.py | 7 -- .../2736.Maximum Sum Queries/README.md | 96 +++++++++---------- .../2736.Maximum Sum Queries/README_EN.md | 96 +++++++++---------- .../README_EN.md | 10 +- .../2739.Total Distance Traveled/README_EN.md | 6 +- .../README_EN.md | 6 +- .../2741.Special Permutations/README_EN.md | 20 +++- .../README_EN.md | 6 +- .../2881.Create a New Column/README.md | 6 +- .../2881.Create a New Column/README_EN.md | 6 +- .../README.md | 44 ++++----- .../README_EN.md | 44 ++++----- 16 files changed, 203 insertions(+), 180 deletions(-) delete mode 100644 solution/2700-2799/2733.Neither Minimum nor Maximum/Solution2.py diff --git a/solution/2700-2799/2733.Neither Minimum nor Maximum/README.md b/solution/2700-2799/2733.Neither Minimum nor Maximum/README.md index b4c7f1e003cea..0e673868c0bdf 100644 --- a/solution/2700-2799/2733.Neither Minimum nor Maximum/README.md +++ b/solution/2700-2799/2733.Neither Minimum nor Maximum/README.md @@ -47,14 +47,19 @@ ## 解法 -### 方法一 +### 方法一:模拟 + +我们可以先找到数组中的最小值和最大值,分别记为 $mi$ 和 $mx$。然后遍历数组,找到第一个不等于 $mi$ 且不等于 $mx$ 的数字,返回即可。 + +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。 ```python class Solution: def findNonMinOrMax(self, nums: List[int]) -> int: - return -1 if len(nums) < 3 else sorted(nums)[1] + mi, mx = min(nums), max(nums) + return next((x for x in nums if x != mi and x != mx), -1) ``` ```java @@ -79,10 +84,9 @@ class Solution { class Solution { public: int findNonMinOrMax(vector& nums) { - int mi = *min_element(nums.begin(), nums.end()); - int mx = *max_element(nums.begin(), nums.end()); + auto [mi, mx] = minmax_element(nums.begin(), nums.end()); for (int x : nums) { - if (x != mi && x != mx) { + if (x != *mi && x != *mx) { return x; } } diff --git a/solution/2700-2799/2733.Neither Minimum nor Maximum/README_EN.md b/solution/2700-2799/2733.Neither Minimum nor Maximum/README_EN.md index 253fb2e8e4ef7..efcfe38468549 100644 --- a/solution/2700-2799/2733.Neither Minimum nor Maximum/README_EN.md +++ b/solution/2700-2799/2733.Neither Minimum nor Maximum/README_EN.md @@ -46,14 +46,19 @@ ## Solutions -### Solution 1 +### Solution 1: Simulation + +First, we find the minimum and maximum values in the array, denoted as $mi$ and $mx$ respectively. Then, we traverse the array and find the first number that is not equal to $mi$ and not equal to $mx$, and return it. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. ```python class Solution: def findNonMinOrMax(self, nums: List[int]) -> int: - return -1 if len(nums) < 3 else sorted(nums)[1] + mi, mx = min(nums), max(nums) + return next((x for x in nums if x != mi and x != mx), -1) ``` ```java @@ -78,10 +83,9 @@ class Solution { class Solution { public: int findNonMinOrMax(vector& nums) { - int mi = *min_element(nums.begin(), nums.end()); - int mx = *max_element(nums.begin(), nums.end()); + auto [mi, mx] = minmax_element(nums.begin(), nums.end()); for (int x : nums) { - if (x != mi && x != mx) { + if (x != *mi && x != *mx) { return x; } } diff --git a/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.cpp b/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.cpp index fc8ad7e591d4e..df97f7d3a240e 100644 --- a/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.cpp +++ b/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.cpp @@ -1,10 +1,9 @@ class Solution { public: int findNonMinOrMax(vector& nums) { - int mi = *min_element(nums.begin(), nums.end()); - int mx = *max_element(nums.begin(), nums.end()); + auto [mi, mx] = minmax_element(nums.begin(), nums.end()); for (int x : nums) { - if (x != mi && x != mx) { + if (x != *mi && x != *mx) { return x; } } diff --git a/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.py b/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.py index fc1dee8848f3b..45d1c61d32dd9 100644 --- a/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.py +++ b/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.py @@ -1,3 +1,4 @@ class Solution: def findNonMinOrMax(self, nums: List[int]) -> int: - return -1 if len(nums) < 3 else sorted(nums)[1] + mi, mx = min(nums), max(nums) + return next((x for x in nums if x != mi and x != mx), -1) diff --git a/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution2.py b/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution2.py deleted file mode 100644 index a55c72804bb32..0000000000000 --- a/solution/2700-2799/2733.Neither Minimum nor Maximum/Solution2.py +++ /dev/null @@ -1,7 +0,0 @@ -class Solution: - def findNonMinOrMax(self, nums: List[int]) -> int: - mi, mx = min(nums), max(nums) - for x in nums: - if x != mi and x != mx: - return x - return -1 diff --git a/solution/2700-2799/2736.Maximum Sum Queries/README.md b/solution/2700-2799/2736.Maximum Sum Queries/README.md index edc776b0952ac..08f487d07a04b 100644 --- a/solution/2700-2799/2736.Maximum Sum Queries/README.md +++ b/solution/2700-2799/2736.Maximum Sum Queries/README.md @@ -186,6 +186,51 @@ class Solution { } ``` +```java +class Solution { + public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) { + int n = nums1.length, m = q.length; + int[][] a = new int[n][2]; + for (int i = 0; i < n; i++) { + a[i][0] = nums1[i]; + a[i][1] = nums2[i]; + } + int[][] b = new int[m][3]; + for (int i = 0; i < m; i++) { + b[i][0] = q[i][0]; + b[i][1] = q[i][1]; + b[i][2] = i; + } + Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]); + Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]); + TreeMap map = new TreeMap<>(); + int[] res = new int[m]; + int max = -1; + for (int i = m - 1, j = n - 1; i >= 0; i--) { + int x = b[i][0], y = b[i][1], idx = b[i][2]; + while (j >= 0 && a[j][0] >= x) { + if (max < a[j][1]) { + max = a[j][1]; + Integer key = map.floorKey(a[j][1]); + while (key != null && map.get(key) <= a[j][0] + a[j][1]) { + map.remove(key); + key = map.floorKey(key); + } + map.put(max, a[j][0] + a[j][1]); + } + j--; + } + Integer key = map.ceilingKey(y); + if (key == null) + res[idx] = -1; + else + res[idx] = map.get(key); + } + return res; + } +} +``` + ```cpp class BinaryIndexedTree { private: @@ -374,55 +419,4 @@ function maximumSumQueries(nums1: number[], nums2: number[], queries: number[][] -### 方法二 - - - -```java -class Solution { - public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) { - int n = nums1.length, m = q.length; - int[][] a = new int[n][2]; - for (int i = 0; i < n; i++) { - a[i][0] = nums1[i]; - a[i][1] = nums2[i]; - } - int[][] b = new int[m][3]; - for (int i = 0; i < m; i++) { - b[i][0] = q[i][0]; - b[i][1] = q[i][1]; - b[i][2] = i; - } - Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]); - Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]); - TreeMap map = new TreeMap<>(); - int[] res = new int[m]; - int max = -1; - for (int i = m - 1, j = n - 1; i >= 0; i--) { - int x = b[i][0], y = b[i][1], idx = b[i][2]; - while (j >= 0 && a[j][0] >= x) { - if (max < a[j][1]) { - max = a[j][1]; - Integer key = map.floorKey(a[j][1]); - while (key != null && map.get(key) <= a[j][0] + a[j][1]) { - map.remove(key); - key = map.floorKey(key); - } - map.put(max, a[j][0] + a[j][1]); - } - j--; - } - Integer key = map.ceilingKey(y); - if (key == null) - res[idx] = -1; - else - res[idx] = map.get(key); - } - return res; - } -} -``` - - - diff --git a/solution/2700-2799/2736.Maximum Sum Queries/README_EN.md b/solution/2700-2799/2736.Maximum Sum Queries/README_EN.md index 3b2c11caf16ef..a26f5d86ff2dd 100644 --- a/solution/2700-2799/2736.Maximum Sum Queries/README_EN.md +++ b/solution/2700-2799/2736.Maximum Sum Queries/README_EN.md @@ -188,6 +188,51 @@ class Solution { } ``` +```java +class Solution { + public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) { + int n = nums1.length, m = q.length; + int[][] a = new int[n][2]; + for (int i = 0; i < n; i++) { + a[i][0] = nums1[i]; + a[i][1] = nums2[i]; + } + int[][] b = new int[m][3]; + for (int i = 0; i < m; i++) { + b[i][0] = q[i][0]; + b[i][1] = q[i][1]; + b[i][2] = i; + } + Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]); + Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]); + TreeMap map = new TreeMap<>(); + int[] res = new int[m]; + int max = -1; + for (int i = m - 1, j = n - 1; i >= 0; i--) { + int x = b[i][0], y = b[i][1], idx = b[i][2]; + while (j >= 0 && a[j][0] >= x) { + if (max < a[j][1]) { + max = a[j][1]; + Integer key = map.floorKey(a[j][1]); + while (key != null && map.get(key) <= a[j][0] + a[j][1]) { + map.remove(key); + key = map.floorKey(key); + } + map.put(max, a[j][0] + a[j][1]); + } + j--; + } + Integer key = map.ceilingKey(y); + if (key == null) + res[idx] = -1; + else + res[idx] = map.get(key); + } + return res; + } +} +``` + ```cpp class BinaryIndexedTree { private: @@ -376,55 +421,4 @@ function maximumSumQueries(nums1: number[], nums2: number[], queries: number[][] -### Solution 2 - - - -```java -class Solution { - public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) { - int n = nums1.length, m = q.length; - int[][] a = new int[n][2]; - for (int i = 0; i < n; i++) { - a[i][0] = nums1[i]; - a[i][1] = nums2[i]; - } - int[][] b = new int[m][3]; - for (int i = 0; i < m; i++) { - b[i][0] = q[i][0]; - b[i][1] = q[i][1]; - b[i][2] = i; - } - Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]); - Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]); - TreeMap map = new TreeMap<>(); - int[] res = new int[m]; - int max = -1; - for (int i = m - 1, j = n - 1; i >= 0; i--) { - int x = b[i][0], y = b[i][1], idx = b[i][2]; - while (j >= 0 && a[j][0] >= x) { - if (max < a[j][1]) { - max = a[j][1]; - Integer key = map.floorKey(a[j][1]); - while (key != null && map.get(key) <= a[j][0] + a[j][1]) { - map.remove(key); - key = map.floorKey(key); - } - map.put(max, a[j][0] + a[j][1]); - } - j--; - } - Integer key = map.ceilingKey(y); - if (key == null) - res[idx] = -1; - else - res[idx] = map.get(key); - } - return res; - } -} -``` - - - diff --git a/solution/2700-2799/2737.Find the Closest Marked Node/README_EN.md b/solution/2700-2799/2737.Find the Closest Marked Node/README_EN.md index 0a175aee3d5aa..ef793996f61c4 100644 --- a/solution/2700-2799/2737.Find the Closest Marked Node/README_EN.md +++ b/solution/2700-2799/2737.Find the Closest Marked Node/README_EN.md @@ -66,7 +66,15 @@ So the answer is 3. ## Solutions -### Solution 1 +### Solution 1: Dijkstra's Algorithm + +First, we construct an adjacency matrix $g$ based on the edge information provided in the problem, where $g[i][j]$ represents the distance from node $i$ to node $j$. If such an edge does not exist, then $g[i][j]$ is positive infinity. + +Then, we can use Dijkstra's algorithm to find the shortest distance from the starting point $s$ to all nodes, denoted as $dist$. + +Finally, we traverse all the marked nodes and find the marked node with the smallest distance. If the distance is positive infinity, we return $-1$. + +The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the number of nodes. diff --git a/solution/2700-2799/2739.Total Distance Traveled/README_EN.md b/solution/2700-2799/2739.Total Distance Traveled/README_EN.md index 211f96d30b4ce..d202c9adc9a2f 100644 --- a/solution/2700-2799/2739.Total Distance Traveled/README_EN.md +++ b/solution/2700-2799/2739.Total Distance Traveled/README_EN.md @@ -46,7 +46,11 @@ Total distance traveled is 10km. ## Solutions -### Solution 1 +### Solution 1: Simulation + +We can simulate the process of the truck's movement. Each time, it consumes 1 liter of fuel from the main fuel tank and travels 10 kilometers. Whenever the fuel in the main fuel tank is consumed by 5 liters, if there is fuel in the auxiliary fuel tank, 1 liter of fuel is transferred from the auxiliary fuel tank to the main fuel tank. The simulation continues until the fuel in the main fuel tank is exhausted. + +The time complexity is $O(n + m)$, where $n$ and $m$ are the amounts of fuel in the main and auxiliary fuel tanks, respectively. The space complexity is $O(1)$. diff --git a/solution/2700-2799/2740.Find the Value of the Partition/README_EN.md b/solution/2700-2799/2740.Find the Value of the Partition/README_EN.md index 0b366887b4d6d..2a25e5bdb54d5 100644 --- a/solution/2700-2799/2740.Find the Value of the Partition/README_EN.md +++ b/solution/2700-2799/2740.Find the Value of the Partition/README_EN.md @@ -57,7 +57,11 @@ It can be proven that 9 is the minimum value out of all partitions. ## Solutions -### Solution 1 +### Solution 1: Sorting + +The problem requires us to minimize the partition value. Therefore, we can sort the array and then take the minimum difference between two adjacent numbers. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array. diff --git a/solution/2700-2799/2741.Special Permutations/README_EN.md b/solution/2700-2799/2741.Special Permutations/README_EN.md index 12363ec7ae29c..3efe7f837b900 100644 --- a/solution/2700-2799/2741.Special Permutations/README_EN.md +++ b/solution/2700-2799/2741.Special Permutations/README_EN.md @@ -41,7 +41,25 @@ ## Solutions -### Solution 1 +### Solution 1: State Compression Dynamic Programming + +We notice that the maximum length of the array in the problem does not exceed $14$. Therefore, we can use an integer to represent the current state, where the $i$-th bit is $1$ if the $i$-th number in the array has been selected, and $0$ if it has not been selected. + +We define $f[i][j]$ as the number of schemes where the current selected integer state is $i$, and the index of the last selected integer is $j$. Initially, $f[0][0]=0$, and the answer is $\sum_{j=0}^{n-1}f[2^n-1][j]$. + +Considering $f[i][j]$, if only one number is currently selected, then $f[i][j]=1$. Otherwise, we can enumerate the index $k$ of the last selected number. If the numbers corresponding to $k$ and $j$ meet the requirements of the problem, then $f[i][j]$ can be transferred from $f[i \oplus 2^j][k]$. That is: + +$$ +f[i][j]= +\begin{cases} +1, & i=2^j\\ +\sum_{k=0}^{n-1}f[i \oplus 2^j][k], & i \neq 2^j \text{ and nums}[j] \text{ and nums}[k] \text{ meet the requirements of the problem}\\ +\end{cases} +$$ + +The final answer is $\sum_{j=0}^{n-1}f[2^n-1][j]$. Note that the answer may be very large, so we need to take the modulus of $10^9+7$. + +The time complexity is $O(n^2 \times 2^n)$, and the space complexity is $O(n \times 2^n)$. Here, $n$ is the length of the array. diff --git a/solution/2700-2799/2743.Count Substrings Without Repeating Character/README_EN.md b/solution/2700-2799/2743.Count Substrings Without Repeating Character/README_EN.md index 6f0e272be378c..9566c9d550a9f 100644 --- a/solution/2700-2799/2743.Count Substrings Without Repeating Character/README_EN.md +++ b/solution/2700-2799/2743.Count Substrings Without Repeating Character/README_EN.md @@ -49,7 +49,11 @@ And it can be shown that there are no special substrings with a length of at lea ## Solutions -### Solution 1 +### Solution 1: Counting + Two Pointers + +We use two pointers $j$ and $i$ to represent the left and right boundaries of the current substring, and an array $cnt$ of length $26$ to count the occurrence of each character in the current substring. We traverse the string from left to right. Each time we traverse to position $i$, we increase the occurrence of $s[i]$, and then check whether $s[i]$ appears at least twice. If so, we need to decrease the occurrence of $s[j]$ and move $j$ one step to the right, until the occurrence of $s[i]$ does not exceed once. In this way, we get the length of the longest special substring ending with $s[i]$, which is $i - j + 1$, so the number of special substrings ending with $s[i]$ is $i - j + 1$. Finally, we add up the number of special substrings ending at each position to get the answer. + +The time complexity is $O(n)$, and the space complexity is $O(C)$. Here, $n$ is the length of the string $s$, and $C$ is the size of the character set. In this problem, the character set consists of lowercase English letters, so $C = 26$. diff --git a/solution/2800-2899/2881.Create a New Column/README.md b/solution/2800-2899/2881.Create a New Column/README.md index dd8c5260b7fe6..9647cabcf9c80 100644 --- a/solution/2800-2899/2881.Create a New Column/README.md +++ b/solution/2800-2899/2881.Create a New Column/README.md @@ -57,7 +57,11 @@ DataFrame employees ## 解法 -### 方法一 +### 方法一:直接计算 + +我们可以直接计算 `salary` 的两倍,然后将结果存入 `bonus` 列。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 diff --git a/solution/2800-2899/2881.Create a New Column/README_EN.md b/solution/2800-2899/2881.Create a New Column/README_EN.md index 3bcaafad10b20..dbadfd4fea689 100644 --- a/solution/2800-2899/2881.Create a New Column/README_EN.md +++ b/solution/2800-2899/2881.Create a New Column/README_EN.md @@ -54,7 +54,11 @@ A new column bonus is created by doubling the value in the column salary. ## Solutions -### Solution 1 +### Solution 1: Direct Calculation + +We can directly calculate the double of `salary` and then store the result in the `bonus` column. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. diff --git a/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README.md b/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README.md index a3bad7b9f58c5..f8d48f7f835b7 100644 --- a/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README.md +++ b/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README.md @@ -110,6 +110,25 @@ class Solution: return b if source[1] == dest[1] else d ``` +```python +class Solution: + def numberOfWays( + self, n: int, m: int, k: int, source: List[int], dest: List[int] + ) -> int: + mod = 10**9 + 7 + f = [1, 0, 0, 0] + for _ in range(k): + g = [0] * 4 + g[0] = ((n - 1) * f[1] + (m - 1) * f[2]) % mod + g[1] = (f[0] + (n - 2) * f[1] + (m - 1) * f[3]) % mod + g[2] = (f[0] + (m - 2) * f[2] + (n - 1) * f[3]) % mod + g[3] = (f[1] + f[2] + (n - 2) * f[3] + (m - 2) * f[3]) % mod + f = g + if source[0] == dest[0]: + return f[0] if source[1] == dest[1] else f[2] + return f[1] if source[1] == dest[1] else f[3] +``` + ```java class Solution { public int numberOfWays(int n, int m, int k, int[] source, int[] dest) { @@ -184,29 +203,4 @@ func numberOfWays(n int, m int, k int, source []int, dest []int) int { -### 方法二 - - - -```python -class Solution: - def numberOfWays( - self, n: int, m: int, k: int, source: List[int], dest: List[int] - ) -> int: - mod = 10**9 + 7 - f = [1, 0, 0, 0] - for _ in range(k): - g = [0] * 4 - g[0] = ((n - 1) * f[1] + (m - 1) * f[2]) % mod - g[1] = (f[0] + (n - 2) * f[1] + (m - 1) * f[3]) % mod - g[2] = (f[0] + (m - 2) * f[2] + (n - 1) * f[3]) % mod - g[3] = (f[1] + f[2] + (n - 2) * f[3] + (m - 2) * f[3]) % mod - f = g - if source[0] == dest[0]: - return f[0] if source[1] == dest[1] else f[2] - return f[1] if source[1] == dest[1] else f[3] -``` - - - diff --git a/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README_EN.md b/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README_EN.md index e72c6ab6c0b18..1dc11e986a290 100644 --- a/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README_EN.md +++ b/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README_EN.md @@ -106,6 +106,25 @@ class Solution: return b if source[1] == dest[1] else d ``` +```python +class Solution: + def numberOfWays( + self, n: int, m: int, k: int, source: List[int], dest: List[int] + ) -> int: + mod = 10**9 + 7 + f = [1, 0, 0, 0] + for _ in range(k): + g = [0] * 4 + g[0] = ((n - 1) * f[1] + (m - 1) * f[2]) % mod + g[1] = (f[0] + (n - 2) * f[1] + (m - 1) * f[3]) % mod + g[2] = (f[0] + (m - 2) * f[2] + (n - 1) * f[3]) % mod + g[3] = (f[1] + f[2] + (n - 2) * f[3] + (m - 2) * f[3]) % mod + f = g + if source[0] == dest[0]: + return f[0] if source[1] == dest[1] else f[2] + return f[1] if source[1] == dest[1] else f[3] +``` + ```java class Solution { public int numberOfWays(int n, int m, int k, int[] source, int[] dest) { @@ -180,29 +199,4 @@ func numberOfWays(n int, m int, k int, source []int, dest []int) int { -### Solution 2 - - - -```python -class Solution: - def numberOfWays( - self, n: int, m: int, k: int, source: List[int], dest: List[int] - ) -> int: - mod = 10**9 + 7 - f = [1, 0, 0, 0] - for _ in range(k): - g = [0] * 4 - g[0] = ((n - 1) * f[1] + (m - 1) * f[2]) % mod - g[1] = (f[0] + (n - 2) * f[1] + (m - 1) * f[3]) % mod - g[2] = (f[0] + (m - 2) * f[2] + (n - 1) * f[3]) % mod - g[3] = (f[1] + f[2] + (n - 2) * f[3] + (m - 2) * f[3]) % mod - f = g - if source[0] == dest[0]: - return f[0] if source[1] == dest[1] else f[2] - return f[1] if source[1] == dest[1] else f[3] -``` - - -