Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6b0cf19

Browse files
add 3243
1 parent571b05e commit6b0cf19

File tree

3 files changed

+69
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+69
-0
lines changed

‎paginated_contents/algorithms/4th_thousand/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3243|[Shortest Distance After Road Addition Queries I](https://leetcode.com/problems/shortest-distance-after-road-addition-queries-i/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3243.java)|| Medium|
34
| 3241|[Time Taken to Mark All Nodes](https://leetcode.com/problems/time-taken-to-mark-all-nodes/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3241.java)|| Hard|
45
| 3240|[Minimum Number of Flips to Make Binary Grid Palindromic II](https://leetcode.com/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-ii/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3240.java)|| Medium|
56
| 3239|[Minimum Number of Flips to Make Binary Grid Palindromic I](https://leetcode.com/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-i/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3239.java)|| Easy|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
packagecom.fishercoder.solutions.fourththousand;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Arrays;
5+
importjava.util.LinkedList;
6+
importjava.util.List;
7+
importjava.util.Queue;
8+
9+
publicclass_3243 {
10+
publicstaticclassSolution1 {
11+
publicint[]shortestDistanceAfterQueries(intn,int[][]queries) {
12+
List<Integer>[]graph =newArrayList[n];
13+
for (inti =0;i <n;i++) {
14+
graph[i] =newArrayList<>();
15+
if (i +1 <n) {
16+
graph[i].add(i +1);
17+
}
18+
}
19+
int[]ans =newint[queries.length];
20+
for (inti =0;i <queries.length;i++) {
21+
graph[queries[i][0]].add(queries[i][1]);
22+
ans[i] =bfs(graph,n)[n -1];
23+
}
24+
returnans;
25+
}
26+
27+
privateint[]bfs(List<Integer>[]graph,intn) {
28+
int[]shortest =newint[n];
29+
Arrays.fill(shortest, -1);
30+
shortest[0] =0;
31+
Queue<Integer>q =newLinkedList<>();
32+
q.offer(0);
33+
while (!q.isEmpty()) {
34+
intcurr =q.poll();
35+
for (intnext :graph[curr]) {
36+
if (shortest[next] == -1) {
37+
shortest[next] =shortest[curr] +1;
38+
q.offer(next);
39+
}
40+
}
41+
}
42+
returnshortest;
43+
}
44+
}
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packagecom.fishercoder.fourththousand;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions.fourththousand._3243;
5+
importorg.junit.jupiter.api.BeforeEach;
6+
importorg.junit.jupiter.api.Test;
7+
8+
importstaticorg.junit.jupiter.api.Assertions.assertArrayEquals;
9+
10+
publicclass_3243Test {
11+
privatestatic_3243.Solution1solution1;
12+
13+
@BeforeEach
14+
publicvoidsetup() {
15+
solution1 =new_3243.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
assertArrayEquals(newint[]{3,2,1},solution1.shortestDistanceAfterQueries(5,
21+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2,4],[0,2],[0,4]")));
22+
}
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp