Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 30, 2016 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/83f0079acbcfc4b6de3f5b1aff6aa131 to your computer and use it in GitHub Desktop.
Save jianminchen/83f0079acbcfc4b6de3f5b1aff6aa131 to your computer and use it in GitHub Desktop.
Minimum Cost - HackerRank woman codesprint #2 - using LINQ only, timeout on test case 11 - 15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace minimumLoss
{
class Program
{
static void Main(string[] args)
{
Process();
}
private static void Process()
{
int n = int.Parse(Console.ReadLine());
Int64[] prices = new Int64[n];
string[] arr = Console.ReadLine().Split(' ');
prices = Array.ConvertAll(arr, Int64.Parse);
Console.WriteLine(MinimumLossCal(n, prices));
}
/*
* Try this one - Nov. 30, 2016
*
* source code from:
* http://codereview.stackexchange.com/a/148557/123986
*/
private static Int64 MinimumLossCal(int n, Int64[] prices)
{
return prices
//calculate all the differences
.SelectMany((currentPrice, i) => prices.Take(i).Select(p => p - currentPrice))
//pick only those, that are positive (loss)
.Where(difference => difference > 0)
//pick minimum
.Min();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment