You are viewing a single comment's thread. Return to all comments →
Here's a Java solution that's guaranteed to run under 3 seconds -
private static boolean iterateAndRemove1(int[] arr) { int prev = Integer.MAX_VALUE; boolean hasRemoved = false; for (int i = 0; i < arr.length; i++) { var value = arr[i]; if (value > -1) { if (value > prev) { arr[i] = -1; hasRemoved = true; } prev = value; } } return hasRemoved; } public static int processGarden(List<Integer> in) { int days = 0; int[] arr = in .stream() .mapToInt(Integer::intValue) .toArray(); while (iterateAndRemove1(arr)) { days++; if (days % 100 == 0) { arr = Arrays .stream(arr) .filter(value -> value >= 0) .toArray(); } } return days; }
Seems like cookies are disabled on this browser, please enable them to open this website
Poisonous Plants
You are viewing a single comment's thread. Return to all comments →
Here's a Java solution that's guaranteed to run under 3 seconds -