From 292d743db369e95850a4527071b124c0e5c4f311 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 24 Jul 2024 12:55:16 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0544 No.0544.Output Contest Matches --- .../0544.Output Contest Matches/README.md | 61 +++++++++++------- .../0544.Output Contest Matches/README_EN.md | 63 ++++++++++++------- .../0544.Output Contest Matches/Solution.cpp | 10 +-- .../0544.Output Contest Matches/Solution.go | 13 ++-- .../0544.Output Contest Matches/Solution.java | 12 ++-- .../0544.Output Contest Matches/Solution.py | 6 +- .../0544.Output Contest Matches/Solution.ts | 9 +++ 7 files changed, 111 insertions(+), 63 deletions(-) create mode 100644 solution/0500-0599/0544.Output Contest Matches/Solution.ts diff --git a/solution/0500-0599/0544.Output Contest Matches/README.md b/solution/0500-0599/0544.Output Contest Matches/README.md index 75765760356ee..6a0ea9563e040 100644 --- a/solution/0500-0599/0544.Output Contest Matches/README.md +++ b/solution/0500-0599/0544.Output Contest Matches/README.md @@ -69,9 +69,11 @@ tags: ### 方法一:模拟 -假设 `team[i]` 为当前轮次中第 i 强的队伍。 +我们可以用一个长度为 $n$ 的数组 $s$ 来存储每个队伍的编号,然后模拟比赛的过程。 -每一轮,将第 i 支队伍变成 `"(" + team[i] + "," + team[n-1-i] + ")"`,并且每一轮淘汰一半的队伍。 +每一轮比赛,我们将数组 $s$ 中的前 $n$ 个元素两两配对,然后将胜者的编号存入数组 $s$ 的前 $n/2$ 个位置。然后,我们将 $n$ 减半,继续进行下一轮比赛,直到 $n$ 减半为 $1$,此时数组 $s$ 中的第一个元素即为最终的比赛匹配方案。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n \times \log n)$。其中 $n$ 为队伍的数量。 @@ -80,12 +82,12 @@ tags: ```python class Solution: def findContestMatch(self, n: int) -> str: - team = [str(i + 1) for i in range(n)] + s = [str(i + 1) for i in range(n)] while n > 1: for i in range(n >> 1): - team[i] = f'({team[i]},{team[n - 1 - i]})' + s[i] = f"({s[i]},{s[n - i - 1]})" n >>= 1 - return team[0] + return s[0] ``` #### Java @@ -93,16 +95,16 @@ class Solution: ```java class Solution { public String findContestMatch(int n) { - String[] team = new String[n]; + String[] s = new String[n]; for (int i = 0; i < n; ++i) { - team[i] = "" + (i + 1); + s[i] = String.valueOf(i + 1); } - for (; n > 1; n /= 2) { - for (int i = 0; i < n / 2; ++i) { - team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")"; + for (; n > 1; n >>= 1) { + for (int i = 0; i < n >> 1; ++i) { + s[i] = String.format("(%s,%s)", s[i], s[n - i - 1]); } } - return team[0]; + return s[0]; } } ``` @@ -113,14 +115,16 @@ class Solution { class Solution { public: string findContestMatch(int n) { - vector team(n); - for (int i = 0; i < n; ++i) team[i] = to_string(i + 1); + vector s(n); + for (int i = 0; i < n; ++i) { + s[i] = to_string(i + 1); + } for (; n > 1; n >>= 1) { for (int i = 0; i < n >> 1; ++i) { - team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")"; + s[i] = "(" + s[i] + "," + s[n - i - 1] + ")"; } } - return team[0]; + return s[0]; } }; ``` @@ -129,17 +133,30 @@ public: ```go func findContestMatch(n int) string { - team := make([]string, n) - for i := range team { - team[i] = strconv.Itoa(i + 1) + s := make([]string, n) + for i := 0; i < n; i++ { + s[i] = strconv.Itoa(i + 1) } - for n > 1 { + for ; n > 1; n >>= 1 { for i := 0; i < n>>1; i++ { - team[i] = "(" + team[i] + "," + team[n-1-i] + ")" + s[i] = fmt.Sprintf("(%s,%s)", s[i], s[n-i-1]) } - n >>= 1 } - return team[0] + return s[0] +} +``` + +#### TypeScript + +```ts +function findContestMatch(n: number): string { + const s: string[] = Array.from({ length: n }, (_, i) => (i + 1).toString()); + for (; n > 1; n >>= 1) { + for (let i = 0; i < n >> 1; ++i) { + s[i] = `(${s[i]},${s[n - i - 1]})`; + } + } + return s[0]; } ``` diff --git a/solution/0500-0599/0544.Output Contest Matches/README_EN.md b/solution/0500-0599/0544.Output Contest Matches/README_EN.md index f0a08ac564d7d..ba2198e77d293 100644 --- a/solution/0500-0599/0544.Output Contest Matches/README_EN.md +++ b/solution/0500-0599/0544.Output Contest Matches/README_EN.md @@ -64,7 +64,13 @@ Since the third round will generate the final winner, you need to output the ans -### Solution 1 +### Solution 1: Simulation + +We can use an array $s$ of length $n$ to store the ID of each team, and then simulate the process of the matches. + +In each round of matches, we pair up the first $n$ elements in array $s$ two by two, and then store the ID of the winners in the first $n/2$ positions of array $s$. After that, we halve $n$ and continue to the next round of matches, until $n$ is reduced to $1$. At this point, the first element in array $s$ is the final match-up scheme. + +The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of teams. @@ -73,12 +79,12 @@ Since the third round will generate the final winner, you need to output the ans ```python class Solution: def findContestMatch(self, n: int) -> str: - team = [str(i + 1) for i in range(n)] + s = [str(i + 1) for i in range(n)] while n > 1: for i in range(n >> 1): - team[i] = f'({team[i]},{team[n - 1 - i]})' + s[i] = f"({s[i]},{s[n - i - 1]})" n >>= 1 - return team[0] + return s[0] ``` #### Java @@ -86,16 +92,16 @@ class Solution: ```java class Solution { public String findContestMatch(int n) { - String[] team = new String[n]; + String[] s = new String[n]; for (int i = 0; i < n; ++i) { - team[i] = "" + (i + 1); + s[i] = String.valueOf(i + 1); } - for (; n > 1; n /= 2) { - for (int i = 0; i < n / 2; ++i) { - team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")"; + for (; n > 1; n >>= 1) { + for (int i = 0; i < n >> 1; ++i) { + s[i] = String.format("(%s,%s)", s[i], s[n - i - 1]); } } - return team[0]; + return s[0]; } } ``` @@ -106,14 +112,16 @@ class Solution { class Solution { public: string findContestMatch(int n) { - vector team(n); - for (int i = 0; i < n; ++i) team[i] = to_string(i + 1); + vector s(n); + for (int i = 0; i < n; ++i) { + s[i] = to_string(i + 1); + } for (; n > 1; n >>= 1) { for (int i = 0; i < n >> 1; ++i) { - team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")"; + s[i] = "(" + s[i] + "," + s[n - i - 1] + ")"; } } - return team[0]; + return s[0]; } }; ``` @@ -122,17 +130,30 @@ public: ```go func findContestMatch(n int) string { - team := make([]string, n) - for i := range team { - team[i] = strconv.Itoa(i + 1) + s := make([]string, n) + for i := 0; i < n; i++ { + s[i] = strconv.Itoa(i + 1) } - for n > 1 { + for ; n > 1; n >>= 1 { for i := 0; i < n>>1; i++ { - team[i] = "(" + team[i] + "," + team[n-1-i] + ")" + s[i] = fmt.Sprintf("(%s,%s)", s[i], s[n-i-1]) } - n >>= 1 } - return team[0] + return s[0] +} +``` + +#### TypeScript + +```ts +function findContestMatch(n: number): string { + const s: string[] = Array.from({ length: n }, (_, i) => (i + 1).toString()); + for (; n > 1; n >>= 1) { + for (let i = 0; i < n >> 1; ++i) { + s[i] = `(${s[i]},${s[n - i - 1]})`; + } + } + return s[0]; } ``` diff --git a/solution/0500-0599/0544.Output Contest Matches/Solution.cpp b/solution/0500-0599/0544.Output Contest Matches/Solution.cpp index 9c82aff11659c..61a2e8360e80a 100644 --- a/solution/0500-0599/0544.Output Contest Matches/Solution.cpp +++ b/solution/0500-0599/0544.Output Contest Matches/Solution.cpp @@ -1,13 +1,15 @@ class Solution { public: string findContestMatch(int n) { - vector team(n); - for (int i = 0; i < n; ++i) team[i] = to_string(i + 1); + vector s(n); + for (int i = 0; i < n; ++i) { + s[i] = to_string(i + 1); + } for (; n > 1; n >>= 1) { for (int i = 0; i < n >> 1; ++i) { - team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")"; + s[i] = "(" + s[i] + "," + s[n - i - 1] + ")"; } } - return team[0]; + return s[0]; } }; \ No newline at end of file diff --git a/solution/0500-0599/0544.Output Contest Matches/Solution.go b/solution/0500-0599/0544.Output Contest Matches/Solution.go index 1e51283d70a00..edb7ea5eb04ea 100644 --- a/solution/0500-0599/0544.Output Contest Matches/Solution.go +++ b/solution/0500-0599/0544.Output Contest Matches/Solution.go @@ -1,13 +1,12 @@ func findContestMatch(n int) string { - team := make([]string, n) - for i := range team { - team[i] = strconv.Itoa(i + 1) + s := make([]string, n) + for i := 0; i < n; i++ { + s[i] = strconv.Itoa(i + 1) } - for n > 1 { + for ; n > 1; n >>= 1 { for i := 0; i < n>>1; i++ { - team[i] = "(" + team[i] + "," + team[n-1-i] + ")" + s[i] = fmt.Sprintf("(%s,%s)", s[i], s[n-i-1]) } - n >>= 1 } - return team[0] + return s[0] } \ No newline at end of file diff --git a/solution/0500-0599/0544.Output Contest Matches/Solution.java b/solution/0500-0599/0544.Output Contest Matches/Solution.java index 986ae52900a04..be60130326c4e 100644 --- a/solution/0500-0599/0544.Output Contest Matches/Solution.java +++ b/solution/0500-0599/0544.Output Contest Matches/Solution.java @@ -1,14 +1,14 @@ class Solution { public String findContestMatch(int n) { - String[] team = new String[n]; + String[] s = new String[n]; for (int i = 0; i < n; ++i) { - team[i] = "" + (i + 1); + s[i] = String.valueOf(i + 1); } - for (; n > 1; n /= 2) { - for (int i = 0; i < n / 2; ++i) { - team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")"; + for (; n > 1; n >>= 1) { + for (int i = 0; i < n >> 1; ++i) { + s[i] = String.format("(%s,%s)", s[i], s[n - i - 1]); } } - return team[0]; + return s[0]; } } \ No newline at end of file diff --git a/solution/0500-0599/0544.Output Contest Matches/Solution.py b/solution/0500-0599/0544.Output Contest Matches/Solution.py index ddfcf132d8faa..6772b8dbbfcbe 100644 --- a/solution/0500-0599/0544.Output Contest Matches/Solution.py +++ b/solution/0500-0599/0544.Output Contest Matches/Solution.py @@ -1,8 +1,8 @@ class Solution: def findContestMatch(self, n: int) -> str: - team = [str(i + 1) for i in range(n)] + s = [str(i + 1) for i in range(n)] while n > 1: for i in range(n >> 1): - team[i] = f'({team[i]},{team[n - 1 - i]})' + s[i] = f"({s[i]},{s[n - i - 1]})" n >>= 1 - return team[0] + return s[0] diff --git a/solution/0500-0599/0544.Output Contest Matches/Solution.ts b/solution/0500-0599/0544.Output Contest Matches/Solution.ts new file mode 100644 index 0000000000000..9bd39d9682b51 --- /dev/null +++ b/solution/0500-0599/0544.Output Contest Matches/Solution.ts @@ -0,0 +1,9 @@ +function findContestMatch(n: number): string { + const s: string[] = Array.from({ length: n }, (_, i) => (i + 1).toString()); + for (; n > 1; n >>= 1) { + for (let i = 0; i < n >> 1; ++i) { + s[i] = `(${s[i]},${s[n - i - 1]})`; + } + } + return s[0]; +}