Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 04a84f3

Browse files
feat: add solutions to lc problem: No.0544 (doocs#3316)
No.0544.Output Contest Matches
1 parent 9226ec0 commit 04a84f3

File tree

7 files changed

+111
-63
lines changed

7 files changed

+111
-63
lines changed

‎solution/0500-0599/0544.Output Contest Matches/README.md‎

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ tags:
6969

7070
### 方法一:模拟
7171

72-
假设 `team[i]` 为当前轮次中第 i 强的队伍
72+
我们可以用一个长度为 $n$ 的数组 $s$ 来存储每个队伍的编号,然后模拟比赛的过程
7373

74-
每一轮,将第 i 支队伍变成 `"(" + team[i] + "," + team[n-1-i] + ")"`,并且每一轮淘汰一半的队伍。
74+
每一轮比赛,我们将数组 $s$ 中的前 $n$ 个元素两两配对,然后将胜者的编号存入数组 $s$ 的前 $n/2$ 个位置。然后,我们将 $n$ 减半,继续进行下一轮比赛,直到 $n$ 减半为 1ドル,ドル此时数组 $s$ 中的第一个元素即为最终的比赛匹配方案。
75+
76+
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(n \times \log n)$。其中 $n$ 为队伍的数量。
7577

7678
<!-- tabs:start -->
7779

@@ -80,29 +82,29 @@ tags:
8082
```python
8183
class Solution:
8284
def findContestMatch(self, n: int) -> str:
83-
team = [str(i + 1) for i in range(n)]
85+
s = [str(i + 1) for i in range(n)]
8486
while n > 1:
8587
for i in range(n >> 1):
86-
team[i] = f'({team[i]},{team[n - 1 - i]})'
88+
s[i] = f"({s[i]},{s[n - i - 1]})"
8789
n >>= 1
88-
return team[0]
90+
return s[0]
8991
```
9092

9193
#### Java
9294

9395
```java
9496
class Solution {
9597
public String findContestMatch(int n) {
96-
String[] team = new String[n];
98+
String[] s = new String[n];
9799
for (int i = 0; i < n; ++i) {
98-
team[i] = ""+(i + 1);
100+
s[i] = String.valueOf(i + 1);
99101
}
100-
for (; n > 1; n /=2) {
101-
for (int i = 0; i < n /2; ++i) {
102-
team[i] = "("+ team[i]+","+ team[n - 1 - i] +")";
102+
for (; n > 1; n >>=1) {
103+
for (int i = 0; i < n >>1; ++i) {
104+
s[i] = String.format("(%s,%s)", s[i], s[n - i - 1]);
103105
}
104106
}
105-
return team[0];
107+
return s[0];
106108
}
107109
}
108110
```
@@ -113,14 +115,16 @@ class Solution {
113115
class Solution {
114116
public:
115117
string findContestMatch(int n) {
116-
vector<string> team(n);
117-
for (int i = 0; i < n; ++i) team[i] = to_string(i + 1);
118+
vector<string> s(n);
119+
for (int i = 0; i < n; ++i) {
120+
s[i] = to_string(i + 1);
121+
}
118122
for (; n > 1; n >>= 1) {
119123
for (int i = 0; i < n >> 1; ++i) {
120-
team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")";
124+
s[i] = "(" + s[i] + "," + s[n - i - 1] + ")";
121125
}
122126
}
123-
return team[0];
127+
return s[0];
124128
}
125129
};
126130
```
@@ -129,17 +133,30 @@ public:
129133
130134
```go
131135
func findContestMatch(n int) string {
132-
team := make([]string, n)
133-
for i := range team {
134-
team[i] = strconv.Itoa(i + 1)
136+
s := make([]string, n)
137+
for i := 0; i < n; i++ {
138+
s[i] = strconv.Itoa(i + 1)
135139
}
136-
for n > 1 {
140+
for ; n > 1; n >>= 1 {
137141
for i := 0; i < n>>1; i++ {
138-
team[i] = "(" + team[i] + "," + team[n-1-i] + ")"
142+
s[i] = fmt.Sprintf("(%s,%s)", s[i], s[n-i-1])
139143
}
140-
n >>= 1
141144
}
142-
return team[0]
145+
return s[0]
146+
}
147+
```
148+
149+
#### TypeScript
150+
151+
```ts
152+
function findContestMatch(n: number): string {
153+
const s: string[] = Array.from({ length: n }, (_, i) => (i + 1).toString());
154+
for (; n > 1; n >>= 1) {
155+
for (let i = 0; i < n >> 1; ++i) {
156+
s[i] = `(${s[i]},${s[n - i - 1]})`;
157+
}
158+
}
159+
return s[0];
143160
}
144161
```
145162

‎solution/0500-0599/0544.Output Contest Matches/README_EN.md‎

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ Since the third round will generate the final winner, you need to output the ans
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Simulation
68+
69+
We can use an array $s$ of length $n$ to store the ID of each team, and then simulate the process of the matches.
70+
71+
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.
72+
73+
The time complexity is $O(n \log n),ドル and the space complexity is $O(n)$. Here, $n$ is the number of teams.
6874

6975
<!-- tabs:start -->
7076

@@ -73,29 +79,29 @@ Since the third round will generate the final winner, you need to output the ans
7379
```python
7480
class Solution:
7581
def findContestMatch(self, n: int) -> str:
76-
team = [str(i + 1) for i in range(n)]
82+
s = [str(i + 1) for i in range(n)]
7783
while n > 1:
7884
for i in range(n >> 1):
79-
team[i] = f'({team[i]},{team[n - 1 - i]})'
85+
s[i] = f"({s[i]},{s[n - i - 1]})"
8086
n >>= 1
81-
return team[0]
87+
return s[0]
8288
```
8389

8490
#### Java
8591

8692
```java
8793
class Solution {
8894
public String findContestMatch(int n) {
89-
String[] team = new String[n];
95+
String[] s = new String[n];
9096
for (int i = 0; i < n; ++i) {
91-
team[i] = ""+(i + 1);
97+
s[i] = String.valueOf(i + 1);
9298
}
93-
for (; n > 1; n /=2) {
94-
for (int i = 0; i < n /2; ++i) {
95-
team[i] = "("+ team[i]+","+ team[n - 1 - i] +")";
99+
for (; n > 1; n >>=1) {
100+
for (int i = 0; i < n >>1; ++i) {
101+
s[i] = String.format("(%s,%s)", s[i], s[n - i - 1]);
96102
}
97103
}
98-
return team[0];
104+
return s[0];
99105
}
100106
}
101107
```
@@ -106,14 +112,16 @@ class Solution {
106112
class Solution {
107113
public:
108114
string findContestMatch(int n) {
109-
vector<string> team(n);
110-
for (int i = 0; i < n; ++i) team[i] = to_string(i + 1);
115+
vector<string> s(n);
116+
for (int i = 0; i < n; ++i) {
117+
s[i] = to_string(i + 1);
118+
}
111119
for (; n > 1; n >>= 1) {
112120
for (int i = 0; i < n >> 1; ++i) {
113-
team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")";
121+
s[i] = "(" + s[i] + "," + s[n - i - 1] + ")";
114122
}
115123
}
116-
return team[0];
124+
return s[0];
117125
}
118126
};
119127
```
@@ -122,17 +130,30 @@ public:
122130
123131
```go
124132
func findContestMatch(n int) string {
125-
team := make([]string, n)
126-
for i := range team {
127-
team[i] = strconv.Itoa(i + 1)
133+
s := make([]string, n)
134+
for i := 0; i < n; i++ {
135+
s[i] = strconv.Itoa(i + 1)
128136
}
129-
for n > 1 {
137+
for ; n > 1; n >>= 1 {
130138
for i := 0; i < n>>1; i++ {
131-
team[i] = "(" + team[i] + "," + team[n-1-i] + ")"
139+
s[i] = fmt.Sprintf("(%s,%s)", s[i], s[n-i-1])
132140
}
133-
n >>= 1
134141
}
135-
return team[0]
142+
return s[0]
143+
}
144+
```
145+
146+
#### TypeScript
147+
148+
```ts
149+
function findContestMatch(n: number): string {
150+
const s: string[] = Array.from({ length: n }, (_, i) => (i + 1).toString());
151+
for (; n > 1; n >>= 1) {
152+
for (let i = 0; i < n >> 1; ++i) {
153+
s[i] = `(${s[i]},${s[n - i - 1]})`;
154+
}
155+
}
156+
return s[0];
136157
}
137158
```
138159

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
class Solution {
22
public:
33
string findContestMatch(int n) {
4-
vector<string> team(n);
5-
for (int i = 0; i < n; ++i) team[i] = to_string(i + 1);
4+
vector<string> s(n);
5+
for (int i = 0; i < n; ++i) {
6+
s[i] = to_string(i + 1);
7+
}
68
for (; n > 1; n >>= 1) {
79
for (int i = 0; i < n >> 1; ++i) {
8-
team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")";
10+
s[i] = "(" + s[i] + "," + s[n - i - 1] + ")";
911
}
1012
}
11-
return team[0];
13+
return s[0];
1214
}
1315
};
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
func findContestMatch(n int) string {
2-
team := make([]string, n)
3-
for i := rangeteam {
4-
team[i] = strconv.Itoa(i + 1)
2+
s := make([]string, n)
3+
for i := 0; i<n; i++ {
4+
s[i] = strconv.Itoa(i + 1)
55
}
6-
for n > 1 {
6+
for ; n >1; n>>= 1 {
77
for i := 0; i < n>>1; i++ {
8-
team[i] = "("+team[i]+","+team[n-1-i] +")"
8+
s[i] = fmt.Sprintf("(%s,%s)", s[i], s[n-i-1])
99
}
10-
n >>= 1
1110
}
12-
return team[0]
11+
return s[0]
1312
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public String findContestMatch(int n) {
3-
String[] team = new String[n];
3+
String[] s = new String[n];
44
for (int i = 0; i < n; ++i) {
5-
team[i] = "" + (i + 1);
5+
s[i] = String.valueOf(i + 1);
66
}
7-
for (; n > 1; n /= 2) {
8-
for (int i = 0; i < n / 2; ++i) {
9-
team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")";
7+
for (; n > 1; n >>= 1) {
8+
for (int i = 0; i < n >> 1; ++i) {
9+
s[i] = String.format("(%s,%s)", s[i], s[n - i - 1]);
1010
}
1111
}
12-
return team[0];
12+
return s[0];
1313
}
1414
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def findContestMatch(self, n: int) -> str:
3-
team = [str(i + 1) for i in range(n)]
3+
s = [str(i + 1) for i in range(n)]
44
while n > 1:
55
for i in range(n >> 1):
6-
team[i] = f'({team[i]},{team[n - 1 - i]})'
6+
s[i] = f"({s[i]},{s[n - i - 1]})"
77
n >>= 1
8-
return team[0]
8+
return s[0]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function findContestMatch(n: number): string {
2+
const s: string[] = Array.from({ length: n }, (_, i) => (i + 1).toString());
3+
for (; n > 1; n >>= 1) {
4+
for (let i = 0; i < n >> 1; ++i) {
5+
s[i] = `(${s[i]},${s[n - i - 1]})`;
6+
}
7+
}
8+
return s[0];
9+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /