LeetCode 1212. 查询球队积分

题目描述

1212. 查询球队积分

思路分析

解法一:哈希表统计积分(推荐)

核心思路

  • 用哈希表累计每支球队的积分。
  • 遍历比赛记录:胜 3 分、平 1 分、负 0 分,分别加到主队和客队。
  • 最后汇总球队信息并按积分降序、球队编号升序排序。


复杂度分析

  • 时间复杂度:O(t + m log t),其中 t 为球队数,m 为比赛数。
  • 空间复杂度:O(t),存储每支球队积分。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {
    public List<TeamScore> teamScores(List<Team> teams, List<Match> matches) {
        Map<Integer, Integer> score = new HashMap<>();
        for (Team t : teams) {
            score.put(t.teamId, 0);
        }

        for (Match m : matches) {
            int host = m.hostTeam;
            int guest = m.guestTeam;
            if (m.hostGoals > m.guestGoals) {
                score.put(host, score.get(host) + 3);
            } else if (m.hostGoals < m.guestGoals) {
                score.put(guest, score.get(guest) + 3);
            } else {
                score.put(host, score.get(host) + 1);
                score.put(guest, score.get(guest) + 1);
            }
        }

        List<TeamScore> res = new ArrayList<>();
        for (Team t : teams) {
            res.add(new TeamScore(t.teamId, t.teamName, score.get(t.teamId)));
        }

        Collections.sort(res, (a, b) -> {
            if (a.numPoints != b.numPoints) {
                return b.numPoints - a.numPoints;
            }
            return a.teamId - b.teamId;
        });
        return res;
    }

    static class Team {
        int teamId;
        String teamName;
        Team(int teamId, String teamName) {
            this.teamId = teamId;
            this.teamName = teamName;
        }
    }

    static class Match {
        int hostTeam;
        int guestTeam;
        int hostGoals;
        int guestGoals;
        Match(int hostTeam, int guestTeam, int hostGoals, int guestGoals) {
            this.hostTeam = hostTeam;
            this.guestTeam = guestTeam;
            this.hostGoals = hostGoals;
            this.guestGoals = guestGoals;
        }
    }

    static class TeamScore {
        int teamId;
        String teamName;
        int numPoints;
        TeamScore(int teamId, String teamName, int numPoints) {
            this.teamId = teamId;
            this.teamName = teamName;
            this.numPoints = numPoints;
        }
    }
}
import "sort"

type Team struct {
    TeamId   int
    TeamName string
}

type Match struct {
    HostTeam  int
    GuestTeam int
    HostGoals int
    GuestGoals int
}

type TeamScore struct {
    TeamId   int
    TeamName string
    NumPoints int
}

func teamScores(teams []Team, matches []Match) []TeamScore {
    score := make(map[int]int)
    for _, t := range teams {
        score[t.TeamId] = 0
    }

    for _, m := range matches {
        if m.HostGoals > m.GuestGoals {
            score[m.HostTeam] += 3
        } else if m.HostGoals < m.GuestGoals {
            score[m.GuestTeam] += 3
        } else {
            score[m.HostTeam] += 1
            score[m.GuestTeam] += 1
        }
    }

    res := make([]TeamScore, 0, len(teams))
    for _, t := range teams {
        res = append(res, TeamScore{
            TeamId: t.TeamId,
            TeamName: t.TeamName,
            NumPoints: score[t.TeamId],
        })
    }

    sort.Slice(res, func(i, j int) bool {
        if res[i].NumPoints != res[j].NumPoints {
            return res[i].NumPoints > res[j].NumPoints
        }
        return res[i].TeamId < res[j].TeamId
    })
    return res
}

相似题目

题目 难度 考察点
1158. 市场分析 I 中等 统计
1193. 每月交易 I 中等 分组统计
1327. 列出指定时间段内所有的下单产品 简单 统计
1179. 重新格式化部门表 简单 分组统计
1107. 每日新用户统计 中等 统计
本文作者:
本文链接: https://hgnulb.github.io/blog/2022/59322178
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处!