-
Notifications
You must be signed in to change notification settings - Fork 2
/
Decompose.java
107 lines (93 loc) · 3.02 KB
/
Decompose.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.*;
import java.util.Iterator;
public class Decompose {
static Boolean debug = false;
public static String decompose(long n) {
if (n == 11) {
debug = false;
}
Map<Long, Set<Long>> memo = new HashMap<>();
Set<Long> decomposeSet = decompose(n*n, true, memo);
System.out.println(Arrays.toString(decomposeSet.toArray()));
String solution = "";
for (Long i: decomposeSet) {
solution += i + " ";
}
return solution.equals("") ? "" : solution.substring(0, solution.length() - 1);
}
public static Set<Long> decompose(long nSq, Boolean start, Map<Long, Set<Long>> memo) {
if (memo.containsKey(nSq)) {
if (debug) {
System.out.println("Used Memo:" + nSq);
}
return memo.get(nSq);
}
Set<Long> result = new TreeSet<>();
long n = (long)Math.sqrt(nSq);
// not sure on the base case
if (nSq == 1) {
result.add(1L);
memo.put(nSq, result);
if (debug) {
System.out.println("added Base Case");
}
return result;
}
// Classic DP?
// Find the largest decomp for a smaller number and then add the larger one?
int smallestSet = Integer.MAX_VALUE;
for (long i = n; i > 0; i--) {
Set<Long> subset = new TreeSet<>();
Long iSq = i*i;
if (!start && iSq == nSq) {
result.add(i);
break;
} else if (iSq < nSq) {
subset = decompose(nSq - iSq, false, memo);
if (subset == null || subset.size() == 0 || subset.contains(i)) {
// not a valid option must be strictly increasing
continue;
} else {
subset = new TreeSet<Long>(subset);
subset.add(i);
// Just take the first one that gives you a real answer
memo.put(nSq, subset);
return subset;
}
}
}
if (result.size() > 0) {
memo.put(nSq, result);
}
return result;
}
// public static Boolean isBetterSet(Set<Long> set1, Set<Long> set2) {
// if (debug) {
// System.out.println(Arrays.toString(set1.toArray()));
// System.out.println(Arrays.toString(set2.toArray()));
// try{Thread.sleep(1000);} catch(Exception e) {};
// }
// Iterator s1 = ((TreeSet<Long>)set1).descendingIterator();
// Iterator s2 = ((TreeSet<Long>)set2).descendingIterator();
// while (s1.hasNext() && s2.hasNext()) {
// Long s1Val = (Long)s1.next();
// Long s2Val = (Long)s2.next();
// if (s1Val > s2Val) {
// if (debug) {
// System.out.println(s1Val);
// System.out.println(s2Val);
// try{Thread.sleep(1000);} catch(Exception e) {};
// }
// return true;
// } else if (s1Val < s2Val) {
// if (debug) {
// System.out.println(s1Val);
// System.out.println(s2Val);
// try{Thread.sleep(1000);} catch(Exception e) {};
// }
// return false;
// }
// }
// return s1.hasNext(); // if one is shorter than the other.. witht he same values... that'll never happen
// }
}