Created
November 20, 2016 22:30
-
-
Save jianminchen/c382bc1b40e47c2740a25abcaeffb846 to your computer and use it in GitHub Desktop.
Minimum Loss - Woman codesprint - study code - simpler and smarter than my solution
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.IO; | |
using System.Linq; | |
class Solution { | |
static void Main(String[] args) { | |
int len = Convert.ToInt32(Console.ReadLine()); | |
long[] a = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); | |
Dictionary<long, int> dict = new Dictionary<long, int>(); | |
for (int i = 0; i < len; i++){ | |
dict.Add(a[i], i); | |
} | |
long min = long.MaxValue; | |
Array.Sort(a); | |
for (int i = len - 1; i > 0; i--){ | |
long diff = a[i] - a[i - 1]; | |
if (diff < min && dict[a[i]] < dict[a[i - 1]]) | |
min = diff; | |
} | |
Console.WriteLine(min); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment