public static int minimumTotal(ArrayList<ArrayList<Integer>> triangle) { if (triangle == null) return 0; int[] sum = new int[triangle.size() + 1]; for (int i = triangle.size() - 1; i >= 0; i--) { for (int j = 0; j < triangle.get(i).size(); j++) { List currentList = triangle.get(i); sum[j] = (Integer) (currentList.get(j)) + Math.min(sum[j], sum[j + 1]); } for (int x = 0; x < sum.length; x++) System.out.print(sum[x] + " "); System.out.println(); } return sum[0]; }
/** * Finds all files in folder and in it's sub-tree of specified depth. * * @param file Starting folder * @param maxDepth Depth of the tree. If 1 - just look in the folder, no sub-folders. * @param filter file filter. * @return List of found files. */ public static List<VisorLogFile> fileTree(File file, int maxDepth, @Nullable FileFilter filter) { if (file.isDirectory()) { File[] files = (filter == null) ? file.listFiles() : file.listFiles(filter); if (files == null) return Collections.emptyList(); List<VisorLogFile> res = new ArrayList<>(files.length); for (File f : files) { if (f.isFile() && f.length() > 0) res.add(new VisorLogFile(f)); else if (maxDepth > 1) res.addAll(fileTree(f, maxDepth - 1, filter)); } return res; } return F.asList(new VisorLogFile(file)); }
private static List<List<Integer>> threeSum(int[] num) { List resultList = new ArrayList<ArrayList>(); Arrays.sort(num); int length = num.length; for (int i = 0; i < length - 2; ) { int j = i + 1, k = length - 1; while (j < k) { int sum = num[i] + num[j] + num[k]; if (sum == 0) { List<Integer> list = new ArrayList<Integer>(); list.add(num[i]); list.add(num[j]); list.add(num[k]); resultList.add(list); do { j++; } while (num[j] == num[j - 1]); do { k--; } while (num[k] == num[k + 1]); } else if (sum > 0) { do { k--; } while (num[k] == num[k + 1]); } else { do { j++; } while (num[j] == num[j - 1]); } } do { i++; } while (num[i] == num[i - 1]); } return resultList; }
private void coordinator() throws InterruptedException { List<ActiveEvent> activeEvents = new ArrayList<>(); long curTime = System.currentTimeMillis(); for (User user : users) { if (user.isActive()) { for (Event event : user.getEvents()) { if (event.getDate().getTime() >= curTime) { activeEvents.add(new ActiveEvent(user.getName(), event.getDate(), event.getText())); } } } } Collections.sort(activeEvents); for (int i = 0; i < activeEvents.size(); i++) { if (i == 0) { Thread.sleep(activeEvents.get(i).getTime() - System.currentTimeMillis()); } else { Thread.sleep(activeEvents.get(i).getTime() - activeEvents.get(i - 1).getTime()); } activeEvents.get(i).print(); } }
private void scannerLoop() throws ParseException { Scanner reader = new Scanner(in); String line = reader.nextLine(); while (!line.equals("startScheduling")) { String[] params = line.split("\\(|,( )*|\\)"); switch (params[0]) { case "create": if (params.length == 4) { User temp = find(params[1]); if (temp == null) { User user = new User(params[1], Integer.parseInt(params[2]), Boolean.parseBoolean(params[3])); users.add(user); } else { out.print("Error: user with this name already exist"); } } else { out.print("Error: wrong arguments"); } break; case "modify": if (params.length == 4) { User temp = find(params[1]); if (!(temp == null)) { temp.setName(params[1]); temp.setTimezone(Integer.parseInt(params[2])); temp.setStatus(Boolean.parseBoolean(params[3])); } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; case "addEvent": if (params.length == 4) { User temp = find(params[1]); if (!(temp == null)) { Pattern pattern = Pattern.compile( "(0?[1-9]|[12][0-9]|3[01])\\.(0?[1-9]|1[012])\\.((19|20)\\d\\d)\\-([01]?[0-9]|2[0-3])\\:[0-5][0-9]\\:[0-5][0-9]"); Matcher matcher = pattern.matcher(params[3]); if (matcher.matches()) { DateFormat df = new SimpleDateFormat("dd.MM.yyyy'-'HH:mm:ss"); Date result = df.parse(params[3]); result.setTime( result.getTime() + (defaultTimezone - temp.getTimezone()) * 3600 * 1000); Event event = new Event(result, params[2]); temp.addEvent(event); } else { out.print("Error: wrong dateformat"); } } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; case "addRandomTimeEvent": if (params.length == 5) { User temp = find(params[1]); if (!(temp == null)) { Pattern pattern = Pattern.compile( "(0?[1-9]|[12][0-9]|3[01])\\.(0?[1-9]|1[012])\\.((19|20)\\d\\d)\\-([01]?[0-9]|2[0-3])\\:[0-5][0-9]\\:[0-5][0-9]"); Matcher matcherFrom = pattern.matcher(params[3]); Matcher matcherTo = pattern.matcher(params[4]); if (matcherFrom.matches() && matcherTo.matches()) { long dateFrom = Timestamp.valueOf(params[3]).getTime(); long dateTo = Timestamp.valueOf(params[4]).getTime(); long diff = dateFrom - dateTo + 1; Event event = new Event(new Date(dateFrom + (long) (Math.random() * diff)), params[2]); temp.addEvent(event); } else { out.print("Error: wrong dateformat"); } } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; case "removeEvent": if (params.length == 3) { User temp = find(params[1]); if (!(temp == null)) { temp.removeEvent(params[2]); } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; case "cloneEvent": if (params.length == 4) { User temp = find(params[1]); User tempTo = find(params[3]); if (!(temp == null) && !(tempTo == null)) { Event event = temp.findEvent(params[2]); tempTo.addEvent(event); } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; case "showInfo": if (params.length == 2) { User temp = find(params[1]); if (!(temp == null)) { temp.showInfo(); } else { out.print("Error: user with this name not found"); } } else { out.print("Error: wrong arguments"); } break; } line = reader.nextLine(); } }
public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader(args[0])); BufferedReader readIn = new BufferedReader(new InputStreamReader(System.in)); int lineCount = 0; while (in.readLine() != null) { lineCount++; } // array of lines from file read-in String[] unsorted = new String[lineCount]; int i = 0; // string we are cutting into substrings String sep = ""; String roomName = ""; int r = 0; String desc = ""; String temp = ""; List adventure = new List(); while (i < lineCount) { sep = unsorted[i]; temp = sep.substring(0, 1); String[][] cmds = new String[30][2]; if (temp.equals("r")) { if (roomName != "") { adventure.insertRoom(roomName, desc, cmds); r = 0; } roomName = sep.substring(2, sep.length()); } if (temp.equals("d")) { desc = desc.concat(sep.substring(2, sep.length())); } if (temp.equals("o")) { cmds[r][0] = sep.substring(2, sep.length()); } if (temp.equals("t")) { cmds[r][1] = sep.substring(2, sep.length()); } i++; } String room = adventure.firstRoom(); String option = ""; char input = 's'; Stack history = new Stack(); history.push(room); // Input from user while (!option.equals("q")) { option = readIn.readLine(); if (option.length() > 1) { System.out.println("Invalid choice! Try again"); } else { input = option.charAt(0); if (option != "") { // checks if its in range of valid options if (((int) input) - 97 < 13) { room = adventure.choice(room, input); history.push(room); adventure.options(room); } else { if ((int) input - 97 == 16) { System.out.println("Exit status: 0"); System.exit(0); } else if ((int) input - 97 == 17) { history.restart(adventure.firstRoom()); } } } } } }