我有一个涉及2个团队的GameGame具有ScoreEvents的列表。对于计分的团队,每个ScoreEvent均为1分。我需要知道每个团队的最大领先得分是多少(如果他们从未获得领先,则为0)。 ScoreEvents列表由TimeSinceStart排序。

public class ScoreEvent
{
    public int TeamId { get; set; }
    public TimeSpan TimeSinceStart { get; set; }
}

public void GetMaxScoreLead()
{
    var ScoreEvents = new List<ScoreEvent>
    {
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(100)},
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(200)},
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(300)},

        //Score at 300 ticks is 3-0 to TeamdId = 0

        new ScoreEvent { TeamId = 1, TimeSinceStart = new TimeSpan(400)},
        new ScoreEvent { TeamId = 1, TimeSinceStart = new TimeSpan(500)},
        new ScoreEvent { TeamId = 1, TimeSinceStart = new TimeSpan(600)},
        new ScoreEvent { TeamId = 1, TimeSinceStart = new TimeSpan(700)},

        //Score at 700 ticks is a 3-4 to TeamId = 1

        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(800)},
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(900)},
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(1000)},
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(1100)}

        //Score at 1100 ticks is 7-4 to TeamId 0
    };
}


因此,对于上面的示例,每个团队最大领导的答案将是:


TeamId(0)= 3大领先
TeamId(1)= 1个最佳线索


编辑:我必须的代码。我知道我需要在某个地方跟踪当前分数。

var teamZeroLargestLead = 0;
var teamOneLargestLead = 0;

var internalTeamZeroLargestLead = 0;
var internalTeamOneLargestLead = 0;

foreach (var scoreEvent in scoreEvents.OrderBy(x => x.TimeSinceStart))
{
    if (scoreEvent.TeamId == 0)
    {
        if (internalTeamOneLargestLead > teamOneLargestLead)
        {
             teamOneLargestLead = internalTeamOneLargestLead;
             internalTeamOneLargestLead = 0;
        }

        internalTeamZeroLargestLead += 1;
     }
     else
     {
        if(internalTeamZeroLargestLead > teamZeroLargestLead)
        {
            teamZeroLargestLead = internalTeamZeroLargestLead;
            internalTeamZeroLargestLead = 0;
        }

        internalTeamOneLargestLead += 1;
    }
}

最佳答案

我用foreach循环略微更新和简化了您的算法,现在它返回正确的结果-teamZeroLead3teamOneLead1

var teamZeroLead = 0;
var teamOneLead = 0;
var teamZeroScore = 0;
var teamOneScore = 0;

foreach (var scoreEvent in scoreEvents.OrderBy(x => x.TimeSinceStart))
{
    if (scoreEvent.TeamId == 0)
    {
        teamZeroScore++;
        teamZeroLead = Math.Max(teamZeroLead, teamZeroScore - teamOneScore);
    }
    else
    {
         teamOneScore++;
         teamOneLead = Math.Max(teamOneLead, teamOneScore - teamZeroScore);
    }
}


在每个循环迭代中,您正在计算每个团队的当前得分,然后计算潜在客户值并将其分配给结果值(如果该值大于先前计算的值)。

可以使用Aggregate方法和值元组编写相同的逻辑,您可以选择对您而言更易读和方便的内容

var result = scoreEvents.Aggregate((teamZeroLead: 0, teamOneLead: 0, teamZeroScore: 0, teamOneScore: 0),
    (scores, scoreEvent) =>
    {
        if (scoreEvent.TeamId == 0)
        {
            scores.teamZeroScore++;
            scores.teamZeroLead = Math.Max(scores.teamZeroLead, scores.teamZeroScore - scores.teamOneScore);
        }
        else
        {
            scores.teamOneScore++;
            scores.teamOneLead = Math.Max(scores.teamOneLead, scores.teamOneScore - scores.teamZeroScore);
        }

        return scores;
    });


执行后,您可以使用result.teamZeroLeadresult.teamOneLead获得结果值

09-30 21:58