You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2600-2699/2642.Design Graph With Shortest Path Calculator/README_EN.md
+25-22
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,15 @@ g.shortestPath(0, 3); // return 6. The shortest path from 0 to 3 now is 0 ->
51
51
52
52
## Solutions
53
53
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.
Copy file name to clipboardexpand all lines: solution/2600-2699/2643.Row With Maximum Ones/README_EN.md
+5-1
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,11 @@
49
49
50
50
## Solutions
51
51
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)$.
0 commit comments