Created
November 30, 2016 19:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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