Created
November 20, 2016 23:39
-
-
Save jianminchen/6fa69ef28c849d63ed0dc4b419cd0dee to your computer and use it in GitHub Desktop.
Minimum Cost - HackerRank - woman codesprint#2 - TreeSet
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
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