Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 20, 2016 23:39
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/6fa69ef28c849d63ed0dc4b419cd0dee to your computer and use it in GitHub Desktop.
Save jianminchen/6fa69ef28c849d63ed0dc4b419cd0dee to your computer and use it in GitHub Desktop.
Minimum Cost - HackerRank - woman codesprint#2 - TreeSet
import java.io.*;
import java.util.*;
public class b {
public static void main(String[] args) throws IOException {
input.init(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = input.nextInt();
long[] ps = new long[n];
for(int i = 0; i<n; i++) ps[i] = input.nextLong();
long res = (long)1e18;
TreeSet<Long> set = new TreeSet<Long>();
for(int i = 0; i<n; i++)
{
set.add(ps[i]);
if(set.higher(ps[i]) != null) res = Math.min(res, set.higher(ps[i]) - ps[i]);
}
out.println(res);
out.close();
}
public static class input {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment