Skip to content

feat: add solutions to lc problem: No.0544 #3316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 39 additions & 22 deletions solution/0500-0599/0544.Output Contest Matches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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$ 为队伍的数量。

<!-- tabs:start -->

Expand All @@ -80,29 +82,29 @@ 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

```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];
}
}
```
Expand All @@ -113,14 +115,16 @@ class Solution {
class Solution {
public:
string findContestMatch(int n) {
vector<string> team(n);
for (int i = 0; i < n; ++i) team[i] = to_string(i + 1);
vector<string> 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];
}
};
```
Expand All @@ -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];
}
```

Expand Down
63 changes: 42 additions & 21 deletions solution/0500-0599/0544.Output Contest Matches/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ Since the third round will generate the final winner, you need to output the ans

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand All @@ -73,29 +79,29 @@ 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

```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];
}
}
```
Expand All @@ -106,14 +112,16 @@ class Solution {
class Solution {
public:
string findContestMatch(int n) {
vector<string> team(n);
for (int i = 0; i < n; ++i) team[i] = to_string(i + 1);
vector<string> 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];
}
};
```
Expand All @@ -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];
}
```

Expand Down
10 changes: 6 additions & 4 deletions solution/0500-0599/0544.Output Contest Matches/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
class Solution {
public:
string findContestMatch(int n) {
vector<string> team(n);
for (int i = 0; i < n; ++i) team[i] = to_string(i + 1);
vector<string> 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];
}
};
13 changes: 6 additions & 7 deletions solution/0500-0599/0544.Output Contest Matches/Solution.go
Original file line number Diff line number Diff line change
@@ -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]
}
12 changes: 6 additions & 6 deletions solution/0500-0599/0544.Output Contest Matches/Solution.java
Original file line number Diff line number Diff line change
@@ -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];
}
}
6 changes: 3 additions & 3 deletions solution/0500-0599/0544.Output Contest Matches/Solution.py
Original file line number Diff line number Diff line change
@@ -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]
9 changes: 9 additions & 0 deletions solution/0500-0599/0544.Output Contest Matches/Solution.ts
Original file line number Diff line number Diff line change
@@ -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];
}
Loading