Created
September 11, 2015 19:20
-
-
Save mh-github/75736e90f7aa2a99bfbf to your computer and use it in GitHub Desktop.
This file contains hidden or 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.util.Scanner; | |
| import java.util.PriorityQueue; | |
| import java.util.Map; | |
| import java.util.Map.Entry; | |
| import java.util.TreeMap; | |
| import java.io.FileInputStream; | |
| import java.io.IOException; | |
| public class DistrictTopAccounts { | |
| static PriorityQueue<Pair> pq; | |
| static TreeMap<Integer, PriorityQueue<Pair>> treemap = new TreeMap<>(); | |
| public static void main(String... args) throws IOException { | |
| System.out.println("Executing DistrictTopAccounts... "); | |
| try (Scanner in = new Scanner( | |
| new FileInputStream(args[0]))) { | |
| String line; | |
| String delimiter = " "; | |
| while (in.hasNextLine()) { | |
| line = in.nextLine(); | |
| String[] tokens = line.split(delimiter); | |
| int account_id = Integer.parseInt(tokens[0]); | |
| int district_id = Integer.parseInt(tokens[1]); | |
| float balance = Float.parseFloat(tokens[2]); | |
| Pair pair = new Pair(account_id, balance); | |
| PriorityQueue<Pair> accountBalance = treemap.get(district_id); | |
| if (accountBalance == null) { | |
| accountBalance = new PriorityQueue<Pair>(4); | |
| accountBalance.add(new Pair(account_id, balance)); | |
| treemap.put(district_id, accountBalance); | |
| } | |
| else { | |
| accountBalance.add(pair); | |
| if (accountBalance.size() > 3) | |
| accountBalance.poll(); | |
| } | |
| } | |
| System.out.println(treemap.size()); | |
| // print the results | |
| for (Map.Entry<Integer, PriorityQueue<Pair>> entry : treemap.entrySet()) { | |
| System.out.print("District id : " + entry.getKey()); | |
| System.out.print(". Top three accounts - "); | |
| for (Pair pair : entry.getValue()) { | |
| System.out.print("account_id: " + pair.getKey() + " balance: " + pair.getValue() + " "); | |
| } | |
| System.out.println(""); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment