Skip to content

Commit a2bd76a

Browse files
add 2976
1 parent 201c941 commit a2bd76a

File tree

3 files changed

+117
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+117
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------|--------------------------------|----------------------------------------------------------------------
3+
| 2976 | [Minimum Cost to Convert String I](https://leetcode.com/problems/minimum-cost-to-convert-string-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2976.java) | | Medium | Graph, Shortest Path
34
| 2974 | [Minimum Number Game](https://leetcode.com/problems/minimum-number-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2974.java) | | Easy |
45
| 2970 | [Count the Number of Incremovable Subarrays I](https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2970.java) | | Easy |
56
| 2965 | [Find Missing and Repeated Values](https://leetcode.com/problems/find-missing-and-repeated-values/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2965.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Queue;
10+
11+
public class _2976 {
12+
public static class Solution1 {
13+
/**
14+
* My completely original solution to use Dijkstra's algorithm.
15+
* Dijkstra's algorithm is the way to go for finding
16+
* the shortest path in a weighted (non-negative) graph.
17+
*/
18+
public long minimumCost(String source, String target, char[] original, char[] changed, int[] cost) {
19+
int ALPHABET_SIZE = 26;
20+
List<int[]>[] graph = new ArrayList[ALPHABET_SIZE];
21+
for (int i = 0; i < ALPHABET_SIZE; i++) {
22+
graph[i] = new ArrayList<>();
23+
}
24+
for (int i = 0; i < original.length; i++) {
25+
graph[original[i] - 'a'].add(new int[]{changed[i] - 'a', cost[i]});
26+
}
27+
long minCost = 0L;
28+
Map<String, Long> cache = new HashMap<>();
29+
for (int i = 0; i < source.length(); i++) {
30+
long thisCost = dijkstra(source.charAt(i) - 'a', target.charAt(i) - 'a', graph, cache);
31+
if (thisCost != -1) {
32+
minCost += thisCost;
33+
} else {
34+
return -1;
35+
}
36+
}
37+
return minCost;
38+
}
39+
40+
private long dijkstra(int source, int target, List<int[]>[] graph, Map<String, Long> cache) {
41+
if (cache.containsKey(source + "->" + target)) {
42+
return cache.get(source + "->" + target);
43+
}
44+
int[] minCosts = new int[26];
45+
Arrays.fill(minCosts, Integer.MAX_VALUE);
46+
minCosts[source] = 0;
47+
Queue<int[]> q = new LinkedList<>();
48+
q.offer(new int[]{source, 0});
49+
while (!q.isEmpty()) {
50+
int[] curr = q.poll();
51+
int currNode = curr[0];
52+
int currCost = curr[1];
53+
if (currCost > minCosts[currNode]) {
54+
continue;
55+
}
56+
for (int[] neighbor : graph[currNode]) {
57+
int neighborNode = neighbor[0];
58+
int neighborCost = neighbor[1];
59+
if (currCost + neighborCost < minCosts[neighborNode]) {
60+
minCosts[neighborNode] = currCost + neighborCost;
61+
q.offer(new int[]{neighborNode, minCosts[neighborNode]});
62+
}
63+
}
64+
}
65+
if (minCosts[target] == Integer.MAX_VALUE) {
66+
minCosts[target] = -1;
67+
}
68+
cache.put(source + "->" + target, (long) minCosts[target]);
69+
return cache.getOrDefault(source + "->" + target, -1L);
70+
}
71+
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2976;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _2976Test {
10+
private static _2976.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2976.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(28, solution1.minimumCost
20+
("abcd", "acbe",
21+
new char[]{'a', 'b', 'c', 'c', 'e', 'd'},
22+
new char[]{'b', 'c', 'b', 'e', 'b', 'e'},
23+
new int[]{2, 5, 5, 1, 2, 20}));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
assertEquals(12, solution1.minimumCost
29+
("aaaa", "bbbb",
30+
new char[]{'a', 'c'},
31+
new char[]{'c', 'b'},
32+
new int[]{1, 2}));
33+
}
34+
35+
@Test
36+
public void test3() {
37+
assertEquals(-1, solution1.minimumCost
38+
("abcd", "abce",
39+
new char[]{'a'},
40+
new char[]{'e'},
41+
new int[]{10000}));
42+
}
43+
}

0 commit comments

Comments
 (0)