From c5bb4d2f432f361e2781013ec5360301854f6606 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 26 Aug 2024 11:22:13 +0800 Subject: [PATCH] 20240826 finished. --- LeetCodeSharp.Tests/P690Tests.cs | 18 ++++++++ LeetCodeSharp.sln | 6 +++ LeetCodeSharp/LeetCodeSharp.csproj | 4 ++ LeetCodeSharp/Problems/Solution690.cs | 66 +++++++++++++++++++++++++++ README.md | 5 ++ commit.sh | 11 ----- justfile | 22 +++++++++ 7 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 LeetCodeSharp.Tests/P690Tests.cs create mode 100644 LeetCodeSharp/Problems/Solution690.cs create mode 100644 README.md delete mode 100755 commit.sh create mode 100644 justfile diff --git a/LeetCodeSharp.Tests/P690Tests.cs b/LeetCodeSharp.Tests/P690Tests.cs new file mode 100644 index 0000000..ff17004 --- /dev/null +++ b/LeetCodeSharp.Tests/P690Tests.cs @@ -0,0 +1,18 @@ +using LeetCodeSharp.Problems690; + +namespace LeetCodeSharp.Tests; + +public class P690Tests +{ + [Fact] + public void Test1() + { + var solution = new Solution(); + Assert.Equal(11, + solution.GetImportance([ + new Employee(1, 5, [2, 3]), + new Employee(2, 3, []), + new Employee(3, 3, []) + ], 1)); + } +} \ No newline at end of file diff --git a/LeetCodeSharp.sln b/LeetCodeSharp.sln index bd911ad..9f13ded 100644 --- a/LeetCodeSharp.sln +++ b/LeetCodeSharp.sln @@ -9,6 +9,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LeetCodeSharp.Fetcher", "Le EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeetCodeSharp.Tests", "LeetCodeSharp.Tests\LeetCodeSharp.Tests.csproj", "{97E271FD-7244-4DD2-982C-103DF1E92B7C}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1F55C3BE-2E8E-47CB-B46A-7B607A196BA1}" + ProjectSection(SolutionItems) = preProject + justfile = justfile + README.md = README.md + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/LeetCodeSharp/LeetCodeSharp.csproj b/LeetCodeSharp/LeetCodeSharp.csproj index b719c27..cbf69d7 100644 --- a/LeetCodeSharp/LeetCodeSharp.csproj +++ b/LeetCodeSharp/LeetCodeSharp.csproj @@ -6,4 +6,8 @@ disable + + + + diff --git a/LeetCodeSharp/Problems/Solution690.cs b/LeetCodeSharp/Problems/Solution690.cs new file mode 100644 index 0000000..419c179 --- /dev/null +++ b/LeetCodeSharp/Problems/Solution690.cs @@ -0,0 +1,66 @@ +// [690] Employee Importance + + +using System.Collections.Generic; +using System.Linq; + +namespace LeetCodeSharp.Problems690 +{ + // Submission codes start here + + /* +// Definition for Employee. +class Employee { + public int id; + public int importance; + public IList subordinates; +} +*/ + + class Employee + { + public int id; + public int importance; + public IList subordinates; + + public Employee(int id, int importance, IList subordinates) + { + this.id = id; + this.importance = importance; + this.subordinates = subordinates; + } + } + + class Solution + { + public int GetImportance(IList employees, int id) + { + var map = employees.ToDictionary(e => e.id, e => e); + + var visited = new HashSet(); + var queue = new Queue(); + queue.Enqueue(id); + + var result = 0; + while (queue.Count != 0) + { + var now = queue.Dequeue(); + + result += map[now].importance; + if (!visited.Add(now)) + { + continue; + } + + foreach (var subordinate in map[now].subordinates) + { + queue.Enqueue(subordinate); + } + } + + return result; + } + } + + // Submission codes end here +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..70c0168 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# LeetCodeSharp + +使用C#解决[LeetCode](https://leetcode.cn)上的问题,但是当且仅当对应的题目没有Rust的版本时才会使用。 + +使用`just pull `获得对应ID的题目,使用`just commit`提交到远程仓库,使用`just test`运行测试代码。 \ No newline at end of file diff --git a/commit.sh b/commit.sh deleted file mode 100755 index 506d674..0000000 --- a/commit.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -set -e - -time=$(date "+%Y%m%d") -message="$time Finished" - -git add -A -git commit -m "$message" - -git push diff --git a/justfile b/justfile new file mode 100644 index 0000000..726e2df --- /dev/null +++ b/justfile @@ -0,0 +1,22 @@ +#!/usr/bin/env just --justfile + +build: + dotnet build -c Release + +test: + dotnet test + +commit: + #!/usr/bin/env bash + set -euxo pipefail + time=$(date "+%Y%m%d") + message="$time finished." + + git add -A + git commit -m "$message" + git push + +pull id: build + #!/usr/bin/env bash + cd LeetCodeSharp + dotnet run --project ../LeetCodeSharp.Fetcher/LeetCodeSharp.Fetcher.csproj -- {{id}} \ No newline at end of file