Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 20, 2016 22:30
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/c382bc1b40e47c2740a25abcaeffb846 to your computer and use it in GitHub Desktop.
Save jianminchen/c382bc1b40e47c2740a25abcaeffb846 to your computer and use it in GitHub Desktop.
Minimum Loss - Woman codesprint - study code - simpler and smarter than my solution
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