Skip to content

Commit edd5931

Browse files
authored
feat: update solutions to lc problems: No.2642~2646 (#2476)
1 parent c42b175 commit edd5931

File tree

7 files changed

+87
-73
lines changed

7 files changed

+87
-73
lines changed

solution/2600-2699/2642.Design Graph With Shortest Path Calculator/README.md

+16-21
Original file line numberDiff line numberDiff line change
@@ -312,48 +312,43 @@ class Graph {
312312
```cs
313313
public class Graph {
314314
private int n;
315-
private int[][] g;
315+
private int[,] g;
316316
private readonly int inf = 1 << 29;
317317

318318
public Graph(int n, int[][] edges) {
319319
this.n = n;
320-
g = new int[n][];
321-
for (int i = 0; i < n; i++)
322-
{
323-
g[i] = new int[n];
324-
for (int j = 0; j < n; j++)
325-
{
326-
g[i][j] = inf;
320+
g = new int[n, n];
321+
for (int i = 0; i < n; i++) {
322+
for (int j = 0; j < n; j++) {
323+
g[i, j] = inf;
327324
}
328325
}
329-
foreach (int[] e in edges)
330-
{
331-
g[e[0]][e[1]] = e[2];
326+
foreach (var e in edges) {
327+
int f = e[0], t = e[1], c = e[2];
328+
g[f, t] = c;
332329
}
333330
}
334331

335332
public void AddEdge(int[] edge) {
336-
g[edge[0]][edge[1]] = edge[2];
333+
int f = edge[0], t = edge[1], c = edge[2];
334+
g[f, t] = c;
337335
}
338336

339337
public int ShortestPath(int node1, int node2) {
340338
int[] dist = new int[n];
341339
bool[] vis = new bool[n];
342340
Array.Fill(dist, inf);
343341
dist[node1] = 0;
344-
345-
for (int i = 0; i < n; i++)
346-
{
342+
for (int i = 0; i < n; ++i) {
347343
int t = -1;
348-
for (int j = 0; j < n; j++)
349-
{
350-
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
344+
for (int j = 0; j < n; ++j) {
345+
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
351346
t = j;
347+
}
352348
}
353349
vis[t] = true;
354-
for (int j = 0; j < n; j++)
355-
{
356-
dist[j] = Math.Min(dist[j], dist[t] + g[t][j]);
350+
for (int j = 0; j < n; ++j) {
351+
dist[j] = Math.Min(dist[j], dist[t] + g[t, j]);
357352
}
358353
}
359354
return dist[node2] >= inf ? -1 : dist[node2];

solution/2600-2699/2642.Design Graph With Shortest Path Calculator/README_EN.md

+25-22
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ g.shortestPath(0, 3); // return 6. The shortest path from 0 to 3 now is 0 -&gt;
5151

5252
## Solutions
5353

54-
### Solution 1
54+
### Solution 1: Dijsktra's Algorithm
55+
56+
In the initialization function, we first use the adjacency matrix $g$ to store the edge weights of the graph, where $g_{ij}$ represents the edge weight from node $i$ to node $j$. If there is no edge between $i$ and $j$, the value of $g_{ij}$ is $\infty$.
57+
58+
In the `addEdge` function, we update the value of $g_{ij}$ to $edge[2]$.
59+
60+
In the `shortestPath` function, we use Dijsktra's algorithm to find the shortest path from node $node1$ to node $node2$. Here, $dist[i]$ represents the shortest path from node $node1$ to node $i$, and $vis[i]$ indicates whether node $i$ has been visited. We initialize $dist[node1]$ to $0$, and the rest of $dist[i]$ are all $\infty$. Then we iterate $n$ times, each time finding the current unvisited node $t$ such that $dist[t]$ is the smallest. Then we mark node $t$ as visited, and then update the value of $dist[i]$ to $min(dist[i], dist[t] + g_{ti})$. Finally, we return $dist[node2]$. If $dist[node2]$ is $\infty$, it means that there is no path from node $node1$ to node $node2$, so we return $-1$.
61+
62+
The time complexity is $O(n^2 \times q)$, and the space complexity is $O(n^2)$. Where $n$ is the number of nodes, and $q$ is the number of calls to the `shortestPath` function.
5563

5664
<!-- tabs:start -->
5765

@@ -300,48 +308,43 @@ class Graph {
300308
```cs
301309
public class Graph {
302310
private int n;
303-
private int[][] g;
311+
private int[,] g;
304312
private readonly int inf = 1 << 29;
305313

306314
public Graph(int n, int[][] edges) {
307315
this.n = n;
308-
g = new int[n][];
309-
for (int i = 0; i < n; i++)
310-
{
311-
g[i] = new int[n];
312-
for (int j = 0; j < n; j++)
313-
{
314-
g[i][j] = inf;
316+
g = new int[n, n];
317+
for (int i = 0; i < n; i++) {
318+
for (int j = 0; j < n; j++) {
319+
g[i, j] = inf;
315320
}
316321
}
317-
foreach (int[] e in edges)
318-
{
319-
g[e[0]][e[1]] = e[2];
322+
foreach (var e in edges) {
323+
int f = e[0], t = e[1], c = e[2];
324+
g[f, t] = c;
320325
}
321326
}
322327

323328
public void AddEdge(int[] edge) {
324-
g[edge[0]][edge[1]] = edge[2];
329+
int f = edge[0], t = edge[1], c = edge[2];
330+
g[f, t] = c;
325331
}
326332

327333
public int ShortestPath(int node1, int node2) {
328334
int[] dist = new int[n];
329335
bool[] vis = new bool[n];
330336
Array.Fill(dist, inf);
331337
dist[node1] = 0;
332-
333-
for (int i = 0; i < n; i++)
334-
{
338+
for (int i = 0; i < n; ++i) {
335339
int t = -1;
336-
for (int j = 0; j < n; j++)
337-
{
338-
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
340+
for (int j = 0; j < n; ++j) {
341+
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
339342
t = j;
343+
}
340344
}
341345
vis[t] = true;
342-
for (int j = 0; j < n; j++)
343-
{
344-
dist[j] = Math.Min(dist[j], dist[t] + g[t][j]);
346+
for (int j = 0; j < n; ++j) {
347+
dist[j] = Math.Min(dist[j], dist[t] + g[t, j]);
345348
}
346349
}
347350
return dist[node2] >= inf ? -1 : dist[node2];
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
11
public class Graph {
22
private int n;
3-
private int[][] g;
3+
private int[,] g;
44
private readonly int inf = 1 << 29;
55

66
public Graph(int n, int[][] edges) {
77
this.n = n;
8-
g = new int[n][];
9-
for (int i = 0; i < n; i++)
10-
{
11-
g[i] = new int[n];
12-
for (int j = 0; j < n; j++)
13-
{
14-
g[i][j] = inf;
8+
g = new int[n, n];
9+
for (int i = 0; i < n; i++) {
10+
for (int j = 0; j < n; j++) {
11+
g[i, j] = inf;
1512
}
1613
}
17-
foreach (int[] e in edges)
18-
{
19-
g[e[0]][e[1]] = e[2];
14+
foreach (var e in edges) {
15+
int f = e[0], t = e[1], c = e[2];
16+
g[f, t] = c;
2017
}
2118
}
2219

2320
public void AddEdge(int[] edge) {
24-
g[edge[0]][edge[1]] = edge[2];
21+
int f = edge[0], t = edge[1], c = edge[2];
22+
g[f, t] = c;
2523
}
2624

2725
public int ShortestPath(int node1, int node2) {
2826
int[] dist = new int[n];
2927
bool[] vis = new bool[n];
3028
Array.Fill(dist, inf);
3129
dist[node1] = 0;
32-
33-
for (int i = 0; i < n; i++)
34-
{
30+
for (int i = 0; i < n; ++i) {
3531
int t = -1;
36-
for (int j = 0; j < n; j++)
37-
{
38-
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
32+
for (int j = 0; j < n; ++j) {
33+
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
3934
t = j;
35+
}
4036
}
4137
vis[t] = true;
42-
for (int j = 0; j < n; j++)
43-
{
44-
dist[j] = Math.Min(dist[j], dist[t] + g[t][j]);
38+
for (int j = 0; j < n; ++j) {
39+
dist[j] = Math.Min(dist[j], dist[t] + g[t, j]);
4540
}
4641
}
4742
return dist[node2] >= inf ? -1 : dist[node2];
@@ -53,4 +48,4 @@ public int ShortestPath(int node1, int node2) {
5348
* Graph obj = new Graph(n, edges);
5449
* obj.AddEdge(edge);
5550
* int param_2 = obj.ShortestPath(node1,node2);
56-
*/
51+
*/

solution/2600-2699/2642.Design Graph With Shortest Path Calculator/Solution.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
class Graph {
22
private g: number[][] = [];
3-
private inf: number = 1 << 29;
43

54
constructor(n: number, edges: number[][]) {
6-
this.g = Array.from({ length: n }, () => Array(n).fill(this.inf));
5+
this.g = Array.from({ length: n }, () => Array(n).fill(Infinity));
76
for (const [f, t, c] of edges) {
87
this.g[f][t] = c;
98
}
@@ -16,9 +15,9 @@ class Graph {
1615

1716
shortestPath(node1: number, node2: number): number {
1817
const n = this.g.length;
19-
const dist: number[] = new Array(n).fill(this.inf);
18+
const dist: number[] = Array(n).fill(Infinity);
2019
dist[node1] = 0;
21-
const vis: boolean[] = new Array(n).fill(false);
20+
const vis: boolean[] = Array(n).fill(false);
2221
for (let i = 0; i < n; ++i) {
2322
let t = -1;
2423
for (let j = 0; j < n; ++j) {
@@ -31,7 +30,7 @@ class Graph {
3130
dist[j] = Math.min(dist[j], dist[t] + this.g[t][j]);
3231
}
3332
}
34-
return dist[node2] >= this.inf ? -1 : dist[node2];
33+
return dist[node2] >= Infinity ? -1 : dist[node2];
3534
}
3635
}
3736

solution/2600-2699/2643.Row With Maximum Ones/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@
4949

5050
## Solutions
5151

52-
### Solution 1
52+
### Solution 1: Simulation
53+
54+
We directly traverse the matrix, count the number of $1$s in each row, and update the maximum value and the corresponding row index. Note that if the number of $1$s in the current row is equal to the maximum value, we need to choose the row with the smaller index.
55+
56+
The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
5357

5458
<!-- tabs:start -->
5559

solution/2600-2699/2644.Find the Maximum Divisibility Score/README_EN.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ Since divisors[0] and divisors[1] both have the maximum divisibility score, we r
5858

5959
## Solutions
6060

61-
### Solution 1
61+
### Solution 1: Enumeration
62+
63+
We can enumerate each element $div$ in $divisors$, and calculate how many elements in $nums$ can be divided by $div$, denoted as $cnt$.
64+
65+
- If $cnt$ is greater than the current maximum divisibility score $mx$, then update $mx = cnt$, and update $ans = div$.
66+
- If $cnt$ equals $mx$ and $div$ is less than $ans$, then update $ans = div$.
67+
68+
Finally, return $ans$.
69+
70+
The time complexity is $O(m \times n)$, where $m$ and $n$ are the lengths of $nums$ and $divisors$ respectively. The space complexity is $O(1)$.
6271

6372
<!-- tabs:start -->
6473

solution/2600-2699/2646.Minimize the Total Price of the Trips/README_EN.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ The total price sum of all trips is 1. It can be proven, that 1 is the minimum a
5959

6060
## Solutions
6161

62-
### Solution 1
62+
### Solution 1: Enumeration
63+
64+
We can enumerate each element $div$ in $divisors$, and calculate how many elements in $nums$ can be divided by $div$, denoted as $cnt$.
65+
66+
- If $cnt$ is greater than the current maximum divisibility score $mx$, then update $mx = cnt$, and update $ans = div$.
67+
- If $cnt$ equals $mx$ and $div$ is less than $ans$, then update $ans = div$.
68+
69+
Finally, return $ans$.
70+
71+
The time complexity is $O(m \times n)$, where $m$ and $n$ are the lengths of $nums$ and $divisors$ respectively. The space complexity is $O(1)$.
6372

6473
<!-- tabs:start -->
6574

0 commit comments

Comments
 (0)