wolves 3 miesięcy temu
rodzic
commit
f14466081c
3 zmienionych plików z 52 dodań i 0 usunięć
  1. 10 0
      25/12/comp/21q1.cpp
  2. 20 0
      25/12/comp/21q2.cpp
  3. 22 0
      25/12/comp/21q3.cpp

+ 10 - 0
25/12/comp/21q1.cpp

@@ -0,0 +1,10 @@
+#include <string>
+
+class Solution {
+public:
+    int mirrorDistance(int n) {
+        std::string s = std::to_string(n);
+        std::reverse(s.begin(),s.end());
+        return std::abs(n - std::stoi(s));
+    }
+};

+ 20 - 0
25/12/comp/21q2.cpp

@@ -0,0 +1,20 @@
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+class Solution {
+public:
+  long long minCost(std::string s, std::vector<int> &cost) {
+    std::unordered_map<char, long long> m;
+    int n = cost.size();
+    long long sum = 0, ans = LLONG_MAX;
+    for (int i = 0; i < n; i++) {
+      m[s[i]] += cost[i];
+      sum += cost[i];
+    }
+    for (auto [k, v] : m) {
+      ans = std::min(ans, sum - v);
+    }
+    return ans;
+  }
+};

+ 22 - 0
25/12/comp/21q3.cpp

@@ -0,0 +1,22 @@
+#include <unordered_map>
+#include <vector>
+
+class Solution {
+public:
+    int minSwaps(std::vector<int>& nums, std::vector<int>& forbidden) {
+        std::unordered_map<int, int> m1,m2;
+        int n = nums.size();
+        for (int i = 0; i < n; i++)
+        {
+            m1[nums[i]]++;
+            m2[nums[i]]++;
+        }
+        for(auto [k,v] : m1){
+            if (v + m2[k] > n)
+            {
+                return -1;
+            }
+        }
+        // ❎未完成
+    }
+};

备用站点 当前处于降级运行的备用站点,仅供应急访问,数据和功能可能不是最新。