-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #629 from LetMeFly666/3243
添加问题“3243.新增道路查询后的最短距离I”的代码和题解
- Loading branch information
Showing
11 changed files
with
1,742 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
Codes/3243-shortest-distance-after-road-addition-queries-i.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* @Author: LetMeFly | ||
* @Date: 2024-11-19 14:30:16 | ||
* @LastEditors: LetMeFly.xyz | ||
* @LastEditTime: 2024-11-19 14:37:15 | ||
*/ | ||
#ifdef _WIN32 | ||
#include "_[1,2]toVector.h" | ||
#endif | ||
|
||
class Solution { | ||
public: | ||
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) { | ||
vector<int> shortest(n); | ||
vector<vector<int>> fromList(n); | ||
for (int i = 1; i < n; i++) { | ||
fromList[i].push_back(i - 1); | ||
shortest[i] = i; | ||
} | ||
vector<int> ans(queries.size()); | ||
for (int i = 0; i < queries.size(); i++) { | ||
int from = queries[i][0], to = queries[i][1]; | ||
fromList[to].push_back(from); | ||
for (int j = to; j < n; j++) { | ||
for (int from : fromList[j]) { | ||
shortest[j] = min(shortest[j], shortest[from] + 1); | ||
} | ||
} | ||
ans[i] = shortest.back(); | ||
} | ||
return ans; | ||
} | ||
}; |
28 changes: 28 additions & 0 deletions
28
Codes/3243-shortest-distance-after-road-addition-queries-i.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* @Author: LetMeFly | ||
* @Date: 2024-11-19 15:00:03 | ||
* @LastEditors: LetMeFly.xyz | ||
* @LastEditTime: 2024-11-19 15:04:56 | ||
*/ | ||
package main | ||
|
||
func shortestDistanceAfterQueries(n int, queries [][]int) (ans []int) { | ||
shortest := make([]int, n) | ||
fromList := make([][]int, n) | ||
for i := range shortest { | ||
shortest[i] = i | ||
fromList[i] = make([]int, 0) | ||
fromList[i] = append(fromList[i], i - 1) | ||
} | ||
ans = make([]int, len(queries)) | ||
for i, query := range queries { | ||
fromList[query[1]] = append(fromList[query[1]], query[0]) | ||
for j := query[1]; j < n; j++ { | ||
for _, from := range fromList[j] { | ||
shortest[j] = min(shortest[j], shortest[from] + 1) | ||
} | ||
} | ||
ans[i] = shortest[n - 1] | ||
} | ||
return | ||
} |
32 changes: 32 additions & 0 deletions
32
Codes/3243-shortest-distance-after-road-addition-queries-i.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* @Author: LetMeFly | ||
* @Date: 2024-11-19 14:46:11 | ||
* @LastEditors: LetMeFly.xyz | ||
* @LastEditTime: 2024-11-19 14:58:40 | ||
*/ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
class Solution { | ||
public int[] shortestDistanceAfterQueries(int n, int[][] queries) { | ||
int[] shortest = new int[n]; | ||
List<Integer>[] fromList = new List[n]; | ||
for (int i = 0; i < n; i++) { | ||
shortest[i] = i; | ||
fromList[i] = new ArrayList<Integer>(); | ||
fromList[i].add(i - 1); | ||
} | ||
int[] ans = new int[queries.length]; | ||
for (int i = 0; i < queries.length; i++) { | ||
int from = queries[i][0], to = queries[i][1]; | ||
fromList[to].add(from); | ||
for (int j = to; j < n; j++) { | ||
for (int from_ : fromList[j]) { | ||
shortest[j] = Math.min(shortest[j], shortest[from_] + 1); | ||
} | ||
} | ||
ans[i] = shortest[n - 1]; | ||
} | ||
return ans; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Codes/3243-shortest-distance-after-road-addition-queries-i.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
''' | ||
Author: LetMeFly | ||
Date: 2024-11-19 14:37:55 | ||
LastEditors: LetMeFly.xyz | ||
LastEditTime: 2024-11-19 14:45:39 | ||
''' | ||
from typing import List | ||
|
||
class Solution: | ||
def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]: | ||
shortest = [i for i in range(n)] | ||
fromList = [[i - 1] for i in range(n)] | ||
ans = [] | ||
for from_, to in queries: | ||
fromList[to].append(from_) | ||
for i in range(to, n): | ||
shortest[i] = min(shortest[i], min(shortest[j] + 1 for j in fromList[i])) | ||
ans.append(shortest[-1]) | ||
return ans |
Oops, something went wrong.