aboutsummaryrefslogtreecommitdiff
path: root/shortest_dist_road_addition1.py
blob: ae40e56d874d0d050753638aa55bf786db45113f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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
        sol = []
        city_distances = {i: [-1] * i + list(range(n - i)) for i in range(n)}
        for query in queries:
            source, destination = query
            city_distances[source][destination] = 1
            for i in range(destination + 1, n):
                city_distances[source][i] = min(
                    city_distances[destination][i] + 1, city_distances[source][i]
                )
            for i in range(source - 1, -1, -1):
                city_distances[i][destination] = min(
                    city_distances[i][source] + 1, city_distances[i][destination]
                )
            sol.append(city_distances[0][-1])
        return sol


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]]))
    print(obj.shortestDistanceAfterQueries(n=5, queries=[[1, 3], [0, 3]]))