I'm trying to solve Kattis problem Compass Card Sales. For this I have a List of Card objects which have uniqueness property. I have to start removing cards with the least uniqueness and recalculate uniqueness value after each removal.
My solution works but it can't pass 8th test case due to time limit. I tried keeping cards as a SortedSet but it worked worse than List. Then I tried with HashSet but it didn't make any difference.
while (CardList.Count > 1)
{
CalculateUniqueness();
Card leastUnique = CardList.Min();
//Card leastUnique = CardList.OrderBy(p => p.TotalUniqueness).ThenByDescending(p => p.Id).First();
Console.WriteLine(leastUnique.Id);
CardList.Remove(leastUnique);
}
Edit: CompareTo method of Card class:
public int CompareTo(object obj)
{
Card otherCard = (Card)obj;
int uniquenessComparison = TotalUniqueness.CompareTo(otherCard.TotalUniqueness);
if (uniquenessComparison != 0)
{
return uniquenessComparison;
}
else
{
return otherCard.Id.CompareTo(Id);
}
}