UglyNumber { public int nthSuperUglyNumber(int n, int[] primes) { if (n < 1) throw new IllegalArgumentException("n can't be less than 1"); if (n == 1) return 1; int i = 1, next = 1; ArrayList<ValueNext> list = new ArrayList<>(); list.add(new ValueNext(1)); while (i < n) { int min = (int)list.stream().filter(vn -> vn.next < primes.length) .mapToLong(vn -> (long)vn.value * primes[vn.next]).min().getAsLong(); next = min; for (ValueNext vn: list) { if (min % vn.value == 0 && IntStream.of(primes).filter(p -> p == min / vn.value).findAny().isPresent()) { vn.next++; } } list.add(new ValueNext(min)); i++; } return next; }  boolean isUgly(int num); int nthUglyNumber(int n); int nthSuperUglyNumber(int n, int[] primes);  }
@Test(dataProvider = "superUglyDP") public void testSuperUgly(int n, int[] primes, int expected) { assertEquals(new UglyNumber().nthSuperUglyNumber(n, primes), expected); }