Не пойму что нужно валидатору. Не принимает последний пункт. Код справляется с значением N = 1.000.000.000 в пределах секунды. С Long.MAX_VALUE
за 44 - 47 секунд. Выдает все числа Армстронга и ничего лишнего. Вот вывод для значений N = 1.000.000.000 и Long.MAX_VALUE соответственно :
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153]
memory 3634
time = 0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153, 4679307774, 32164049650, 32164049651, 40028394225, 42678290603, 44708635679, 49388550606, 82693916578, 94204591914, 28116440335967, 4338281769391370, 4338281769391371, 21897142587612075, 35641594208964132, 35875699062250035, 1517841543307505039, 3289582984443187032, 4498128791164624869, 4929273885928088826]
memory 187137
time = 45
Помогите пожалуйста найти причину не приятия валидатором данного решения.
package com.javarush.task.task20.task2025;
import java.util.*;
/*
Алгоритмы-числа
*/
public class Solution {
public static long[] getNumbers(long N) {
int st = String.valueOf(N).toCharArray().length;
List<Long> list = new ArrayList<>();
List<Long> summa = new ArrayList<>();
if (N < 9) {
for (long j = 0; j < N; j++) {
list.add(j);
}
} else {
for (long j = 0; j < 10; j++) {
summa.add(j);
}
list = generationNumber(summa, 1, N, list);
for (int i = 1; i < st; i++) {
for (int j = 0; j < 10; j++) {
long x = summa.get(j) * j;
summa.remove(j);
summa.add(j, x);
}
list = generationNumber(summa, i + 1, N, list);
}
}
long[] result = new long[list.size()];
Collections.sort(list);
for (int j = 0; j < list.size(); j++) {
if (list.get(j) < N)
result[j] = list.get(j);
}
return result;
}
public static void main(String[] args) {
long a = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(10)));
long b = System.currentTimeMillis();
System.out.println("memory " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("time = " + (b - a) / 1000);
a = System.currentTimeMillis();
System.out.println(Arrays.toString(getNumbers(1000000000)));
b = System.currentTimeMillis();
System.out.println("memory " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (8 * 1024));
System.out.println("time = " + (b - a) / 1000);
}
public static List<Long> generationNumber(List<Long> list, int st, long N, List<Long> result) {
Map<Long, Integer> map = new HashMap<>();
for (int i = 0; i < list.size(); i++) {
map.put(list.get(i), i);
}
for (int i = 0; i < st; i++) {
map = addMap(map, list);
}
result = sort(map, result, st, list, N);
return result;
}
public static Map<Long, Integer> addMap(Map<Long, Integer> proverka, List<Long> znachenie) {
Map<Long, Integer> proverkaPlus = new HashMap<>();
for (Map.Entry<Long, Integer> m : proverka.entrySet()) {
proverkaPlus.put(m.getKey(), m.getValue());
for (int i = 9; i >= 0; i--) {
if (m.getValue() < i) continue;
proverkaPlus.put(m.getKey() + znachenie.get(i), i);
}
}
return proverkaPlus;
}
public static List<Long> sort(Map<Long, Integer> map, List<Long> result, int st, List<Long> list, long N) {
long sum = 0;
int n = 0;
for (Map.Entry<Long, Integer> x : map.entrySet()) {
sum = 0;
n = 0;
long a = x.getKey();
if (a >= N) continue;
do {
try {
sum += list.get((int) (a % 10));
} catch (IndexOutOfBoundsException e) {
}
a = a / 10;
n++;
} while (!(a < 1));
if ((n != st)) continue;
if (sum == x.getKey() && !result.contains(sum)) result.add(sum);
}
return result;
}
}