commit f17ee1474ae2b77c40b99339e92643326928ef19
parent 4726890803716047d9cc32ac6436a787b13b59f3
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Sat, 27 Jul 2024 16:21:32 +0200
Daily Problem
Diffstat:
2 files changed, 39 insertions(+), 0 deletions(-)
diff --git a/Problems/2976.cpp b/Problems/2976.cpp
@@ -0,0 +1,38 @@
+class Solution {
+ public:
+ long long minimumCost(const string &source, const string &target, const vector<char> &original,
+ const vector<char> &changed, const vector<int> &cost) const {
+ static unsigned long long path[26][26];
+ memset(path, 0xFF, sizeof(path));
+
+ for (int i = 0; i < size(cost); i++) {
+ auto &cpath = path[original[i] - 'a'][changed[i] - 'a'];
+ cpath = min(cpath, (unsigned long long)cost[i]);
+ }
+
+ for (int k = 0; k < 26; k++) {
+ for (int i = 0; i < 26; i++) {
+ if (path[i][k] == -1) continue;
+
+ for (int j = 0; j < 26; j++) {
+ if (path[k][j] == -1) continue;
+
+ path[i][j] = min(path[i][j], path[i][k] + path[k][j]);
+ }
+ }
+ }
+
+ long long res = 0;
+
+ for (int i = 0; i < size(source); i++) {
+ if (source[i] == target[i]) continue;
+
+ const auto cost = path[source[i] - 'a'][target[i] - 'a'];
+ if (cost == -1) return -1;
+
+ res += cost;
+ }
+
+ return res;
+ }
+};
diff --git a/README.md b/README.md
@@ -1279,6 +1279,7 @@ for solving problems.
| 2962 | Medium | [Count Subarrays Where Max Element Appears at Least K Times](Problems/2962.cpp) |
| 2966 | Medium | [Divide Array Into Arrays With Max Difference](Problems/2966.cpp) |
| 2971 | Medium | [Find Polygon With the Largest Perimeter](Problems/2971.cpp) |
+| 2976 | Medium | [Minimum Cost to Convert String I](Problems/2976.cpp) |
| 2997 | Medium | [Minimum Number of Operations to Make Array XOR Equal to K](Problems/2997.cpp) |
| 3005 | Easy | [Count Elements With Maximum Frequency](Problems/3005.cpp) |
| 3011 | Medium | [Find if Array Can Be Sorted](Problems/3011.cpp) |