Skip to content

Commit ea2913d

Browse files
committed
Add solution #544
1 parent f498eab commit ea2913d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@
529529
541|[Reverse String II](./solutions/0541-reverse-string-ii.js)|Easy|
530530
542|[01 Matrix](./solutions/0542-01-matrix.js)|Medium|
531531
543|[Diameter of Binary Tree](./solutions/0543-diameter-of-binary-tree.js)|Easy|
532+
544|[Output Contest Matches](./solutions/0544-output-contest-matches.js)|Medium|
532533
546|[Remove Boxes](./solutions/0546-remove-boxes.js)|Hard|
533534
547|[Number of Provinces](./solutions/0547-number-of-provinces.js)|Medium|
534535
551|[Student Attendance Record I](./solutions/0551-student-attendance-record-i.js)|Easy|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 544. Output Contest Matches
3+
* https://leetcode.com/problems/output-contest-matches/
4+
* Difficulty: Medium
5+
*
6+
* During the NBA playoffs, we always set the rather strong team to play with the rather weak
7+
* team, like making the rank 1 team play with the rank nth team, which is a good strategy to
8+
* make the contest more interesting.
9+
*
10+
* Given n teams, return their final contest matches in the form of a string.
11+
*
12+
* The n teams are labeled from 1 to n, which represents their initial rank (i.e., Rank 1 is
13+
* the strongest team and Rank n is the weakest team).
14+
*
15+
* We will use parentheses '(', and ')' and commas ',' to represent the contest team pairing.
16+
* We use the parentheses for pairing and the commas for partition. During the pairing process
17+
* in each round, you always need to follow the strategy of making the rather strong one pair
18+
* with the rather weak one.
19+
*/
20+
21+
/**
22+
* @param {number} n
23+
* @return {string}
24+
*/
25+
var findContestMatch = function(n) {
26+
const teams = Array.from({ length: n }, (_, i) => (i + 1).toString());
27+
return pairTeams(teams);
28+
29+
function pairTeams(arr) {
30+
if (arr.length === 1) return arr[0];
31+
32+
const nextRound = [];
33+
for (let i = 0; i < arr.length / 2; i++) {
34+
nextRound.push(`(${arr[i]},${arr[arr.length - 1 - i]})`);
35+
}
36+
37+
return pairTeams(nextRound);
38+
}
39+
};

0 commit comments

Comments
 (0)