aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--shortest_dist_road_addition1.cpp48
-rw-r--r--shortest_dist_road_addition1.py39
2 files changed, 87 insertions, 0 deletions
diff --git a/shortest_dist_road_addition1.cpp b/shortest_dist_road_addition1.cpp
new file mode 100644
index 0000000..e0cc205
--- /dev/null
+++ b/shortest_dist_road_addition1.cpp
@@ -0,0 +1,48 @@
+// 3243. Shortest Distance After Road Addition Queries I
+#include "leetcode.h"
+
+/*
+ * you are given an integer 'n' and a 2d integer array 'queries'. there are 'n'
+ * cities numbered from 0 to 'n - 1'. initially there is a unidirectional road
+ * from city 'i' to city 'i + 1' for all '0 <= i < n - 1'. 'queries[i] = [u_i,
+ * v_i]' represents the addition of a new unidirectional road from city 'u_i' to
+ * city 'v_i'. after each query, you need to find the length of the shortest
+ * path from city 0 to city 'n - 1'. return an array 'answer' where for each 'i'
+ * in the range of '[0, queries.length - 1]', 'answer[i]' is the length of the
+ * shortest path from city 0 to city 'n - 1' after processing the first 'i + 1'
+ * queries.
+ */
+
+class Solution {
+public:
+ vector<int> shortestDistanceAfterQueries(int n,
+ vector<vector<int>> &queries) {
+ vector<int> ans;
+ unordered_map<int, vector<int>> mp;
+ for (int i = 0; i < n - 1; i++)
+ mp[i].push_back(i + 1);
+ for (auto i : queries) {
+ mp[i[0]].push_back(i[1]);
+ vector<int> dp(n, INT_MAX);
+ dp[n - 1] = 0;
+ for (int j = n - 2; j >= 0; --j)
+ for (int next : mp[j])
+ dp[j] = min(dp[j], dp[next] + 1);
+ ans.push_back(dp[0]);
+ }
+ return ans;
+ }
+};
+
+int main() {
+ Solution obj;
+ vvd(int) q1 = {{2, 4}, {0, 2}, {0, 4}}, q2 = {{0, 3}, {0, 3}};
+ vector<int> sdaq1 = obj.shortestDistanceAfterQueries(5, q1);
+ vector<int> sdaq2 = obj.shortestDistanceAfterQueries(4, q2);
+ for (auto i : sdaq1)
+ printf("%d ", i); // expect: 3 2 1
+ printf("\n");
+ for (auto i : sdaq2)
+ printf("%d ", i); // expect: 1 1
+ printf("\n");
+}
diff --git a/shortest_dist_road_addition1.py b/shortest_dist_road_addition1.py
new file mode 100644
index 0000000..0115c4a
--- /dev/null
+++ b/shortest_dist_road_addition1.py
@@ -0,0 +1,39 @@
+# 3243. Shortest Distance After Road Addition Queries I
+
+"""
+you are given an integer 'n' and a 2d integer array 'queries'. there are 'n'
+cities numbered from 0 to 'n - 1'. initially there is a unidirectional road
+from city 'i' to city 'i + 1' for all '0 <= i < n - 1'. 'queries[i] = [u_i,
+v_i]' represents the addition of a new unidirectional road from city 'u_i' to
+city 'v_i'. after each query, you need to find the length of the shortest
+path from city 0 to city 'n - 1'. return an array 'answer' where for each 'i'
+in the range of '[0, queries.length - 1]', 'answer[i]' is the length of the
+shortest path from city 0 to city 'n - 1' after processing the first 'i + 1'
+queries.
+"""
+
+
+class Solution(object):
+ def shortestDistanceAfterQueries(self, n, queries):
+ """
+ :type n: int
+ :type queries: List[List[int]]
+ :rtype: List[int]
+ """
+ ans = []
+ mp = {i: [i + 1] for i in range(n - 1)}
+ for i in queries:
+ mp[i[0]].append(i[1])
+ dp = [float("inf")] * n
+ dp[n - 1] = 0
+ for j in range(n - 2, -1, -1):
+ for next in mp[j]:
+ dp[j] = min(dp[j], dp[next] + 1)
+ ans.append(dp[0])
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.shortestDistanceAfterQueries(n=5, queries=[[2, 4], [0, 2], [0, 4]]))
+ print(obj.shortestDistanceAfterQueries(n=4, queries=[[0, 3], [0, 2]]))