注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
TimeLine : 11月23收到链接 11月26做的题
坐标 多伦多
第一道题 是 给一个List 给商店打星
具体描述见这里 https://aonecode.com/amazon-online-assessment-five-star-sellers
C# 代码
[Java] 纯文本查看 复制代码
public class CustomComparator : IComparer<Tuple<int, int>>
{
public int Compare(Tuple<int, int> x, Tuple<int, int> y)
{
double first = (double)(x.Item1 + 1) / (double)(x.Item2 + 1) - (double)x.Item1 / (double)x.Item2;
double second = (double)(y.Item1 + 1) / (double)(y.Item2 + 1) - (double)y.Item1 / (double)y.Item2;
return second.CompareTo(first) ;
}
}
public int fiveStarReviews(int[][] productRatings, int ratingsThreshold)
{
var comparator = new CustomComparator();
SortedDictionary<Tuple<int, int>, int> pairs = new SortedDictionary<Tuple<int, int>, int>(comparator);
double sumRating = 0;
for(int i = 0; i < productRatings.Length; i++)
{
double percentageFiveStar = (double)productRatings[i][0] / (double)productRatings[i][1] ;
var newPair = new Tuple<int, int>(productRatings[i][0], productRatings[i][1]);
if (!pairs.ContainsKey(newPair))
pairs.Add(newPair, 0);
pairs[newPair]++;
sumRating += percentageFiveStar;
}
double totalThreshhold = ratingsThreshold * productRatings.Length / 100.00;
int step = 0;
while (sumRating < totalThreshhold)
{
var element = pairs.First();
sumRating = sumRating - (double)element.Key.Item1 / (double)element.Key.Item2 + (double)(element.Key.Item1 + 1) / (double)(element.Key.Item2 + 1);
step++;
if(element.Value > 1)
{
pairs[element.Key]--;
}
else
{
pairs.Remove(element.Key);
}
var newPair = new Tuple<int, int>(element.Key.Item1 + 1, element.Key.Item2 + 1);
if (!pairs.ContainsKey(newPair))
pairs.Add(newPair, 0);
pairs[newPair]++;
//Console.WriteLine("step: " + step + ", sumRating: " + sumRating + " ,numerator: " +newPair.Item1 + " ,demorator:" + newPair.Item2);
Console.WriteLine("step: " + step + " ,numerator: " +newPair.Item1 + " ,demorator:" + newPair.Item2);
}
return step;
}
求加米
|