Skip to content

feat: update solutions to lc problems: No.2642~2646 #2476

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
Mar 22, 2024
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
Expand Up @@ -312,48 +312,43 @@ class Graph {
```cs
public class Graph {
private int n;
private int[][] g;
private int[,] g;
private readonly int inf = 1 << 29;

public Graph(int n, int[][] edges) {
this.n = n;
g = new int[n][];
for (int i = 0; i < n; i++)
{
g[i] = new int[n];
for (int j = 0; j < n; j++)
{
g[i][j] = inf;
g = new int[n, n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
g[i, j] = inf;
}
}
foreach (int[] e in edges)
{
g[e[0]][e[1]] = e[2];
foreach (var e in edges) {
int f = e[0], t = e[1], c = e[2];
g[f, t] = c;
}
}

public void AddEdge(int[] edge) {
g[edge[0]][edge[1]] = edge[2];
int f = edge[0], t = edge[1], c = edge[2];
g[f, t] = c;
}

public int ShortestPath(int node1, int node2) {
int[] dist = new int[n];
bool[] vis = new bool[n];
Array.Fill(dist, inf);
dist[node1] = 0;

for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; ++i) {
int t = -1;
for (int j = 0; j < n; j++)
{
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
for (int j = 0; j < n; ++j) {
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
t = j;
}
}
vis[t] = true;
for (int j = 0; j < n; j++)
{
dist[j] = Math.Min(dist[j], dist[t] + g[t][j]);
for (int j = 0; j < n; ++j) {
dist[j] = Math.Min(dist[j], dist[t] + g[t, j]);
}
}
return dist[node2] >= inf ? -1 : dist[node2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ g.shortestPath(0, 3); // return 6. The shortest path from 0 to 3 now is 0 -&gt;

## Solutions

### Solution 1
### Solution 1: Dijsktra's Algorithm

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

In the `addEdge` function, we update the value of $g_{ij}$ to $edge[2]$.

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

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.

<!-- tabs:start -->

Expand Down Expand Up @@ -300,48 +308,43 @@ class Graph {
```cs
public class Graph {
private int n;
private int[][] g;
private int[,] g;
private readonly int inf = 1 << 29;

public Graph(int n, int[][] edges) {
this.n = n;
g = new int[n][];
for (int i = 0; i < n; i++)
{
g[i] = new int[n];
for (int j = 0; j < n; j++)
{
g[i][j] = inf;
g = new int[n, n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
g[i, j] = inf;
}
}
foreach (int[] e in edges)
{
g[e[0]][e[1]] = e[2];
foreach (var e in edges) {
int f = e[0], t = e[1], c = e[2];
g[f, t] = c;
}
}

public void AddEdge(int[] edge) {
g[edge[0]][edge[1]] = edge[2];
int f = edge[0], t = edge[1], c = edge[2];
g[f, t] = c;
}

public int ShortestPath(int node1, int node2) {
int[] dist = new int[n];
bool[] vis = new bool[n];
Array.Fill(dist, inf);
dist[node1] = 0;

for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; ++i) {
int t = -1;
for (int j = 0; j < n; j++)
{
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
for (int j = 0; j < n; ++j) {
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
t = j;
}
}
vis[t] = true;
for (int j = 0; j < n; j++)
{
dist[j] = Math.Min(dist[j], dist[t] + g[t][j]);
for (int j = 0; j < n; ++j) {
dist[j] = Math.Min(dist[j], dist[t] + g[t, j]);
}
}
return dist[node2] >= inf ? -1 : dist[node2];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
public class Graph {
private int n;
private int[][] g;
private int[,] g;
private readonly int inf = 1 << 29;

public Graph(int n, int[][] edges) {
this.n = n;
g = new int[n][];
for (int i = 0; i < n; i++)
{
g[i] = new int[n];
for (int j = 0; j < n; j++)
{
g[i][j] = inf;
g = new int[n, n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
g[i, j] = inf;
}
}
foreach (int[] e in edges)
{
g[e[0]][e[1]] = e[2];
foreach (var e in edges) {
int f = e[0], t = e[1], c = e[2];
g[f, t] = c;
}
}

public void AddEdge(int[] edge) {
g[edge[0]][edge[1]] = edge[2];
int f = edge[0], t = edge[1], c = edge[2];
g[f, t] = c;
}

public int ShortestPath(int node1, int node2) {
int[] dist = new int[n];
bool[] vis = new bool[n];
Array.Fill(dist, inf);
dist[node1] = 0;

for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; ++i) {
int t = -1;
for (int j = 0; j < n; j++)
{
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
for (int j = 0; j < n; ++j) {
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
t = j;
}
}
vis[t] = true;
for (int j = 0; j < n; j++)
{
dist[j] = Math.Min(dist[j], dist[t] + g[t][j]);
for (int j = 0; j < n; ++j) {
dist[j] = Math.Min(dist[j], dist[t] + g[t, j]);
}
}
return dist[node2] >= inf ? -1 : dist[node2];
Expand All @@ -53,4 +48,4 @@ public int ShortestPath(int node1, int node2) {
* Graph obj = new Graph(n, edges);
* obj.AddEdge(edge);
* int param_2 = obj.ShortestPath(node1,node2);
*/
*/
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
class Graph {
private g: number[][] = [];
private inf: number = 1 << 29;

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

shortestPath(node1: number, node2: number): number {
const n = this.g.length;
const dist: number[] = new Array(n).fill(this.inf);
const dist: number[] = Array(n).fill(Infinity);
dist[node1] = 0;
const vis: boolean[] = new Array(n).fill(false);
const vis: boolean[] = Array(n).fill(false);
for (let i = 0; i < n; ++i) {
let t = -1;
for (let j = 0; j < n; ++j) {
Expand All @@ -31,7 +30,7 @@ class Graph {
dist[j] = Math.min(dist[j], dist[t] + this.g[t][j]);
}
}
return dist[node2] >= this.inf ? -1 : dist[node2];
return dist[node2] >= Infinity ? -1 : dist[node2];
}
}

Expand Down
6 changes: 5 additions & 1 deletion solution/2600-2699/2643.Row With Maximum Ones/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@

## Solutions

### Solution 1
### Solution 1: Simulation

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.

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)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,16 @@ Since divisors[0] and divisors[1] both have the maximum divisibility score, we r

## Solutions

### Solution 1
### Solution 1: Enumeration

We can enumerate each element $div$ in $divisors$, and calculate how many elements in $nums$ can be divided by $div$, denoted as $cnt$.

- If $cnt$ is greater than the current maximum divisibility score $mx$, then update $mx = cnt$, and update $ans = div$.
- If $cnt$ equals $mx$ and $div$ is less than $ans$, then update $ans = div$.

Finally, return $ans$.

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)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ The total price sum of all trips is 1. It can be proven, that 1 is the minimum a

## Solutions

### Solution 1
### Solution 1: Enumeration

We can enumerate each element $div$ in $divisors$, and calculate how many elements in $nums$ can be divided by $div$, denoted as $cnt$.

- If $cnt$ is greater than the current maximum divisibility score $mx$, then update $mx = cnt$, and update $ans = div$.
- If $cnt$ equals $mx$ and $div$ is less than $ans$, then update $ans = div$.

Finally, return $ans$.

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)$.

<!-- tabs:start -->

Expand Down
Loading