private static int[] candy(int[] ratings) { int[] candy = new int[ratings.length]; Arrays.fill(candy, 1); // increasing for (int i = 1; i < ratings.length; i++) { if (ratings[i] > ratings[i - 1]) { candy[i] = candy[i - 1] + 1; } } // decreasing for (int j = ratings.length - 2; j >= 0; j--) { if (ratings[j] > ratings[j + 1]) { if (((j - 1 >= 0)) && (ratings[j - 1] < ratings[j])) { candy[j] = Math.max(candy[j], candy[j - 1] + 1); } else { candy[j] = candy[j + 1] + 1; } } } // int sum = 0; // for(int k=0; k<candy.length; k++) sum+=candy[k]; return candy; }
public static int findSameMin(int[] A, int[] B) { Arrays.sort(A); Arrays.sort(B); int indexA = 0; int indexB = 0; while (indexA < A.length && indexB < B.length) { int headOfA = A[indexA]; int headOfB = B[indexB]; if (headOfA == headOfB) { return headOfA; } else if (headOfA < headOfB) { indexA++; } else { indexB++; } } return -1; }
public static void main(String[] args) { Scanner in = new Scanner(System.in); int times = in.nextInt(); for (int i = 0; i < times; i++) { int size = in.nextInt(); int[] nums = new int[size]; for (int s = 0; s < size; s++) { nums[s] = in.nextInt(); } Arrays.sort(nums); out.println("Case " + (i + 1) + ": " + nums[nums.length / 2]); } }
/** * Returns compact class host. * * @param obj Object to compact. * @return String. */ @Nullable public static Object compactObject(Object obj) { if (obj == null) return null; if (obj instanceof Enum) return obj.toString(); if (obj instanceof String || obj instanceof Boolean || obj instanceof Number) return obj; if (obj instanceof Collection) { Collection col = (Collection) obj; Object[] res = new Object[col.size()]; int i = 0; for (Object elm : col) res[i++] = compactObject(elm); return res; } if (obj.getClass().isArray()) { Class<?> arrType = obj.getClass().getComponentType(); if (arrType.isPrimitive()) { if (obj instanceof boolean[]) return Arrays.toString((boolean[]) obj); if (obj instanceof byte[]) return Arrays.toString((byte[]) obj); if (obj instanceof short[]) return Arrays.toString((short[]) obj); if (obj instanceof int[]) return Arrays.toString((int[]) obj); if (obj instanceof long[]) return Arrays.toString((long[]) obj); if (obj instanceof float[]) return Arrays.toString((float[]) obj); if (obj instanceof double[]) return Arrays.toString((double[]) obj); } Object[] arr = (Object[]) obj; int iMax = arr.length - 1; StringBuilder sb = new StringBuilder("["); for (int i = 0; i <= iMax; i++) { sb.append(compactObject(arr[i])); if (i != iMax) sb.append(", "); } sb.append("]"); return sb.toString(); } return U.compact(obj.getClass().getName()); }
/** * Concat arrays in one. * * @param arrays Arrays. * @return Summary array. */ public static int[] concat(int[]... arrays) { assert arrays != null; assert arrays.length > 1; int len = 0; for (int[] a : arrays) len += a.length; int[] r = Arrays.copyOf(arrays[0], len); for (int i = 1, shift = 0; i < arrays.length; i++) { shift += arrays[i - 1].length; System.arraycopy(arrays[i], 0, r, shift, arrays[i].length); } return r; }
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; }
/** * Run command in separated console. * * @param workFolder Work folder for command. * @param args A string array containing the program and its arguments. * @return Started process. * @throws IOException If failed to start process. */ public static Process openInConsole(@Nullable File workFolder, String... args) throws IOException { String[] commands = args; String cmd = F.concat(Arrays.asList(args), " "); if (U.isWindows()) commands = F.asArray("cmd", "/c", String.format("start %s", cmd)); if (U.isMacOs()) commands = F.asArray( "osascript", "-e", String.format("tell application \"Terminal\" to do script \"%s\"", cmd)); if (U.isUnix()) commands = F.asArray("xterm", "-sl", "1024", "-geometry", "200x50", "-e", cmd); ProcessBuilder pb = new ProcessBuilder(commands); if (workFolder != null) pb.directory(workFolder); return pb.start(); }