Trung bình 1 người đã giải Tỷ lệ chấp nhận 100.0%

Tournament Standings

Rank teams from match records.

Bạn sẽ học được gì

  • Parse match results from text
  • Aggregate points per team in a dictionary
  • Sort by points, then by name

Đề bài

Xem bản dịch

Rank teams from match records. Each record has the form First;Second;result, with no extra spaces. Result is win, loss, or draw from First’s perspective. A win earns 3 points, a draw earns 1 per team, and a loss earns 0. Count matches played, wins, draws, losses, and points for each team that appears. Sort by descending points, then ascending team name (ASCII order).

Return one semicolon-separated row per team: Name;Played;Won;Drawn;Lost;Points. Do not include a header or alignment spaces.

Implement tournament_standings(matches).

Input

matches: Match records, in any order.

Output

Return ranked rows with the six fields described above. Write numbers without leading zeros.

Ràng buộc

0…10,000 valid records. Team names contain 1…30 ASCII letters and are case-sensitive. Each match has two different names. Repeated matches are separate games. Empty input gives an empty result.

Ví dụ

Ví dụ 1
Đầu vào:
matches = ["Falcons;Bears;win", "Bears;Owls;draw"]

Kết quả mong đợi:
expected = ["Falcons;1;1;0;0;3", "Bears;2;0;1;1;1", "Owls;1;0;1;0;1"]
Đầu vào:
std::vector<std::string> matches = {"Falcons;Bears;win", "Bears;Owls;draw"};

Kết quả mong đợi:
std::vector<std::string> expected = {"Falcons;1;1;0;0;3", "Bears;2;0;1;1;1", "Owls;1;0;1;0;1"};
Đầu vào:
string[] matches = new string[] {"Falcons;Bears;win", "Bears;Owls;draw"};

Kết quả mong đợi:
string[] expected = new string[] {"Falcons;1;1;0;0;3", "Bears;2;0;1;1;1", "Owls;1;0;1;0;1"};

Giải thích

Falcons have 3 points from their win. Bears and Owls each have 1 point from the draw, so Bears come first alphabetically. Bears played twice and lost once.

Ví dụ 2
Đầu vào:
matches = ["Zebras;Ants;loss"]

Kết quả mong đợi:
expected = ["Ants;1;1;0;0;3", "Zebras;1;0;0;1;0"]
Đầu vào:
std::vector<std::string> matches = {"Zebras;Ants;loss"};

Kết quả mong đợi:
std::vector<std::string> expected = {"Ants;1;1;0;0;3", "Zebras;1;0;0;1;0"};
Đầu vào:
string[] matches = new string[] {"Zebras;Ants;loss"};

Kết quả mong đợi:
string[] expected = new string[] {"Ants;1;1;0;0;3", "Zebras;1;0;0;1;0"};

Giải thích

Loss is from Zebras’ perspective: Ants won and receive 3 points. Zebras receive 0.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Accumulate stats for every team seen in the match list, even if it only loses.

Gợi ý 2

Each record updates two teams at once, and the result is always from the first team's perspective.

Gợi ý 3

After collecting totals, sort by points descending and then by team name ascending.

Độ phức tạp mong đợi

O(m log t) time, O(t) extra space

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 Н"Никита "Programmer" Python 968 ms - 5/5 Xem code

Học hỏi từ người khác

Lời giải cộng đồng

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem code của người khác và nhận xét của AI. Đăng nhập và giải thử thách để tham gia thảo luận.