You are viewing a single comment's thread. Return to all comments →
Java sol
private static boolean[] isPrime = new boolean[10000]; public static void sieve() { int n = isPrime.length; for (int i = 0; i < n; i++) { isPrime[i] = true; } isPrime[0] = false; isPrime[1] = false; for (int i = 2; i < n; i++) { if (isPrime[i] == true) { for (int j = i * i; j < n; j += i) { isPrime[j] = false; } } } } public static List<Integer> thPrime(int q) { List<Integer> primes = new ArrayList<>(); sieve(); int count = 0; int idx = 2; while (count < q) { if (isPrime[idx] == true) { primes.add(idx); count++; } idx++; } return primes; } public static List<Integer> waiter(List<Integer> number, int q) { List<Integer> ans = new ArrayList<>(); Stack<Integer> stA = new Stack<>(); Stack<Integer> stB = new Stack<>(); List<Integer> primes = thPrime(q); for (int i = 0; i < primes.size(); i++) { if (i == 0) { for (int j = number.size() - 1; j >= 0; j--) { if (number.get(j) % primes.get(i) == 0) { stB.add(number.get(j)); } else { stA.add(number.get(j)); } } } else { Stack<Integer> stA1 = new Stack<>(); while(!stA.isEmpty()) { if (stA.peek() % primes.get(i) == 0) { stB.add(stA.pop()); } else { stA1.add(stA.pop()); } } stA = stA1; } while (!stB.isEmpty()) { ans.add(stB.pop()); } } while (!stA.isEmpty()) { ans.add(stA.pop()); } return ans; }
Seems like cookies are disabled on this browser, please enable them to open this website
Waiter
You are viewing a single comment's thread. Return to all comments →
Java sol