Skip to content

Commit cf06249

Browse files
authored
feat: add solutions to lc problem: No.3193 (doocs#3156)
No.3193.Count the Number of Inversions
1 parent acdcd0d commit cf06249

File tree

14 files changed

+464
-28
lines changed

14 files changed

+464
-28
lines changed

solution/0400-0499/0419.Battleships in a Board/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ tags:
2525
<p>&nbsp;</p>
2626

2727
<p><strong>示例 1:</strong></p>
28-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0419.Battleships%20in%20a%20Board/images/battelship-grid.jpg" style="width: 333px; height: 333px;" />
28+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0419.Battleships%20in%20a%20Board/images/1719200420-KKnzye-image.png" style="width: 333px; height: 333px;" />
2929
<pre>
3030
<strong>输入:</strong>board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
3131
<strong>输出:</strong>2
Loading

solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,46 @@ tags:
1818

1919
<!-- description:start -->
2020

21-
<p>给定一个由整数数组组成的数组<code>arrays</code>,其中<code>arrays[i]</code>是严格递增排序的,返回一个表示所有数组之间的最长公共子序列的整数数组。</p>
21+
<p>给定一个由整数数组组成的数组&nbsp;<code>arrays</code>,其中&nbsp;<code>arrays[i]</code>&nbsp;是 <strong>严格递增</strong> 排序的,返回一个表示 <strong>所有</strong> 数组之间的 <strong>最长公共子序列</strong> 的整数数组。</p>
2222

23-
<p>子序列是从另一个序列派生出来的序列,删除一些元素或不删除任何元素,而不改变其余元素的顺序。</p>
23+
<p><strong>子序列</strong> 是从另一个序列派生出来的序列,删除一些元素或不删除任何元素,而不改变其余元素的顺序。</p>
2424

2525
<p><strong>示例1:</strong></p>
2626

2727
<pre>
28-
<strong>输入:</strong> arrays = [[<strong><em>1</em></strong>,3,<strong><em>4</em></strong>],
29-
  [<strong><em>1</em></strong>,<strong><em>4</em></strong>,7,9]]
28+
<strong>输入:</strong> arrays = [[<u>1</u>,3,<u>4</u>],
29+
&nbsp; [<u>1</u>,<u>4</u>,7,9]]
3030
<strong>输出:</strong> [1,4]
31-
<strong>解释:</strong> 这两个数组中的最长子序列是[1,4]。
31+
<strong>解释:</strong>&nbsp;这两个数组中的最长子序列是[1,4]。
3232
</pre>
3333

3434
<p><strong>示例 2:</strong></p>
3535

3636
<pre>
37-
<strong>输入:</strong> arrays = [[<strong><em>2</em></strong>,<strong><em>3</em></strong>,<strong><em>6</em></strong>,8],
38-
  [1,<strong><em>2</em></strong>,<strong><em>3</em></strong>,5,<strong><em>6</em></strong>,7,10],
39-
  [<strong><em>2</em></strong>,<strong><em>3</em></strong>,4,<em><strong>6</strong></em>,9]]
37+
<strong>输入:</strong> arrays = [[<u>2</u>,<u>3</u>,<u>6</u>,8],
38+
&nbsp; [1,<u>2</u>,<u>3</u>,5,<u>6</u>,7,10],
39+
&nbsp; [<u>2</u>,<u>3</u>,4,<u>6</u>,9]]
4040
<strong>输出:</strong> [2,3,6]
41-
<strong>解释:</strong> 这三个数组中的最长子序列是[2,3,6]。
41+
<strong>解释:</strong>&nbsp;这三个数组中的最长子序列是 [2,3,6]。
4242
</pre>
4343

4444
<p><strong>示例 3:</strong></p>
4545

4646
<pre>
4747
<strong>输入:</strong> arrays = [[1,2,3,4,5],
48-
  [6,7,8]]
48+
&nbsp; [6,7,8]]
4949
<strong>输出:</strong> []
50-
<strong>解释:</strong> 这两个数组之间没有公共子序列。
50+
<strong>解释:</strong>&nbsp;这两个数组之间没有公共子序列。
5151
</pre>
5252

53-
<p> </p>
53+
<p>&nbsp;</p>
5454

5555
<p><strong>限制条件:</strong></p>
5656

5757
<ul>
58-
<li><code>2 <= arrays.length <= 100</code></li>
59-
<li><code>1 <= arrays[i].length <= 100</code></li>
60-
<li><code>1 <= arrays[i][j] <= 100</code></li>
58+
<li><code>2 &lt;= arrays.length &lt;= 100</code></li>
59+
<li><code>1 &lt;= arrays[i].length &lt;= 100</code></li>
60+
<li><code>1 &lt;= arrays[i][j] &lt;= 100</code></li>
6161
<li><code>arrays[i]</code> 是严格递增排序.</li>
6262
</ul>
6363

solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tags:
1818

1919
<!-- description:start -->
2020

21-
<p>Given an array of integer arrays <code>arrays</code> where each <code>arrays[i]</code> is sorted in <strong>strictly increasing</strong> order, return <em>an integer array representing the <strong>longest common subsequence</strong> between <strong>all</strong> the arrays</em>.</p>
21+
<p>Given an array of integer arrays <code>arrays</code> where each <code>arrays[i]</code> is sorted in <strong>strictly increasing</strong> order, return <em>an integer array representing the <strong>longest common subsequence</strong> among&nbsp;<strong>all</strong> the arrays</em>.</p>
2222

2323
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some elements (possibly none) without changing the order of the remaining elements.</p>
2424

solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ tags:
2525

2626
<p>The <strong>cost</strong> of going from a position <code>(x1, y1)</code> to any other position in the space <code>(x2, y2)</code> is <code>|x2 - x1| + |y2 - y1|</code>.</p>
2727

28-
<p>There are also some <strong>special roads</strong>. You are given a 2D array <code>specialRoads</code> where <code>specialRoads[i] = [x1<sub>i</sub>, y1<sub>i</sub>, x2<sub>i</sub>, y2<sub>i</sub>, cost<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> special road goes in <strong>on direction</strong> from <code>(x1<sub>i</sub>, y1<sub>i</sub>)</code> to <code>(x2<sub>i</sub>, y2<sub>i</sub>)</code> with a cost equal to <code>cost<sub>i</sub></code>. You can use each special road any number of times.</p>
28+
<p>There are also some <strong>special roads</strong>. You are given a 2D array <code>specialRoads</code> where <code>specialRoads[i] = [x1<sub>i</sub>, y1<sub>i</sub>, x2<sub>i</sub>, y2<sub>i</sub>, cost<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> special road goes in <strong>one direction</strong> from <code>(x1<sub>i</sub>, y1<sub>i</sub>)</code> to <code>(x2<sub>i</sub>, y2<sub>i</sub>)</code> with a cost equal to <code>cost<sub>i</sub></code>. You can use each special road any number of times.</p>
2929

3030
<p>Return the <strong>minimum</strong> cost required to go from <code>(startX, startY)</code> to <code>(targetX, targetY)</code>.</p>
3131

solution/3100-3199/3193.Count the Number of Inversions/README.md

+153-4
Original file line numberDiff line numberDiff line change
@@ -116,32 +116,181 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3193.Co
116116

117117
<!-- solution:start -->
118118

119-
### 方法一
119+
### 方法一:动态规划
120+
121+
我们定义 $f[i][j]$ 表示 $[0..i]$ 的排列中,逆序对数量为 $j$ 的排列数。考虑下标为 $i$ 的数 $a_i$ 与前面 $i$ 个数的大小关系。如果 $a_i$ 比前面 $k$ 个数小,那么前面的 $k$ 个数与 $a_i$ 都构成了逆序对,逆序对数量为 $k$。因此,我们可以得到状态转移方程:
122+
123+
$$
124+
f[i][j] = \sum_{k=0}^{\min(i, j)} f[i-1][j-k]
125+
$$
126+
127+
由于题目要求 $[0..\text{end}_i]$ 的逆序对数量为 $\text{cnt}_i$,因此,当我们计算 $i = \text{end}_i$ 时,我们只需要计算 $f[i][\text{cnt}_i]$ 即可。其余的 $f[i][..]$ 都为 $0$。
128+
129+
时间复杂度 $O(n \times m \times \min(n, m))$,空间复杂度 $O(n \times m)$。其中 $m$ 是逆序对数量的最大值。本题中 $m \le 400$。
120130

121131
<!-- tabs:start -->
122132

123133
#### Python3
124134

125135
```python
126-
136+
class Solution:
137+
def numberOfPermutations(self, n: int, requirements: List[List[int]]) -> int:
138+
req = [-1] * n
139+
for end, cnt in requirements:
140+
req[end] = cnt
141+
if req[0] > 0:
142+
return 0
143+
req[0] = 0
144+
mod = 10**9 + 7
145+
m = max(req)
146+
f = [[0] * (m + 1) for _ in range(n)]
147+
f[0][0] = 1
148+
for i in range(1, n):
149+
l, r = 0, m
150+
if req[i] >= 0:
151+
l = r = req[i]
152+
for j in range(l, r + 1):
153+
for k in range(min(i, j) + 1):
154+
f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod
155+
return f[n - 1][req[n - 1]]
127156
```
128157

129158
#### Java
130159

131160
```java
132-
161+
class Solution {
162+
public int numberOfPermutations(int n, int[][] requirements) {
163+
int[] req = new int[n];
164+
Arrays.fill(req, -1);
165+
int m = 0;
166+
for (var r : requirements) {
167+
req[r[0]] = r[1];
168+
m = Math.max(m, r[1]);
169+
}
170+
if (req[0] > 0) {
171+
return 0;
172+
}
173+
req[0] = 0;
174+
final int mod = (int) 1e9 + 7;
175+
int[][] f = new int[n][m + 1];
176+
f[0][0] = 1;
177+
for (int i = 1; i < n; ++i) {
178+
int l = 0, r = m;
179+
if (req[i] >= 0) {
180+
l = r = req[i];
181+
}
182+
for (int j = l; j <= r; ++j) {
183+
for (int k = 0; k <= Math.min(i, j); ++k) {
184+
f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod;
185+
}
186+
}
187+
}
188+
return f[n - 1][req[n - 1]];
189+
}
190+
}
133191
```
134192

135193
#### C++
136194

137195
```cpp
138-
196+
class Solution {
197+
public:
198+
int numberOfPermutations(int n, vector<vector<int>>& requirements) {
199+
vector<int> req(n, -1);
200+
int m = 0;
201+
for (const auto& r : requirements) {
202+
req[r[0]] = r[1];
203+
m = max(m, r[1]);
204+
}
205+
if (req[0] > 0) {
206+
return 0;
207+
}
208+
req[0] = 0;
209+
const int mod = 1e9 + 7;
210+
vector<vector<int>> f(n, vector<int>(m + 1, 0));
211+
f[0][0] = 1;
212+
for (int i = 1; i < n; ++i) {
213+
int l = 0, r = m;
214+
if (req[i] >= 0) {
215+
l = r = req[i];
216+
}
217+
for (int j = l; j <= r; ++j) {
218+
for (int k = 0; k <= min(i, j); ++k) {
219+
f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod;
220+
}
221+
}
222+
}
223+
return f[n - 1][req[n - 1]];
224+
}
225+
};
139226
```
140227
141228
#### Go
142229
143230
```go
231+
func numberOfPermutations(n int, requirements [][]int) int {
232+
req := make([]int, n)
233+
for i := range req {
234+
req[i] = -1
235+
}
236+
for _, r := range requirements {
237+
req[r[0]] = r[1]
238+
}
239+
if req[0] > 0 {
240+
return 0
241+
}
242+
req[0] = 0
243+
m := slices.Max(req)
244+
const mod = int(1e9 + 7)
245+
f := make([][]int, n)
246+
for i := range f {
247+
f[i] = make([]int, m+1)
248+
}
249+
f[0][0] = 1
250+
for i := 1; i < n; i++ {
251+
l, r := 0, m
252+
if req[i] >= 0 {
253+
l, r = req[i], req[i]
254+
}
255+
for j := l; j <= r; j++ {
256+
for k := 0; k <= min(i, j); k++ {
257+
f[i][j] = (f[i][j] + f[i-1][j-k]) % mod
258+
}
259+
}
260+
}
261+
return f[n-1][req[n-1]]
262+
}
263+
```
144264

265+
#### TypeScript
266+
267+
```ts
268+
function numberOfPermutations(n: number, requirements: number[][]): number {
269+
const req: number[] = Array(n).fill(-1);
270+
for (const [end, cnt] of requirements) {
271+
req[end] = cnt;
272+
}
273+
if (req[0] > 0) {
274+
return 0;
275+
}
276+
req[0] = 0;
277+
const m = Math.max(...req);
278+
const mod = 1e9 + 7;
279+
const f = Array.from({ length: n }, () => Array(m + 1).fill(0));
280+
f[0][0] = 1;
281+
for (let i = 1; i < n; ++i) {
282+
let [l, r] = [0, m];
283+
if (req[i] >= 0) {
284+
l = r = req[i];
285+
}
286+
for (let j = l; j <= r; ++j) {
287+
for (let k = 0; k <= Math.min(i, j); ++k) {
288+
f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod;
289+
}
290+
}
291+
}
292+
return f[n - 1][req[n - 1]];
293+
}
145294
```
146295

147296
<!-- tabs:end -->

0 commit comments

Comments
 (0)