Skip to content

Commit a0b0f72

Browse files
authored
Fixed sonar warnings.
1 parent afe56fc commit a0b0f72

File tree

3 files changed

+18
-39
lines changed

3 files changed

+18
-39
lines changed

src/main/java/g1901_2000/s1928_minimum_cost_to_reach_destination_in_time/Solution.java

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@
1212
public class Solution {
1313
public int minCost(int maxTime, int[][] edges, int[] passingFees) {
1414
final PriorityQueue<Tuple> pq =
15-
new PriorityQueue<>(
16-
(a, b) -> {
17-
if (a.cost != b.cost) {
18-
return a.cost - b.cost;
19-
}
20-
return a.time - b.time;
21-
});
15+
new PriorityQueue<>((a, b) -> a.cost == b.cost ? a.time - b.time : a.cost - b.cost);
2216
final int n = passingFees.length;
2317
final int[] minTime = new int[n];
2418
Arrays.fill(minTime, Integer.MAX_VALUE);
@@ -29,22 +23,18 @@ public int minCost(int maxTime, int[][] edges, int[] passingFees) {
2923
pq.offer(new Tuple(0, passingFees[0], 0));
3024
while (!pq.isEmpty()) {
3125
final Tuple curr = pq.poll();
32-
if (curr.time > maxTime || curr.time >= minTime[curr.node]) {
33-
continue;
34-
}
35-
minTime[curr.node] = curr.time;
36-
if (curr.node == n - 1) {
37-
return curr.cost;
38-
}
39-
for (final Edge edge : graph.getEdges(curr.node)) {
40-
final int time = curr.time + edge.weight;
41-
if (time > maxTime) {
42-
continue;
26+
if (curr.time <= maxTime && curr.time < minTime[curr.node]) {
27+
minTime[curr.node] = curr.time;
28+
if (curr.node == n - 1) {
29+
return curr.cost;
4330
}
44-
if (time >= minTime[edge.dst]) {
45-
continue;
31+
for (final Edge edge : graph.getEdges(curr.node)) {
32+
final int time = curr.time + edge.weight;
33+
if (time > maxTime || time >= minTime[edge.dst]) {
34+
continue;
35+
}
36+
pq.offer(new Tuple(edge.dst, curr.cost + passingFees[edge.dst], time));
4637
}
47-
pq.offer(new Tuple(edge.dst, curr.cost + passingFees[edge.dst], time));
4838
}
4939
}
5040
return -1;
@@ -54,8 +44,8 @@ private static class Graph {
5444
private final Map<Integer, List<Edge>> edges = new HashMap<>();
5545

5646
private void addEdge(final int src, final int dst, final int weight) {
57-
this.edges.computeIfAbsent(src, k -> new ArrayList<>()).add(new Edge(src, dst, weight));
58-
this.edges.computeIfAbsent(dst, k -> new ArrayList<>()).add(new Edge(dst, src, weight));
47+
this.edges.computeIfAbsent(src, k -> new ArrayList<>()).add(new Edge(dst, weight));
48+
this.edges.computeIfAbsent(dst, k -> new ArrayList<>()).add(new Edge(src, weight));
5949
}
6050

6151
private List<Edge> getEdges(final int node) {
@@ -64,19 +54,13 @@ private List<Edge> getEdges(final int node) {
6454
}
6555

6656
private static final class Edge {
67-
private final int src;
6857
private final int dst;
6958
private final int weight;
7059

71-
private Edge(final int src, final int dst, final int weight) {
72-
this.src = src;
60+
private Edge(final int dst, final int weight) {
7361
this.dst = dst;
7462
this.weight = weight;
7563
}
76-
77-
public String toString() {
78-
return "(" + src + "," + dst + "," + weight + ")";
79-
}
8064
}
8165

8266
private static class Tuple {
@@ -89,9 +73,5 @@ private Tuple(final int node, final int cost, final int time) {
8973
this.cost = cost;
9074
this.time = time;
9175
}
92-
93-
public String toString() {
94-
return "(" + node + "," + cost + "," + time + ")";
95-
}
9676
}
9777
}

src/main/java/g1901_2000/s1929_concatenation_of_array/Solution.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
public class Solution {
66
public int[] getConcatenation(int[] nums) {
77
int[] result = new int[nums.length * 2];
8-
int i = 0;
9-
for (; i < nums.length; i++) {
10-
result[i] = nums[i];
11-
}
8+
System.arraycopy(nums, 0, result, 0, nums.length);
9+
int i = nums.length;
1210
for (int j = 0; i < result.length && j < nums.length; i++, j++) {
1311
result[i] = nums[j];
1412
}

src/main/java/g1901_2000/s1936_add_minimum_number_of_rungs/Solution.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ public class Solution {
66
public int addRungs(int[] rungs, int dist) {
77
int addons = 0;
88
int currentHeight = 0;
9-
for (int i = 0; i < rungs.length; ) {
9+
int i = 0;
10+
while (i < rungs.length) {
1011
int nextRung = rungs[i];
1112
if (nextRung - currentHeight <= dist) {
1213
currentHeight = nextRung;

0 commit comments

Comments
 (0)