diff --git a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md index 817f8fbc6cb69..797701b8ac2a0 100644 --- a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md +++ b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md @@ -365,6 +365,27 @@ impl Solution { } ``` +### **C#** + +```cs +public class Solution { + public int GarbageCollection(string[] garbage, int[] travel) { + int len = garbage.Length; + int res = 0; + HashSet s = new HashSet(); + for (int i = len - 1; i >= 0; i--) { + foreach (char ch in garbage[i].ToCharArray()) { + if (!s.Contains(ch)) + s.Add(ch); + } + res += garbage[i].Length; + res += i > 0 ? s.Count * travel[i - 1] : 0; + } + return res; + } +} +``` + ### **...** ``` diff --git a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md index d9a3dc8f2d49d..b631efb851fb2 100644 --- a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md +++ b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md @@ -346,6 +346,27 @@ impl Solution { } ``` +### **C#** + +```cs +public class Solution { + public int GarbageCollection(string[] garbage, int[] travel) { + int len = garbage.Length; + int res = 0; + HashSet s = new HashSet(); + for (int i = len - 1; i >= 0; i--) { + foreach (char ch in garbage[i].ToCharArray()) { + if (!s.Contains(ch)) + s.Add(ch); + } + res += garbage[i].Length; + res += i > 0 ? s.Count * travel[i - 1] : 0; + } + return res; + } +} +``` + ### **...** ``` diff --git a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/Solution.cs b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/Solution.cs new file mode 100644 index 0000000000000..d6f56689f606f --- /dev/null +++ b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/Solution.cs @@ -0,0 +1,16 @@ +public class Solution { + public int GarbageCollection(string[] garbage, int[] travel) { + int len = garbage.Length; + int res = 0; + HashSet s = new HashSet(); + for (int i = len - 1; i >= 0; i--) { + foreach (char ch in garbage[i].ToCharArray()) { + if (!s.Contains(ch)) + s.Add(ch); + } + res += garbage[i].Length; + res += i > 0 ? s.Count * travel[i - 1] : 0; + } + return res; + } +}